Bird
Raised Fist0
PostgreSQLquery~20 mins

Why aggregation matters in PostgreSQL - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Aggregation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
What is the total sales amount for each product?

Given a sales table with columns product_id and amount, which query correctly calculates the total sales amount per product?

PostgreSQL
SELECT product_id, SUM(amount) FROM sales GROUP BY product_id;
ASELECT product_id, COUNT(amount) FROM sales;
BSELECT product_id, SUM(amount) FROM sales GROUP BY product_id;
CSELECT product_id, amount FROM sales;
DSELECT product_id, SUM(amount) FROM sales;
Attempts:
2 left
💡 Hint

Think about how to group rows to get totals per product.

🧠 Conceptual
intermediate
1:30remaining
Why use aggregation functions in databases?

Which statement best explains why aggregation functions like SUM, COUNT, and AVG are important in databases?

AThey allow combining multiple rows into a single summary value, helping to analyze data efficiently.
BThey create new tables automatically based on conditions.
CThey speed up data entry by automating inserts.
DThey delete duplicate rows from tables.
Attempts:
2 left
💡 Hint

Think about what aggregation functions do with many rows of data.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in this aggregation query

What error does this query produce?

SELECT department, AVG(salary) FROM employees WHERE department = 'Sales';
ARuntimeError: invalid column name 'salary'
BSyntaxError: missing GROUP BY clause when using aggregation with non-aggregated columns
CNo error, query runs correctly
DSyntaxError: WHERE clause cannot be used with aggregation functions
Attempts:
2 left
💡 Hint

Check if all non-aggregated columns are grouped.

❓ optimization
advanced
2:30remaining
Optimize aggregation query for large datasets

You have a large orders table with millions of rows. Which approach improves performance when calculating total sales per region?

ARemove the GROUP BY clause to speed up the query
BRun the query without any indexes or optimizations
CUse SELECT * instead of specifying columns
DCreate an index on the <code>region</code> column before running <code>SELECT region, SUM(amount) FROM orders GROUP BY region;</code>
Attempts:
2 left
💡 Hint

Indexes help queries filter and group data faster.

🔧 Debug
expert
3:00remaining
Why does this aggregation query return fewer rows than expected?

Given the query:

SELECT category, COUNT(*) FROM products GROUP BY category HAVING COUNT(*) > 5;

Why might some categories be missing from the result?

ABecause the HAVING clause filters out categories with 5 or fewer products, so only categories with more than 5 appear.
BBecause GROUP BY removes duplicate categories entirely.
CBecause COUNT(*) counts only non-null categories, so null categories are excluded.
DBecause the query syntax is invalid and returns no rows.
Attempts:
2 left
💡 Hint

Consider what HAVING does after grouping.

Practice

(1/5)
1. What is the main purpose of aggregation functions like COUNT or SUM in SQL?
easy
A. To summarize multiple rows into a single value
B. To delete duplicate rows from a table
C. To join two tables together
D. To create new tables from existing ones

Solution

  1. Step 1: Understand aggregation functions

    Aggregation functions like COUNT and SUM take many rows and produce one summary value.
  2. Step 2: Identify their purpose

    They help to reduce data by summarizing, not by deleting or joining tables.
  3. Final Answer:

    To summarize multiple rows into a single value -> Option A
  4. Quick Check:

    Aggregation = Summary [OK]
Hint: Aggregation means summarizing many rows into one value [OK]
Common Mistakes:
  • Thinking aggregation deletes rows
  • Confusing aggregation with JOIN
  • Believing aggregation creates new tables
2. Which of the following is the correct syntax to count rows in a table named sales?
easy
A. SELECT COUNT ALL sales;
B. SELECT COUNT(*) FROM sales;
C. SELECT COUNT(sales) FROM *;
D. SELECT COUNT FROM sales();

