SQLFOMULAS
SQLFOMULAS
FROM employees;
FROM employees;
SELECT *
FROM employees
SELECT *
FROM employees
WHERE coffeeshop_id = 1;
-- Select all the employees who work in Common Grounds and make more than 50k
SELECT *
FROM employees
-- Select all the employees who work in Common Grounds or make more than 50k
SELECT *
FROM employees
-- Select all the employees who work in Common Grounds, make more than 50k and are male
SELECT *
FROM employees
WHERE
AND coffeeshop_id = 1
-- Select all the employees who work in Common Grounds or make more than 50k or are male
SELECT *
FROM employees
WHERE
OR coffeeshop_id = 1
OR gender = 'M';
-- Select all rows from the suppliers table where the supplier is Beans and Barley
SELECT *
FROM suppliers
-- 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
SELECT *
FROM suppliers
SELECT *
FROM suppliers
WHERE
coffee_type = 'Robusta'
OR coffee_type = 'Arabica';
SELECT
employee_id,
first_name,
last_name,
salary
FROM employees
ORDER BY salary;
employee_id,
first_name,
last_name,
salary
FROM employees
SELECT
employee_id,
first_name,
last_name,
salary
FROM employees
LIMIT 10;
FROM employees;
FROM locations;
-- Renaming columns
SELECT
email,
email AS email_address,
hire_date,
hire_date AS date_joined,
salary,
salary AS pay
FROM employees;
SELECT
hire_date as date,
FROM employees;
SELECT
first_name,
UPPER(first_name) AS first_name_upper,
last_name,
UPPER(last_name) AS last_name_upper
FROM employees;
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_no_spaces,
SELECT
first_name,
last_name,
FROM employees;
SELECT
FROM employees;
-- Boolean expressios
-- if the person makes less than 50k, then true, otherwise false
SELECT
salary,
-- if the person is a female and makes less than 50k, then true, otherwise false
SELECT
salary,
FROM employees
LIMIT 15;
SELECT
email,
FROM employees;
SELECT
email,
FROM employees;
select
email as gov_email
from employees
-- SUBSTRING
SELECT
email,
FROM employees;
-- POSITION
SELECT
email,
POSITION('@' IN email)
FROM employees;
SELECT
email,
FROM employees;
SELECT
email,
FROM employees;
email,
FROM employees;
FROM employees;
FROM employees;
FROM employees;
SELECT AVG(salary)
FROM employees;
SELECT ROUND(AVG(salary),2)
FROM employees;
SELECT SUM(salary)
FROM employees;
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,
round(avg(salary), 0) as average_sal,
sum(salary) as total_sal,
count(*) as num_of_emp
FROM employees;
SELECT
coffeeshop_id,
COUNT(employee_id)
FROM employees
GROUP BY coffeeshop_id;
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
-- 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"
-- 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
SELECT
email,
SUBSTRING(email FROM 5) --refiere al conteo de de cada una de las palabras que tiene el
correo
FROM employees;
-- POSITION
email,
FROM employees;
SELECT
email,
FROM employees;
FROM(
SELECT
employee_id,
salary,
CASE
END as pay_category
FROM employees
)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,
FROM employees;
INNER JOINT
SELECT
s.coffeeshop_name,
l.city,
l.country
FROM
shops s
JOIN locations l
ON s.city_id = l.city_id;
SELECT
s.coffeeshop_name,
l.city,
l.country
FROM
shops s
ON s.city_id = l.city_id;
-- Subqueries
-- Basic subqueries with subqueries in the FROM clause
SELECT *
FROM (
SELECT *
FROM employees
) as a;
SELECT
a.employee_id,
a.first_name,
a.last_name
FROM (
SELECT *
FROM employees
) a;
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;
SELECT *
FROM shops
SELECT city_id
FROM locations
);
SELECT *
FROM employees
SELECT coffeeshop_id
FROM shops
SELECT city_id
FROM locations
);
-- Return all employees who make over 35k and work in US coffee shops
SELECT *
FROM employees
SELECT coffeeshop_id
FROM shops
SELECT city_id
FROM locations
);
-- 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
) AS pay_pattern
FROM employees e1
ORDER BY hire_date;