Agrupaments. GROUP BY
Contingut
- 1 Referència
- 2 Teoria
Referència
Teoria
SELECT column1, column2, AGGREGATE_FUNCTION (column3) FROM table1 GROUP BY column1, column2;
SELECT department_id, COUNT(employee_id) headcount FROM employees GROUP BY department_id;
SQL GROUP BY with INNER JOIN example
SELECT e.department_id, department_name, COUNT(employee_id) headcount FROM employees e INNER JOIN departments d ON d.department_id = e.department_id GROUP BY e.department_id;
SQL GROUP BY with ORDER BY example
SELECT
e.department_id,
department_name,
COUNT(employee_id) headcount
FROM
employees e
INNER JOIN
departments d ON d.department_id = e.department_id
GROUP BY e.department_id
ORDER BY headcount DESC;
SQL GROUP BY with HAVING example
SELECT
e.department_id,
department_name,
COUNT(employee_id) headcount
FROM
employees e
INNER JOIN
departments d ON d.department_id = e.department_id
GROUP BY e.department_id
HAVING headcount > 5
ORDER BY headcount DESC;
SQL GROUP BY with MIN, MAX, and AVG example
SELECT
e.department_id,
department_name,
MIN(salary) min_salary,
MAX(salary) max_salary,
ROUND(AVG(salary), 2) average_salary
FROM
employees e
INNER JOIN
departments d ON d.department_id = e.department_id
GROUP BY e.department_id;
SQL GROUP BY with SUM function example
SELECT
e.department_id,
department_name,
SUM(salary) total_salary
FROM
employees e
INNER JOIN
departments d ON d.department_id = e.department_id
GROUP BY e.department_id;
SQL GROUP BY multiple columns
SELECT
e.department_id,
department_name,
e.job_id,
job_title,
COUNT(employee_id)
FROM
employees e
INNER JOIN
departments d ON d.department_id = e.department_id
INNER JOIN
jobs j ON j.job_id = e.job_id
GROUP BY e.department_id , e.job_id;
SQL GROUP BY and DISTINCT
SELECT DISTINCT phone_number FROM employees;
Having
La clàusula HAVING permet especificar una condició per als agrupaments que fem sobre la condició GROUP BY. No l'hem de confondre amb un WHERE.
Per trobar els managers que tinguin com a mínim 5 subordinats, hem d'afegir la clàusula HAVING:
SELECT
manager_id,
first_name,
last_name,
COUNT(employee_id) direct_reports
FROM
employees
WHERE
manager_id IS NOT NULL
GROUP BY manager_id
HAVING direct_reports >= 5;
Per calcular la suma total dels salaris que la companyia paga a cada departament, i que aquest total estigui entre 20000 i 30000:
SELECT
department_id, SUM(salary)
FROM
employees
GROUP BY department_id
HAVING SUM(salary) BETWEEN 20000 AND 30000
ORDER BY SUM(salary);
Per trobar els departaments que tenen empleats amb el salari més petit de qualsevol empleat més gran que 10000:
SELECT e.department_id, department_name, MIN(salary) FROM employees e INNER JOIN departments d ON d.department_id = e.department_id GROUP BY e.department_id HAVING MIN(salary) >= 10000 ORDER BY MIN(salary);
Per trobar els departments que tenen com a salari mig dels seus empleats entre 5000 i 7000:
SELECT e.department_id, department_name, ROUND(AVG(salary), 2) FROM employees e INNER JOIN departments d ON d.department_id = e.department_id GROUP BY e.department_id HAVING AVG(salary) BETWEEN 5000 AND 7000 ORDER BY AVG(salary);
Having vs WHERE. No és el mateix
creat per Joan Quintana Compte, gener 2022