Solution

  1. Step 1: Recall correct COUNT syntax

    The correct syntax to count all rows is SELECT COUNT(*) FROM table_name;.
  2. Step 2: Check each option

    SELECT COUNT(*) FROM sales; matches the correct syntax. Others have syntax errors or wrong order.
  3. Final Answer:

    SELECT COUNT(*) FROM sales; -> Option B
  4. Quick Check:

    COUNT(*) syntax = SELECT COUNT(*) FROM sales; [OK]
Hint: COUNT(*) counts all rows, use parentheses and star [OK]
Common Mistakes:
  • Omitting parentheses after COUNT
  • Using COUNT without * or column
  • Wrong order of keywords
3. Given the table orders with columns customer_id and amount, what does this query return?
SELECT customer_id, SUM(amount) FROM orders GROUP BY customer_id;
medium
A. Total amount spent by each customer
B. List of all orders with amounts
C. Total amount spent by all customers combined
D. Number of orders per customer

Solution

  1. Step 1: Understand GROUP BY usage

    GROUP BY customer_id groups rows by each customer.
  2. Step 2: Understand SUM(amount)

    SUM(amount) adds all amounts per customer group.
  3. Final Answer:

    Total amount spent by each customer -> Option A
  4. Quick Check:

    GROUP BY + SUM = total per group [OK]
Hint: GROUP BY groups rows; SUM adds values per group [OK]
Common Mistakes:
  • Thinking SUM returns total for all rows without grouping
  • Confusing SUM with COUNT
  • Ignoring GROUP BY effect
4. What is wrong with this query?
SELECT customer_id, COUNT(*) FROM orders;
medium
A. customer_id must be inside COUNT()
B. COUNT(*) cannot be used without WHERE
C. Missing GROUP BY clause for customer_id
D. SELECT must have ORDER BY with COUNT

Solution

  1. Step 1: Check aggregation with non-aggregated column

    customer_id is selected but not aggregated or grouped.
  2. Step 2: Identify missing GROUP BY

    To use COUNT(*) with customer_id, GROUP BY customer_id is required.
  3. Final Answer:

    Missing GROUP BY clause for customer_id -> Option C
  4. Quick Check:

    Non-aggregated columns need GROUP BY [OK]
Hint: Use GROUP BY when mixing columns with aggregation [OK]
Common Mistakes:
  • Forgetting GROUP BY when selecting non-aggregated columns
  • Thinking COUNT needs WHERE clause
  • Assuming ORDER BY is mandatory with aggregation
5. You want to find the average order amount per customer but only for customers who placed more than 3 orders. Which query correctly achieves this?
hard
A. SELECT customer_id, AVG(amount) FROM orders WHERE COUNT(*) > 3 GROUP BY customer_id;
B. SELECT customer_id, AVG(amount) FROM orders HAVING COUNT(*) > 3 GROUP BY customer_id;
C. SELECT customer_id, AVG(amount) FROM orders GROUP BY customer_id WHERE COUNT(*) > 3;
D. SELECT customer_id, AVG(amount) FROM orders GROUP BY customer_id HAVING COUNT(*) > 3;

Solution

  1. Step 1: Understand filtering groups with HAVING

    HAVING filters groups after aggregation; WHERE filters rows before aggregation.
  2. Step 2: Check query order and clauses

    SELECT customer_id, AVG(amount) FROM orders GROUP BY customer_id HAVING COUNT(*) > 3; correctly uses GROUP BY then HAVING COUNT(*) > 3 to filter customers with more than 3 orders.
  3. Final Answer:

    SELECT customer_id, AVG(amount) FROM orders GROUP BY customer_id HAVING COUNT(*) > 3; -> Option D
  4. Quick Check:

    Use HAVING to filter groups after GROUP BY [OK]
Hint: Use HAVING to filter groups, not WHERE [OK]
Common Mistakes:
  • Using WHERE to filter aggregated counts
  • Placing HAVING before GROUP BY
  • Syntax errors mixing WHERE and HAVING
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard