SQL GROUP BY Clause ๐ฆ
Mentor's Note: A simple
AVGgives you the average salary for the whole company. But what if you want the average salary for each department separately?GROUP BYis the tool that puts data into "Buckets" before calculating. ๐ก
๐ The Scenario: The Fruit Basket ๐งบโ
Imagine a basket of fruits: Apples, Bananas, and Oranges.
- Step 1 (Grouping): You separate them into three piles (The buckets).
- Step 2 (Aggregate): You count how many are in each pile.
- Result: "3 Apples, 5 Bananas, 2 Oranges." โ
๐จ Visual Logic: The Bucketing Processโ
๐ป 1. The Basic Syntaxโ
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE condition
GROUP BY column_name;
Example: Salary by Departmentโ
-- Scenario: Find the total budget for each department
SELECT dept_name, SUM(salary) AS total_budget
FROM employees
GROUP BY dept_name;
๐ป 2. Grouping by Multiple Columns ๐ชโ
You can create "sub-buckets".
-- Scenario: Find headcount per City within each Department
SELECT dept_name, city, COUNT(*) AS emp_count
FROM employees
GROUP BY dept_name, city;
โ ๏ธ The Golden Rule of Groupingโ
Every column in your SELECT list that is NOT part of an aggregate function MUST be in the GROUP BY clause.
-- โ WRONG (Will cause an error)
SELECT name, dept_name, AVG(salary) FROM employees GROUP BY dept_name;
-- โ
CORRECT
SELECT dept_name, AVG(salary) FROM employees GROUP BY dept_name;
๐ก Interview Tip ๐โ
"Interviewers love asking about the difference between
GROUP BYandORDER BY. Answer:GROUP BYcombines rows to summarize them.ORDER BYjust changes the display order of the rows!"