Bird
Raised Fist0
SQLquery~20 mins

How GROUP BY changes query execution in SQL - Practice Exercises

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
🎖️
GROUP BY Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of GROUP BY with aggregate function
Given the table Sales with columns Region and Amount, what is the output of this query?
SELECT Region, SUM(Amount) FROM Sales GROUP BY Region;
SQL
CREATE TABLE Sales (Region VARCHAR(10), Amount INT);
INSERT INTO Sales VALUES ('East', 100), ('West', 200), ('East', 150), ('West', 50);
ASyntaxError
B[{"Region": "East", "SUM(Amount)": 100}, {"Region": "West", "SUM(Amount)": 200}, {"Region": "East", "SUM(Amount)": 150}, {"Region": "West", "SUM(Amount)": 50}]
C[{"Region": "East", "SUM(Amount)": 250}, {"Region": "West", "SUM(Amount)": 250}]
D[{"Region": null, "SUM(Amount)": 500}]
Attempts:
2 left
💡 Hint
GROUP BY groups rows by the column values before applying aggregate functions.
🧠 Conceptual
intermediate
1:30remaining
Effect of GROUP BY on non-aggregated columns
What happens if you select a column in a query with GROUP BY that is not included in the GROUP BY clause or an aggregate function?
AThe query returns the first value of that column for each group.
BThe query returns an error because all selected columns must be grouped or aggregated.
CThe query returns the last value of that column for each group.
DThe query ignores the column and returns only grouped columns.
Attempts:
2 left
💡 Hint
Think about SQL rules for grouping and selecting columns.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in GROUP BY usage
Which option contains a syntax error in the GROUP BY clause?
SQL
Table: Employees (Department VARCHAR, Salary INT)
ASELECT Department, Salary FROM Employees GROUP BY Department;
BSELECT Department, AVG(Salary) FROM Employees GROUP BY Department;
CSELECT Department, COUNT(*) FROM Employees GROUP BY Department;
DSELECT Department, MAX(Salary) FROM Employees GROUP BY Department;
Attempts:
2 left
💡 Hint
Check if all selected columns are grouped or aggregated.
optimization
advanced
1:30remaining
How GROUP BY affects query performance
Which statement best explains how adding a GROUP BY clause affects query execution?
AGROUP BY has no effect on query execution time.
BGROUP BY always makes queries faster by reducing the number of rows returned.
CGROUP BY eliminates the need for indexes on the grouped columns.
DGROUP BY causes the database to sort or hash rows to group them, which can increase execution time.
Attempts:
2 left
💡 Hint
Think about what the database must do to group rows.
🔧 Debug
expert
2:30remaining
Diagnose the cause of unexpected GROUP BY results
A query:
SELECT Department, COUNT(EmployeeID) FROM Employees GROUP BY Department;

returns lower counts than expected. Which issue is most likely causing this?
ACOUNT(EmployeeID) counts only non-NULL EmployeeID, so some rows are excluded.
BSome Department values are NULL, causing those rows to be grouped together.
CThe GROUP BY clause is missing a column, causing rows to merge incorrectly.
DThe query is missing a WHERE clause to filter rows.
Attempts:
2 left
💡 Hint
Consider how COUNT behaves with NULL values.

Practice

(1/5)
1. What does the GROUP BY clause do in an SQL query?
easy
A. It filters rows based on a condition.
B. It groups rows that have the same values in specified columns.
C. It deletes duplicate rows from the result.
D. It sorts the rows in ascending order.

Solution

  1. Step 1: Understand the purpose of GROUP BY

    The GROUP BY clause collects rows with the same values in specified columns into groups.
  2. Step 2: Compare with other SQL clauses

    Sorting is done by ORDER BY, filtering by WHERE, and removing duplicates by DISTINCT, not GROUP BY.
  3. Final Answer:

    It groups rows that have the same values in specified columns. -> Option B
  4. Quick Check:

    GROUP BY groups rows by column values [OK]
Hint: GROUP BY groups rows by column values, not sorting or filtering [OK]
Common Mistakes:
  • Confusing GROUP BY with ORDER BY
  • Thinking GROUP BY filters rows
  • Assuming GROUP BY removes duplicates
