Bird
Raised Fist0
SQLquery~20 mins

Why grouping is needed in SQL - Challenge Your Understanding

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Grouping Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Purpose of GROUP BY in SQL
Why do we use the GROUP BY clause in SQL queries?
ATo filter rows based on a condition
BTo sort the rows in ascending order
CTo join two tables together
DTo combine rows that have the same values in specified columns into summary rows
Attempts:
2 left
💡 Hint
Think about how to summarize data by categories.
query_result
intermediate
2:00remaining
Output of GROUP BY with COUNT
Given the table Sales with columns Product and Quantity, what is the output of this query?
SELECT Product, COUNT(*) FROM Sales GROUP BY Product;
SQL
Sales table data:
Product | Quantity
--------|---------
Apple   | 10
Banana  | 5
Apple   | 7
Banana  | 3
Cherry  | 8
A[{"Product": "Apple", "COUNT(*)": 2}, {"Product": "Banana", "COUNT(*)": 2}, {"Product": "Cherry", "COUNT(*)": 1}]
B[{"Product": "Apple", "COUNT(*)": 17}, {"Product": "Banana", "COUNT(*)": 8}, {"Product": "Cherry", "COUNT(*)": 8}]
C[{"Product": "Apple", "COUNT(*)": 1}, {"Product": "Banana", "COUNT(*)": 1}, {"Product": "Cherry", "COUNT(*)": 1}]
D[{"Product": "Apple", "COUNT(*)": 3}, {"Product": "Banana", "COUNT(*)": 3}, {"Product": "Cherry", "COUNT(*)": 3}]
Attempts:
2 left
💡 Hint
COUNT(*) counts rows per group, not sum of Quantity.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in GROUP BY usage
Which option contains a syntax error in using GROUP BY?
SQL
Table: Employees (Name, Department, Salary)
ASELECT Department, AVG(Salary) FROM Employees GROUP BY Department;
BSELECT Department, COUNT(*) FROM Employees GROUP BY Department;
CSELECT Name, Department FROM Employees GROUP BY Department;
DSELECT Department, SUM(Salary) FROM Employees GROUP BY Department;
Attempts:
2 left
💡 Hint
Check if all selected columns are either grouped or aggregated.
optimization
advanced
2:00remaining
Optimizing GROUP BY queries with indexes
Which index will best improve performance of this query?
SELECT Department, COUNT(*) FROM Employees GROUP BY Department;
ACREATE INDEX idx_name ON Employees(Name);
BCREATE INDEX idx_department ON Employees(Department);
CCREATE INDEX idx_salary ON Employees(Salary);
DCREATE INDEX idx_department_salary ON Employees(Department, Salary);
Attempts:
2 left
💡 Hint
Think about which column is used in GROUP BY.
🔧 Debug
expert
2:30remaining
Why does this GROUP BY query fail?
Given the table Orders with columns OrderID, CustomerID, and OrderDate, why does this query fail?
SELECT CustomerID, OrderDate, COUNT(*) FROM Orders GROUP BY CustomerID;
ABecause OrderDate is neither in GROUP BY nor aggregated
BBecause CustomerID is not a valid column
CBecause COUNT(*) cannot be used with GROUP BY
DBecause GROUP BY requires ORDER BY clause
Attempts:
2 left
💡 Hint
Check if all selected columns are grouped or aggregated.

Practice

(1/5)
1. Why do we use GROUP BY in SQL queries?
easy
A. To organize rows into groups based on column values
B. To sort the results alphabetically
C. To delete duplicate rows from the table
D. To change the data type of a column

Solution

  1. Step 1: Understand the purpose of grouping

    Grouping organizes rows that share the same value in specified columns into sets.
  2. Step 2: Identify what GROUP BY does in SQL

    It collects rows into groups so aggregate functions like SUM or COUNT can be applied per group.
  3. Final Answer:

    To organize rows into groups based on column values -> Option A
  4. Quick Check:

    Grouping = Organizing rows by column values [OK]
Hint: Grouping collects rows by column values for summary [OK]
Common Mistakes:
  • Confusing grouping with sorting
  • Thinking grouping deletes duplicates
  • Assuming grouping changes data types
