Data Grouping and Aggregation
Aggregate Functions
Section titled “Aggregate Functions”SELECT COUNT(*) FROM employees;SELECT AVG(salary) FROM employees;SELECT SUM(salary) FROM employees;SELECT MIN(salary), MAX(salary) FROM employees;GROUP BY
Section titled “GROUP BY”SELECT department, COUNT(*) as employee_countFROM employeesGROUP BY department;
SELECT department, AVG(salary) as avg_salaryFROM employeesGROUP BY department;HAVING Clause
Section titled “HAVING Clause”SELECT department, COUNT(*) as employee_countFROM employeesGROUP BY departmentHAVING COUNT(*) > 5;Multiple Grouping
Section titled “Multiple Grouping”SELECT department, job_title, COUNT(*) as countFROM employeesGROUP BY department, job_titleORDER BY department, job_title;Common Patterns
Section titled “Common Patterns”-- Department statisticsSELECT department, COUNT(*) as total_employees, AVG(salary) as avg_salary, MIN(salary) as min_salary, MAX(salary) as max_salaryFROM employeesGROUP BY departmentORDER BY avg_salary DESC;