SQL GROUP BY Clause π¦ΒΆ
Prerequisites: Aggregate Functions
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ΒΆ
graph TD
A[Raw Data] --> B{GROUP BY City}
B -- "Surat" --> C[Bucket A]
B -- "Mumbai" --> D[Bucket B]
C --> E[AVG Salary: 50k]
D --> F[AVG Salary: 65k]
π» 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!"