2. Which of the following is the correct syntax to group data by the column department?
easy
A. SELECT department, COUNT(*) FROM employees GROUP BY department;
B. SELECT department, COUNT(*) FROM employees ORDER BY department;
C. SELECT department, COUNT(*) FROM employees WHERE department;
D. SELECT department, COUNT(*) FROM employees HAVING department;

Solution

  1. Step 1: Identify the correct clause for grouping

    The GROUP BY clause groups rows by column values.
  2. Step 2: Check each option's syntax

    SELECT department, COUNT(*) FROM employees GROUP BY department; uses GROUP BY department, which is correct. Others use clauses for sorting, filtering, or incomplete syntax.
  3. Final Answer:

    SELECT department, COUNT(*) FROM employees GROUP BY department; -> Option A
  4. Quick Check:

    Correct grouping uses GROUP BY [OK]
Hint: Use GROUP BY to group, not ORDER BY or WHERE [OK]
Common Mistakes:
  • Using ORDER BY instead of GROUP BY
  • Using WHERE to filter groups
  • Using HAVING without aggregation
3. Given the table sales with columns region and amount, what will this query return?
SELECT region, SUM(amount) FROM sales GROUP BY region;
medium
A. Syntax error due to missing WHERE clause
B. List of all sales amounts without grouping
C. Sum of all sales amounts without region breakdown
D. Total sales amount for each region

Solution

  1. Step 1: Understand the query components

    The query groups rows by region and sums amount per group.
  2. Step 2: Predict the output

    It returns one row per region with the total sales amount for that region.
  3. Final Answer:

    Total sales amount for each region -> Option D
  4. Quick Check:

    GROUP BY region + SUM(amount) = total per region [OK]
Hint: GROUP BY + SUM = totals per group [OK]
Common Mistakes:
  • Thinking it sums all rows ignoring groups
  • Expecting a syntax error without WHERE
  • Confusing grouping with filtering
4. Identify the error in this query:
SELECT department, COUNT(employee_id) FROM employees;
medium
A. SELECT must include WHERE clause
B. Missing GROUP BY clause for department
C. COUNT cannot be used with employee_id
D. employee_id should be in HAVING clause

Solution

  1. Step 1: Analyze the SELECT and aggregation

    The query selects department and counts employee_id but lacks grouping.
  2. Step 2: Understand SQL rules for aggregation

    When using aggregate functions with other columns, those columns must be in GROUP BY.
  3. Final Answer:

    Missing GROUP BY clause for department -> Option B
  4. Quick Check:

    Aggregate + column needs GROUP BY [OK]
Hint: Use GROUP BY with non-aggregated columns [OK]
Common Mistakes:
  • Omitting GROUP BY with aggregates
  • Misusing HAVING for columns
  • Thinking COUNT can't use column names
5. You want to find the average salary per job title but only for job titles with more than 5 employees. Which query correctly uses grouping and filtering?
hard
A. SELECT job_title, AVG(salary) FROM employees GROUP BY job_title WHERE COUNT(*) > 5;
B. SELECT job_title, AVG(salary) FROM employees WHERE COUNT(*) > 5 GROUP BY job_title;
C. SELECT job_title, AVG(salary) FROM employees GROUP BY job_title HAVING COUNT(*) > 5;
D. SELECT job_title, AVG(salary) FROM employees HAVING COUNT(*) > 5 GROUP BY job_title;

Solution

  1. Step 1: Understand filtering groups with HAVING

    To filter groups after aggregation, use HAVING, not WHERE.
  2. Step 2: Check query order and clauses

    SELECT job_title, AVG(salary) FROM employees GROUP BY job_title HAVING COUNT(*) > 5; correctly groups by job_title and filters groups with more than 5 employees using HAVING.
  3. Final Answer:

    SELECT job_title, AVG(salary) FROM employees GROUP BY job_title HAVING COUNT(*) > 5; -> Option C
  4. Quick Check:

    Filter groups with HAVING after GROUP BY [OK]
Hint: Use HAVING to filter groups, not WHERE [OK]
Common Mistakes:
  • Using WHERE to filter aggregated groups
  • Placing HAVING before GROUP BY
  • Confusing clause order in SQL