2. Which of the following is the correct syntax to group rows by the column department?
easy
A. SELECT department, COUNT(*) FROM employees WHERE department;
B. SELECT department, COUNT(*) FROM employees ORDER BY department;
C. SELECT department, COUNT(*) FROM employees GROUP BY department;
D. SELECT department, COUNT(*) FROM employees HAVING department;

Solution

  1. Step 1: Identify correct GROUP BY usage

    The GROUP BY clause must follow the FROM clause and specify the column to group by, here 'department'.
  2. Step 2: Check each option's syntax

    SELECT department, COUNT(*) FROM employees GROUP BY department; uses GROUP BY correctly. SELECT department, COUNT(*) FROM employees ORDER BY department; uses ORDER BY which sorts, not groups. SELECT department, COUNT(*) FROM employees WHERE department; uses WHERE incorrectly. SELECT department, COUNT(*) FROM employees HAVING department; uses HAVING without GROUP BY, which is invalid.
  3. Final Answer:

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

    GROUP BY syntax: SELECT ... FROM ... GROUP BY column [OK]
Hint: GROUP BY follows FROM and lists columns to group by [OK]
Common Mistakes:
  • Using ORDER BY instead of GROUP BY
  • Using WHERE to group rows
  • Using HAVING without GROUP BY
3. Given the table sales with columns region and amount, what is the output of this query?
SELECT region, SUM(amount) FROM sales GROUP BY region;
medium
A. A list of regions with the total sales amount per region.
B. A list of all sales amounts without grouping.
C. An error because SUM() cannot be used with GROUP BY.
D. A list of regions sorted by amount.

Solution

  1. Step 1: Understand GROUP BY with aggregate functions

    The query groups rows by 'region' and calculates the sum of 'amount' for each group.
  2. Step 2: Analyze the output

    The result shows each region once with the total sales amount summed from all rows in that region.
  3. Final Answer:

    A list of regions with the total sales amount per region. -> Option A
  4. Quick Check:

    GROUP BY + SUM() = totals per group [OK]
Hint: GROUP BY with SUM() gives totals per group [OK]
Common Mistakes:
  • Expecting all rows without grouping
  • Thinking SUM() causes error with GROUP BY
  • Confusing grouping with sorting
4. What is wrong with this query?
SELECT department, employee_name, COUNT(*) FROM employees GROUP BY department;
medium
A. You cannot select employee_name without grouping by it or using an aggregate function.
B. COUNT(*) cannot be used with GROUP BY.
C. GROUP BY must come before SELECT.
D. The query is correct and will run without errors.

Solution

  1. Step 1: Check columns in SELECT with GROUP BY

    When using GROUP BY on 'department', all selected columns must be grouped or aggregated.
  2. Step 2: Identify the error

    'employee_name' is neither grouped nor aggregated, causing a syntax error.
  3. Final Answer:

    You cannot select employee_name without grouping by it or using an aggregate function. -> Option A
  4. Quick Check:

    Non-grouped columns must be aggregated [OK]
Hint: All selected columns must be grouped or aggregated [OK]
Common Mistakes:
  • Selecting non-grouped columns without aggregation
  • Thinking COUNT(*) is invalid with GROUP BY
  • Misplacing GROUP BY clause
5. You want to find departments with more than 5 employees. Which query correctly uses GROUP BY and HAVING to achieve this?
hard
A. SELECT department, COUNT(*) FROM employees GROUP BY department WHERE COUNT(*) > 5;
B. SELECT department, COUNT(*) FROM employees HAVING COUNT(*) > 5 GROUP BY department;
C. SELECT department, COUNT(*) FROM employees WHERE COUNT(*) > 5 GROUP BY department;
D. SELECT department, COUNT(*) FROM employees GROUP BY department HAVING COUNT(*) > 5;

Solution

  1. Step 1: Understand HAVING clause usage

    HAVING filters groups after aggregation, so it must come after GROUP BY.
  2. Step 2: Check query order and syntax

    SELECT department, COUNT(*) FROM employees GROUP BY department HAVING COUNT(*) > 5; correctly places HAVING after GROUP BY with condition COUNT(*) > 5. Other options misuse HAVING or WHERE clauses.
  3. Final Answer:

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

    HAVING filters groups after GROUP BY [OK]
Hint: Use HAVING after GROUP BY to filter groups [OK]
Common Mistakes:
  • Using WHERE to filter aggregated results
  • Placing HAVING before GROUP BY
  • Confusing WHERE and HAVING clauses