0
0
PostgreSQLquery~20 mins

Why aggregation matters in PostgreSQL - Challenge Your Understanding

Choose your learning style9 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.