DATABASE SQL PROJET
-- DROP TABLES
DROP TABLE IF EXISTS employees CASCADE;
DROP TABLE IF EXISTS shops CASCADE;
DROP TABLE IF EXISTS locations CASCADE;
DROP TABLE IF EXISTS suppliers CASCADE;
-- CREATE TABLES ========================================
-- Create employees table
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
email VARCHAR(50),
hire_date DATE,
gender VARCHAR(1), -- "M"/"F" (male/female)
salary INT,
coffeeshop_id INT
);
-- Create shops table
CREATE TABLE shops (
coffeeshop_id INT PRIMARY KEY,
coffeeshop_name VARCHAR(50),
city_id INT
);
-- Add foreign key to the employees table
ALTER TABLE employees
ADD FOREIGN KEY (coffeeshop_id)
REFERENCES shops(coffeeshop_id)
ON DELETE SET NULL;
-- Create locations table
CREATE TABLE locations (
city_id INT PRIMARY KEY,
city VARCHAR(50),
country VARCHAR(50)
);
-- Add foreign key to shops table
ALTER TABLE shops
ADD FOREIGN KEY (city_id)
REFERENCES locations(city_id)
ON DELETE SET NULL;
-- Create suppliers table
CREATE TABLE suppliers (
coffeeshop_id INT,
supplier_name VARCHAR(40),
coffee_type VARCHAR(20),
PRIMARY KEY (coffeeshop_id, supplier_name),
FOREIGN KEY (coffeeshop_id) REFERENCES shops(coffeeshop_id)
ON DELETE CASCADE
);
-- INSERT INTO TABLES ====================================
-- Insert first two entries into employees table
-- Set the coffeeshop_id to NULL for now as we have not inserted the coffeeshop values into
the shops table yet
INSERT INTO employees VALUES (501559, 'Carson', 'Mosconi', '
[email protected]',
'2015/08/29', 'M', 32973, NULL);
INSERT INTO employees VALUES (144108, 'Khalil', 'Corr', '
[email protected]', '2014/04/23',
'M', 52802, NULL);
-- Insert the shop with coffeeshop_id = 1 into the shops table
-- Set to city_id to NULL for now as we have not inserted the city values into the locations
table yet
INSERT INTO shops VALUES(1, 'Common Grounds', NULL);
-- Now set the coffeeshop_id for the first two employees we inserted to 1
UPDATE employees
SET coffeeshop_id = 1
WHERE employee_id IN (501559, 144108);
-- Insert Los Angeles into the locations table
INSERT INTO locations VALUES(1, 'Los Angeles', 'United States');
-- Now you can update the city_id in the shops table for coffeeshop_id = 1 to 1
UPDATE shops
SET city_id = 1
WHERE coffeeshop_id = 1;
-- Insert the first two values where coffeeshop_id = 1 into the suppliers table
INSERT INTO suppliers VALUES(1, 'Beans and Barley', 'Arabica');
INSERT INTO suppliers VALUES(1, 'Cool Beans', 'Robusta');
-- copy and past the rest of code =================================
-- NOW YOU CAN DO THE REST OF THE INSERT STATEMENTS
-- Insert into the locations table
INSERT INTO locations VALUES(2, 'New York', 'United States');
INSERT INTO locations VALUES(3, 'London', 'United Kingdom');
-- Insert into the shops table
INSERT INTO shops VALUES(2, 'Early Rise', 2);
INSERT INTO shops VALUES(3, 'Ancient Bean', 3);
INSERT INTO shops VALUES(4, 'Urban Grind', 1);
INSERT INTO shops VALUES(5, 'Trembling Cup', 2);
-- Insert into the suppliers table
INSERT INTO suppliers VALUES(2, 'Vanilla Bean', 'Liberica');
INSERT INTO suppliers VALUES(2, 'Beans and Barley', 'Arabica');
INSERT INTO suppliers VALUES(2, 'Cool Beans', 'Robusta');
INSERT INTO suppliers VALUES(3, 'Bean Me Up', 'Excelsa');
INSERT INTO suppliers VALUES(3, 'Vanilla Bean', 'Liberica');
INSERT INTO suppliers VALUES(3, 'Cool Beans', 'Robusta');
INSERT INTO suppliers VALUES(3, 'Beans and Barley', 'Arabica');
INSERT INTO suppliers VALUES(4, 'Vanilla Bean', 'Liberica');
INSERT INTO suppliers VALUES(4, 'Bean Me Up', 'Excelsa');
INSERT INTO suppliers VALUES(5, 'Beans and Barley', 'Arabica');
INSERT INTO suppliers VALUES(5, 'Vanilla Bean', 'Liberica');
INSERT INTO suppliers VALUES(5, 'Bean Me Up', 'Excelsa');
-- And finally insert all the remaining values into the employees table
INSERT INTO employees VALUES (782284, 'Vilhelmina', 'Rayman', '[email protected]',
'2015/08/17', 'F', 57855, 2);
INSERT INTO employees VALUES (225709, 'Eleen', 'Tarpey', '[email protected]',
'2016/09/14', 'F', 48048, 3);
INSERT INTO employees VALUES (614903, 'Hamel', 'Grocock', '[email protected]',
'2016/03/27', 'M', 66566, 3);
INSERT INTO employees VALUES (590293, 'Frazier', 'Balls', '[email protected]',
'2021/12/22', 'M', 15235, 3);
INSERT INTO employees VALUES (243999, 'Jeremy', 'Whitlam',
'[email protected]', '2014/01/21', 'M', 41159, 4);
INSERT INTO employees VALUES (599230, 'Webb', 'Hevey', '[email protected]',
'2010/04/27', 'M', 48477, 4);
INSERT INTO employees VALUES (758331, 'Katharine', 'Sexcey', '[email protected]',
'2014/07/03', 'F', 23772, 5);
INSERT INTO employees VALUES (561012, 'Barton', 'Lillow', '[email protected]',
'2015/08/17', 'M', 15083, 5);
INSERT INTO employees VALUES (938560, 'Samantha', 'Newall', '[email protected]',
'2013/08/10', 'F', 10223, 2);
INSERT INTO employees VALUES (746871, 'Joshua', 'Winscum',
'[email protected]', '2022/12/29', 'M', 28232, 2);
INSERT INTO employees VALUES (75097, 'Wally', 'Huebner', '[email protected]',
'2020/08/30', 'F', 57731, 4);
INSERT INTO employees VALUES (659627, 'Austen', 'Waterhouse',
'[email protected]', '2011/06/25', 'M', 32946, 1);
INSERT INTO employees VALUES (755091, 'Clem', 'Kitchingman',
'[email protected]', '2014/07/23', 'M', 46818, 4);
INSERT INTO employees VALUES (925779, 'Pavel', 'Butchard', '[email protected]',
'2016/09/21', 'M', 35003, 5);
INSERT INTO employees VALUES (510410, 'Tarra', 'Rolin', '[email protected]',
'2019/03/21', 'F', 27191, 4);
INSERT INTO employees VALUES (353657, 'Brigham', 'Boucher', '[email protected]',
'2016/03/09', 'M', 38899, 2);
INSERT INTO employees VALUES (877425, 'Horten', 'Byre', NULL, '2022/05/21', 'M', 40458,
5);
INSERT INTO employees VALUES (608868, 'Annabelle', 'Ottiwill', '[email protected]' ,
'2016/07/19', 'F', 54857, 5);
INSERT INTO employees VALUES (593979, 'Rockie', 'Meriot', '[email protected]',
'2011/07/29', 'M', 45651, 4);
INSERT INTO employees VALUES (649417, 'Terese', 'Monshall',
'[email protected]', '2010/02/09', 'F', 13829, 1);
INSERT INTO employees VALUES (125157, 'Bartolomeo', 'Gossage',
'[email protected]', '2019/03/31', 'M', 17474, 4);
INSERT INTO employees VALUES (891720, 'Amye', 'Dilger', '[email protected]',
'2014/12/29', 'F', 63671, 3);
INSERT INTO employees VALUES (826977, 'Lucas', 'Cuphus', NULL, '2021/12/21', 'M', 58566,
5);
INSERT INTO employees VALUES (824952, 'Marja', 'Lacey', NULL, '2010/02/28', 'F', 28628, 5);
INSERT INTO employees VALUES (208729, 'Olenolin', 'Kincla', '[email protected]',
'2017/12/11', 'M', 19570, 2);
INSERT INTO employees VALUES (790257, 'Rab', 'Dafter', '[email protected]',
'2022/02/26', 'M', 49327, 1);
INSERT INTO employees VALUES (54960, 'Eachelle', 'Baniard',
'[email protected]', '2013/05/12', 'F', 29847, 4);
INSERT INTO employees VALUES (450160, 'Hilary', 'Reeman', '[email protected]',
'2023/01/11', 'M', 14374, 1);
INSERT INTO employees VALUES (623414, 'Devi', 'Hasell', '[email protected]',
'2021/05/17', 'F', 53462, 5);
INSERT INTO employees VALUES (334855, 'Domingo', 'Mannooch', '[email protected]',
'2013/04/08', 'M', 12530, 4);
INSERT INTO employees VALUES (185481, 'Kermie', 'Haswell', '[email protected]',
'2016/02/17', 'M', 21871, 2);
INSERT INTO employees VALUES (6027, 'Pattie', 'Piscotti', '[email protected]',
'2022/05/10', 'M', 24899, 3);
INSERT INTO employees VALUES (547028, 'Beilul', 'Plaxton', '[email protected]',
'2011/12/17', 'F', 63922, 4);
INSERT INTO employees VALUES (275024, 'Marius', 'Matisse', '[email protected]',
'2019/06/08', 'M', 29028, 3);
INSERT INTO employees VALUES (399449, 'Sax', 'Crank', '[email protected]',
'2020/08/06', 'M', 16789, 5);
INSERT INTO employees VALUES (572629, 'Linnie', 'Welds', '[email protected]',
'2011/05/29', 'F', 24491, 3);
INSERT INTO employees VALUES (922844, 'Carlen', 'Louis', '[email protected]',
'2017/01/04', 'F', 20130, 2);
INSERT INTO employees VALUES (126377, 'Terence', 'Braz', '[email protected]',
'2023/01/04', 'M', 52151, 4);
INSERT INTO employees VALUES (522236, 'Rosemarie', 'Tottman', '[email protected]',
'2015/04/07', 'F', 53672, 2);
INSERT INTO employees VALUES (793404, 'Aili', 'Stowe', NULL, '2014/08/09', 'F', 19337, 1);
INSERT INTO employees VALUES (854959, 'Gustavus', 'Kettlestringe',
'[email protected]', '2018/04/11', 'M', 47048, 2);
INSERT INTO employees VALUES (916890, 'Joya', 'Bayldon', '[email protected]',
'2012/04/07', 'F', 59867, 4);
QUERIES
-- SELECT statement
-- select all (*) columns from table
SELECT * FROM employees;
SELECT * FROM shops;
SELECT * FROM locations;
SELECT * FROM suppliers;
-- select some (3) columns of table
SELECT
employee_id,
first_name,
last_name
FROM employees;
--===================================================
-- WHERE clause + AND & OR
-- Select only the employees who make more than 50k
SELECT *
FROM employees
WHERE salary > 50000;
-- Select only the employees who work in Common Grounds coffeshop
SELECT *
FROM employees
WHERE coffeeshop_id = 1;
-- Select all the employees who work in Common Grounds and make more than 50k
SELECT *
FROM employees
WHERE salary > 50000 AND coffeeshop_id = 1;
-- Select all the employees who work in Common Grounds or make more than 50k
SELECT *
FROM employees
WHERE salary > 50000 OR coffeeshop_id = 1;
-- Select all the employees who work in Common Grounds, make more than 50k and are male
SELECT *
FROM employees
WHERE
salary > 50000
AND coffeeshop_id = 1
AND gender = 'M';
-- Select all the employees who work in Common Grounds or make more than 50k or are male
SELECT *
FROM employees
WHERE
salary > 50000
OR coffeeshop_id = 1
OR gender = 'M';
--=======================================================
-- IN, NOT IN, IS NULL, BETWEEN
-- Select all rows from the suppliers table where the supplier is Beans and Barley
SELECT *
FROM suppliers
WHERE supplier_name = 'Beans and Barley';
-- Select all rows from the suppliers table where the supplier is NOT Beans and Barley
SELECT *
FROM suppliers
WHERE NOT supplier_name = 'Beans and Barley';
SELECT *
FROM suppliers
WHERE supplier_name <> 'Beans and Barley';
-- Select all Robusta and Arabica coffee types
SELECT *
FROM suppliers
WHERE coffee_type IN ('Robusta', 'Arabica');
SELECT *
FROM suppliers
WHERE
coffee_type = 'Robusta'
OR coffee_type = 'Arabica';
-- Select all coffee types that are not Robusta or Arabica
SELECT *
FROM suppliers
WHERE coffee_type NOT IN ('Robusta', 'Arabica');
-- Select all employees with missing email addresses
SELECT *
FROM employees
WHERE email IS NULL;
-- Select all employees whose emails are not missing
SELECT *
FROM employees
WHERE NOT email IS NULL;
-- Select all employees who make between 35k and 50k
SELECT
employee_id,
first_name,
last_name,
salary
FROM employees
WHERE salary BETWEEN 35000 AND 50000;
SELECT
employee_id,
first_name,
last_name,
salary
FROM employees
WHERE
salary >= 35000
AND salary <= 50000;
--===========================================================
-- ORDER BY, LIMIT, DISTINCT, Renaming columns
-- Order by salary ascending
SELECT
employee_id,
first_name,
last_name,
salary
FROM employees
ORDER BY salary;
-- Order by salary descending
SELECT
employee_id,
first_name,
last_name,
salary
FROM employees
ORDER BY salary DESC;
-- Top 10 highest paid employees
SELECT
employee_id,
first_name,
last_name,
salary
FROM employees
ORDER BY salary DESC
LIMIT 10;
-- Return all unique coffeeshop ids
SELECT DISTINCT coffeeshop_id
FROM employees;
-- Return all unique countries
SELECT DISTINCT country
FROM locations;
-- Renaming columns
SELECT
email,
email AS email_address,
hire_date,
hire_date AS date_joined,
salary,
salary AS pay
FROM employees;
--=========================================================
-- EXTRACT
SELECT
hire_date as date,
EXTRACT(YEAR FROM hire_date) AS year,
EXTRACT(MONTH FROM hire_date) AS month,
EXTRACT(DAY FROM hire_date) AS day
FROM employees;
--=========================================================
-- UPPER, LOWER, LENGTH, TRIM
-- Uppercase first and last names
SELECT
first_name,
UPPER(first_name) AS first_name_upper,
last_name,
UPPER(last_name) AS last_name_upper
FROM employees;
-- Lowercase first and last names
SELECT
first_name,
LOWER(first_name) AS first_name_upper,
last_name,
LOWER(last_name) AS last_name_upper
FROM employees;
-- Return the email and the length of emails
SELECT
email,
LENGTH(email) AS email_length
FROM employees;
-- TRIM
SELECT
LENGTH(' HELLO ') AS hello_with_spaces,
LENGTH('HELLO') AS hello_no_spaces,
LENGTH(TRIM(' HELLO ')) AS hello_trimmed;
--=========================================================
-- Concatenation, Boolean expressions, wildcards
-- Concatenate first and last names to create full names
SELECT
first_name,
last_name,
first_name || ' ' || last_name AS full_name
FROM employees;
-- Concatenate columns to create a sentence
SELECT
first_name || ' ' || last_name || ' makes ' || salary
FROM employees;
-- Boolean expressios
-- if the person makes less than 50k, then true, otherwise false
SELECT
first_name || ' ' || last_name AS full_name,
(salary < 50000) AS less_than_50k
FROM employees;
-- if the person is a female and makes less than 50k, then true, otherwise false
SELECT
first_name || ' ' || last_name AS full_name,
(salary < 50000 AND gender = 'F') AS less_than_50k_female
FROM employees;
-- Boolean expressions with wildcards (% subString)
-- if email has '.com', return true, otherwise false
SELECT
email,
(email like '%.com%') AS dotcom_flag
FROM employees;
SELECT
email,
(email like '%.gov%') AS dotgov_flag
FROM employees;
-- return only government employees
select
first_name || ' ' || last_name as full_name,
email as gov_email
from employees
where email like '%.gov%';
--==========================================================
-- SUBSTRING, POSITION, COALESCE
-- SUBSTRING
-- Get the email from the 5th character
SELECT
email,
SUBSTRING(email FROM 5)
FROM employees;
-- POSITION
-- Find the position of '@' in the email
SELECT
email,
POSITION('@' IN email)
FROM employees;
-- SUBSTRING & POSITION to find the email client
SELECT
email,
SUBSTRING(email FROM POSITION('@' IN email))
FROM employees;
SELECT
email,
SUBSTRING(email FROM POSITION('@' IN email) + 1)
FROM employees;
-- COALESCE to fill missing emails with custom value
SELECT
email,
COALESCE(email, 'NO EMAIL PROVIDED')
FROM employees;
--===================================================
-- MIN, MAX, AVG, SUM, COUNT
-- Select the minimum salary
SELECT MIN(salary) as min_sal
FROM employees;
-- Select the maximum salary
SELECT MAX(salary) as max_sal
FROM employees;
-- Select difference between maximum and minimum salary
SELECT MAX(salary) - MIN(salary)
FROM employees;
-- Select the average salary
SELECT AVG(salary)
FROM employees;
-- Round average salary to nearest integer
SELECT ROUND(AVG(salary),0)
FROM employees;
-- Sum up the salaries
SELECT SUM(salary)
FROM employees;
-- Count the number of entries
SELECT COUNT(*)
FROM employees;
SELECT COUNT(salary)
FROM employees;
SELECT COUNT(email)
FROM employees;
-- summary
SELECT
MIN(salary) as min_sal,
MAX(salary) as max_sal,
MAX(salary) - MIN(salary) as diff_sal,
round(avg(salary), 0) as average_sal,
sum(salary) as total_sal,
count(*) as num_of_emp
FROM employees;
--=========================================================
-- GROUP BY & HAVING
-- Return the number of employees for each coffeeshop
SELECT
coffeeshop_id,
COUNT(employee_id)
FROM employees
GROUP BY coffeeshop_id;
-- Return the total salaries for each coffeeshop
SELECT
coffeeshop_id,
SUM(salary)
FROM employees
GROUP BY coffeeshop_id;
-- Return the number of employees, the avg & min & max & total salaries for each coffeeshop
SELECT
coffeeshop_id,
COUNT(*) AS num_of_emp,
ROUND(AVG(salary), 0) AS avg_sal,
MIN(salary) AS min_sal,
MAX(salary) AS max_sal,
SUM(salary) AS total_sal
FROM employees
GROUP BY coffeeshop_id
ORDER BY num_of_emp DESC;
-- HAVING
-- After GROUP BY, return only the coffeeshops with more than 200 employees
SELECT
coffeeshop_id,
COUNT(*) AS num_of_emp,
ROUND(AVG(salary), 0) AS avg_sal,
MIN(salary) AS min_sal,
MAX(salary) AS max_sal,
SUM(salary) AS total_sal
FROM employees
GROUP BY coffeeshop_id
HAVING COUNT(*) > 200 -- filter, alter "where" after "gruop by"
ORDER BY num_of_emp DESC;
-- After GROUP BY, return only the coffeeshops with a minimum salary of less than 10k
SELECT
coffeeshop_id,
COUNT(*) AS num_of_emp,
ROUND(AVG(salary), 0) AS avg_sal,
MIN(salary) AS min_sal,
MAX(salary) AS max_sal,
SUM(salary) AS total_sal
FROM employees
GROUP BY coffeeshop_id
HAVING MIN(salary) < 10000
ORDER BY num_of_emp DESC;
--===========================================================
-- CASE, CASE with GROUP BY, and CASE for transposing data
-- CASE
-- If pay is less than 50k, then low pay, otherwise high pay
SELECT
employee_id,
first_name || ' ' || last_name as full_name,
salary,
CASE
WHEN salary < 50000 THEN 'low pay'
WHEN salary >= 50000 THEN 'high pay'
ELSE 'no pay'
END as pay_category
FROM employees
ORDER BY salary DESC;
-- If pay is less than 20k, then low pay
-- if between 20k-50k inclusive, then medium pay
-- if over 50k, then high pay
SELECT
employee_id,
first_name || ' ' || last_name as full_name,
salary,
CASE
WHEN salary < 20000 THEN 'low pay'
WHEN salary BETWEEN 20000 and 50000 THEN 'medium pay'
WHEN salary > 50000 THEN 'high pay'
ELSE 'no pay'
END as pay_category
FROM employees
ORDER BY salary DESC;
-- CASE & GROUP BY
-- Return the count of employees in each pay category
SELECT a.pay_category, COUNT(*)
FROM(
SELECT
employee_id,
first_name || ' ' || last_name as full_name,
salary,
CASE
WHEN salary < 20000 THEN 'low pay'
WHEN salary BETWEEN 20000 and 50000 THEN 'medium pay'
WHEN salary > 50000 THEN 'high pay'
ELSE 'no pay'
END as pay_category
FROM employees
ORDER BY salary DESC
)a
GROUP BY a.pay_category;
-- Transpose above
SELECT
SUM(CASE WHEN salary < 20000 THEN 1 ELSE 0 END) AS low_pay,
SUM(CASE WHEN salary BETWEEN 20000 AND 50000 THEN 1 ELSE 0 END) AS
medium_pay,
SUM(CASE WHEN salary > 50000 THEN 1 ELSE 0 END) AS high_pay
FROM employees;
--================================================
-- JOIN
-- Inserting values just for JOIN exercises
INSERT INTO locations VALUES (4, 'Paris', 'France');
INSERT INTO shops VALUES (6, 'Happy Brew', NULL);
-- Checking the values we inserted
SELECT * FROM shops;
SELECT * FROM locations;
-- "INNER JOIN" same as just "J0iN"
SELECT
s.coffeeshop_name,
l.city,
l.country
FROM (
shops s
inner JOIN locations as l
ON s.city_id = l.city_id
);
SELECT
s.coffeeshop_name,
l.city,
l.country
FROM
shops s
JOIN locations l
ON s.city_id = l.city_id;
-- LEFT JOIN
SELECT
s.coffeeshop_name,
l.city,
l.country
FROM
shops s
LEFT JOIN locations l
ON s.city_id = l.city_id;
-- RIGHT JOIN
SELECT
s.coffeeshop_name,
l.city,
l.country
FROM
shops s
RIGHT JOIN locations l
ON s.city_id = l.city_id;
-- FULL OUTER JOIN
SELECT
s.coffeeshop_name,
l.city,
l.country
FROM
shops s
FULL OUTER JOIN locations l
ON s.city_id = l.city_id;
-- Delete the values we created just for the JOIN exercises
DELETE FROM locations WHERE city_id = 4;
DELETE FROM shops WHERE coffeeshop_id = 6;
--========================================================
-- UNION (to stack data on top each other)
-- Return all cities and countries
SELECT city FROM locations
UNION
SELECT country FROM locations;
-- UNION removes duplicates
SELECT country FROM locations
UNION
SELECT country FROM locations;
-- UNION ALL keeps duplicates
SELECT country FROM locations
UNION ALL
SELECT country FROM locations;
-- Return all coffeeshop names, cities and countries
SELECT coffeeshop_name FROM shops
UNION
SELECT city FROM locations
UNION
SELECT country FROM locations;
--=================================================
-- Subqueries
-- Basic subqueries with subqueries in the FROM clause
SELECT *
FROM (
SELECT *
FROM employees
where coffeeshop_id IN (3,4)
) as a;
SELECT
a.employee_id,
a.first_name,
a.last_name
FROM (
SELECT *
FROM employees
where coffeeshop_id IN (3,4)
) a;
-- Basic subqueries with subqueries in the SELECT clause
SELECT
first_name,
last_name,
salary,
(
SELECT MAX(salary)
FROM employees
LIMIT 1
) max_sal
FROM employees;
SELECT
first_name,
last_name,
salary,
(
SELECT ROUND(AVG(salary), 0)
FROM employees
LIMIT 1
) avg_sal
FROM employees;
SELECT
first_name,
last_name,
salary,
salary - ( -- avg_sal
SELECT ROUND(AVG(salary), 0)
FROM employees
LIMIT 1
) avg_sal_diff
FROM employees;
-- Subqueries in the WHERE clause
-- Return all US coffee shops
SELECT *
FROM shops
WHERE city_id IN ( -- US city_id's
SELECT city_id
FROM locations
WHERE country = 'United States'
);
-- Return all employees who work in US coffee shops
SELECT *
FROM employees
WHERE coffeeshop_id IN ( -- US coffeeshop_id's
SELECT coffeeshop_id
FROM shops
WHERE city_id IN ( -- US city-id's
SELECT city_id
FROM locations
WHERE country = 'United States'
)
);
-- Return all employees who make over 35k and work in US coffee shops
SELECT *
FROM employees
WHERE salary > 35000 AND coffeeshop_id IN ( -- US coffeeshop_id's
SELECT coffeeshop_id
FROM shops
WHERE city_id IN ( -- US city_id's
SELECT city_id
FROM locations
WHERE country = 'United States'
)
);
-- 30 day moving total pay
-- The inner query calculates the total_salary of employees who were hired "within" the 30-
day period before the hire_date of the current employee
SELECT
hire_date,
salary,
(
SELECT SUM(salary)
FROM employees e2
WHERE e2.hire_date BETWEEN e1.hire_date - 30 AND e1.hire_date
) AS pay_pattern
FROM employees e1
ORDER BY hire_date;