🧠 Introduction
SQL’s power lies in its ability to aggregate and summarize
data. The GROUP BY clause is the go-to tool for performing aggregate operations
like totals, averages, counts, and more — especially when grouped by specific
attributes such as department, date, region, or customer.
Interviewers often ask:
“Find departments with more than 2 employees.”
“List customers who made more than $10,000 in purchases.”
“Find products with an average rating above 4.”
All these rely on GROUP BY and its companion filter: HAVING.
In this chapter, you’ll learn:
🗃️ Sample Table: Orders
🧾 orders
|
order_id |
customer_id |
product |
quantity |
amount |
order_date |
|
1 |
101 |
Mouse |
2 |
500 |
2024-01-01 |
|
2 |
102 |
Keyboard |
1 |
700 |
2024-01-02 |
|
3 |
101 |
Laptop |
1 |
30000 |
2024-01-02 |
|
4 |
103 |
Monitor |
1 |
15000 |
2024-01-03 |
|
5 |
101 |
Mouse |
1 |
250 |
2024-01-04 |
|
6 |
104 |
Keyboard |
3 |
2100 |
2024-01-04 |
|
7 |
102 |
Mouse |
2 |
500 |
2024-01-05 |
🔍 Problem 1: Total Amount
Spent by Each Customer
SELECT
customer_id, SUM(amount) AS total_spent
FROM
orders
GROUP
BY customer_id;
✅ Output:
|
customer_id |
total_spent |
|
101 |
30750 |
|
102 |
1200 |
|
103 |
15000 |
|
104 |
2100 |
We’re summarizing (SUM) by grouping on customer_id.
❓ Problem 2: Customers Who Spent
More Than 10,000
SELECT
customer_id, SUM(amount) AS total_spent
FROM
orders
GROUP
BY customer_id
HAVING
SUM(amount) > 10000;
✅ Output:
|
customer_id |
total_spent |
|
101 |
30750 |
|
103 |
15000 |
Use HAVING to filter grouped results.
🔥
HAVING works after aggregation; WHERE works before.
💡 WHERE vs HAVING
|
Clause |
Used For |
Applied When |
|
WHERE |
Filtering rows |
Before aggregation |
|
HAVING |
Filtering groups |
After aggregation |
Example:
--
Only orders > 1000
SELECT
customer_id, SUM(amount)
FROM
orders
WHERE
amount > 1000
GROUP
BY customer_id;
🔄 Multiple Aggregates
Example
Show total quantity and amount for each product.
SELECT
product,
SUM(quantity) AS total_qty,
SUM(amount) AS total_sales
FROM
orders
GROUP
BY product;
⚙️ Using GROUP BY with Multiple
Columns
Show total sales per customer per product.
SELECT
customer_id, product, SUM(amount) AS total_sales
FROM
orders
GROUP
BY customer_id, product;
🎯 Real-World Use Cases
🧑💻
Interview Challenge
Find all products that have been ordered more than twice.
SELECT
product, COUNT(*) AS order_count
FROM
orders
GROUP
BY product
HAVING
COUNT(*) > 2;
✅ Output:
|
product |
order_count |
|
Mouse |
3 |
📊 Combining with ORDER BY
and LIMIT
Top 2 customers by total spending
SELECT
customer_id, SUM(amount) AS total_spent
FROM
orders
GROUP
BY customer_id
ORDER
BY total_spent DESC
LIMIT
2;
🧠 Common Mistakes
|
Mistake |
Why it’s wrong |
|
Using WHERE with aggregate functions |
Aggregates can’t be used in WHERE |
|
Forgetting GROUP BY when using SUM, AVG, etc. |
SQL will throw an error |
|
Mixing aggregated and non-aggregated columns |
Only columns in GROUP BY or aggregates can be in SELECT |
⚡ Performance Tips
✅ Summary
Interviewers often ask about joins, groupings, window functions, subqueries, and ranking problems. These test both your understanding of SQL syntax and logic.
Focus on solving real-world problems, practice on platforms like LeetCode or HackerRank, and understand how SQL is used in business scenarios.
Understanding logic is more important. Interviewers prefer candidates who can reason through a problem over those who rely on memorized solutions.
Yes. Functions like ROW_NUMBER(), RANK(), and DENSE_RANK() often appear in intermediate to advanced interviews.
Use real datasets, explore open SQL challenges online, and build your own problems from business scenarios.
Absolutely. Using aliases and CTEs can make your queries cleaner and more readable, which is often appreciated in interviews.
Learn to use IS NULL, COALESCE(), and proper filtering logic to handle NULLs, as they can affect groupings and joins.
Yes. Inner joins, left joins, and subqueries are very common in interviews, especially when querying across multiple tables.
Yes. Explaining both brute-force and optimized approaches shows depth in understanding and problem-solving.
With consistent practice, most people can get proficient in 2–4 weeks, especially if they focus on real interview problems.
Tutorials are for educational purposes only, with no guarantees of comprehensiveness or error-free content; TuteeHUB disclaims liability for outcomes from reliance on the materials, recommending verification with official sources for critical applications.
Kindly log in to use this feature. We’ll take you to the login page automatically.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
Comments(0)