0
0
PostgreSQLquery~20 mins

GROUP BY single and multiple columns in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
GROUP BY Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
GROUP BY with a single column

Consider a table sales with columns region and amount. What is the output of this query?

SELECT region, SUM(amount) FROM sales GROUP BY region ORDER BY region;
PostgreSQL
CREATE TABLE sales(region TEXT, amount INT);
INSERT INTO sales VALUES
('East', 100), ('West', 200), ('East', 150), ('North', 50), ('West', 100);
A[{"region": "East", "sum": 100}, {"region": "West", "sum": 200}, {"region": "North", "sum": 50}]
B[{"region": "East", "sum": 250}, {"region": "West", "sum": 300}]
C[{"region": "East", "sum": 250}, {"region": "North", "sum": 50}, {"region": "West", "sum": 300}]
D[{"region": "East", "sum": 250}, {"region": "North", "sum": 50}, {"region": "West", "sum": 200}]
Attempts:
2 left
💡 Hint

GROUP BY groups rows by the unique values in the region column, then SUM adds amounts per group.

query_result
intermediate
2:00remaining
GROUP BY with multiple columns

Given a table orders with columns customer_id, product_id, and quantity, what is the output of this query?

SELECT customer_id, product_id, SUM(quantity) FROM orders GROUP BY customer_id, product_id ORDER BY customer_id, product_id;
PostgreSQL
CREATE TABLE orders(customer_id INT, product_id INT, quantity INT);
INSERT INTO orders VALUES
(1, 101, 2), (1, 102, 3), (2, 101, 1), (1, 101, 4), (2, 103, 5);
A[{"customer_id": 1, "product_id": 101, "sum": 6}, {"customer_id": 1, "product_id": 102, "sum": 3}, {"customer_id": 2, "product_id": 101, "sum": 1}, {"customer_id": 2, "product_id": 103, "sum": 5}]
B[{"customer_id": 1, "product_id": 101, "sum": 6}, {"customer_id": 1, "product_id": 102, "sum": 3}, {"customer_id": 2, "product_id": 101, "sum": 1}]
C[{"customer_id": 1, "product_id": 101, "sum": 4}, {"customer_id": 1, "product_id": 102, "sum": 3}, {"customer_id": 2, "product_id": 101, "sum": 1}, {"customer_id": 2, "product_id": 103, "sum": 5}]
D[{"customer_id": 1, "product_id": 101, "sum": 6}, {"customer_id": 1, "product_id": 102, "sum": 3}, {"customer_id": 2, "product_id": 103, "sum": 5}]
Attempts:
2 left
💡 Hint

GROUP BY with two columns groups rows by unique pairs of customer_id and product_id.

📝 Syntax
advanced
2:00remaining
GROUP BY with aggregate and non-aggregated columns

Which of the following queries will cause a syntax error in PostgreSQL?

SELECT department, employee_name, COUNT(*) FROM employees GROUP BY department;
PostgreSQL
CREATE TABLE employees(department TEXT, employee_name TEXT);
AThe query runs but returns NULL for employee_name.
BThe query runs correctly and returns counts per department with employee names.
CThe query runs but returns random employee_name values per department.
DThe query will cause a syntax error because employee_name is not in GROUP BY or aggregated.
Attempts:
2 left
💡 Hint

In PostgreSQL, all selected columns must be either aggregated or included in GROUP BY.

optimization
advanced
2:00remaining
Optimizing GROUP BY with multiple columns

You have a large table transactions with columns user_id, category, and amount. Which index will best optimize this query?

SELECT user_id, category, SUM(amount) FROM transactions GROUP BY user_id, category;
ACREATE INDEX idx_category_user ON transactions(category, user_id);
BCREATE INDEX idx_user_category ON transactions(user_id, category);
CCREATE INDEX idx_amount ON transactions(amount);
DCREATE INDEX idx_user ON transactions(user_id);
Attempts:
2 left
💡 Hint

Indexes that match the GROUP BY columns in order help aggregation queries.

🧠 Conceptual
expert
3:00remaining
Understanding GROUP BY with NULL values

Given a table products with columns category and price, what will this query return?

SELECT category, COUNT(*) FROM products GROUP BY category ORDER BY category;

Assume some rows have category as NULL.

PostgreSQL
CREATE TABLE products(category TEXT, price INT);
INSERT INTO products VALUES
('Books', 10), (NULL, 20), ('Books', 15), (NULL, 30), ('Games', 25);
A[{"category": null, "count": 2}, {"category": "Books", "count": 2}, {"category": "Games", "count": 1}]
B[{"category": "Books", "count": 2}, {"category": "Games", "count": 1}]
C[{"category": "Books", "count": 2}, {"category": "Games", "count": 1}, {"category": null, "count": 0}]
D[{"category": null, "count": 0}, {"category": "Books", "count": 2}, {"category": "Games", "count": 1}]
Attempts:
2 left
💡 Hint

GROUP BY treats NULL as a distinct group.