0
0
PostgreSQLquery~20 mins

DISTINCT ON for unique per group in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
DISTINCT ON Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this DISTINCT ON query?

Given the table employees with columns department, employee_id, and salary, what will this query return?

SELECT DISTINCT ON (department) department, employee_id, salary
FROM employees
ORDER BY department, salary DESC;
PostgreSQL
CREATE TABLE employees (department TEXT, employee_id INT, salary INT);
INSERT INTO employees VALUES
('Sales', 1, 5000),
('Sales', 2, 7000),
('HR', 3, 6000),
('HR', 4, 5500),
('IT', 5, 8000);

SELECT DISTINCT ON (department) department, employee_id, salary
FROM employees
ORDER BY department, salary DESC;
AReturns all employees ordered by department and salary descending
BReturns one row per department with the highest salary employee
CReturns one row per department with the lowest salary employee
DReturns an error because DISTINCT ON requires all ORDER BY columns in SELECT
Attempts:
2 left
💡 Hint

Remember that DISTINCT ON (column) returns the first row of each group based on the ORDER BY clause.

🧠 Conceptual
intermediate
1:30remaining
Why must the ORDER BY clause include the DISTINCT ON columns first?

In PostgreSQL, when using DISTINCT ON (column), why is it required that the ORDER BY clause starts with the same column(s) as in DISTINCT ON?

ABecause ORDER BY must always sort by all columns in the SELECT clause
BBecause ORDER BY and DISTINCT ON are unrelated and can be in any order
CBecause PostgreSQL uses the ORDER BY to determine which row to keep per group, and it must group by the DISTINCT ON columns first
DBecause DISTINCT ON automatically sorts the result ignoring ORDER BY
Attempts:
2 left
💡 Hint

Think about how PostgreSQL picks the first row per group.

📝 Syntax
advanced
2:00remaining
Which query correctly uses DISTINCT ON to get the earliest order per customer?

Given a table orders with columns customer_id, order_id, and order_date, which query returns the earliest order for each customer?

ASELECT DISTINCT ON (order_date) customer_id, order_id, order_date FROM orders ORDER BY customer_id, order_date ASC;
BSELECT DISTINCT ON (customer_id) customer_id, order_id, order_date FROM orders ORDER BY order_date ASC, customer_id;
CSELECT DISTINCT customer_id, order_id, order_date FROM orders ORDER BY customer_id, order_date ASC;
DSELECT DISTINCT ON (customer_id) customer_id, order_id, order_date FROM orders ORDER BY customer_id, order_date ASC;
Attempts:
2 left
💡 Hint

Remember the order of columns in ORDER BY must start with DISTINCT ON columns.

optimization
advanced
2:30remaining
How to optimize a DISTINCT ON query on a large table?

You have a large sales table with columns region, sale_id, and sale_date. You want to get the latest sale per region using DISTINCT ON. Which approach improves performance?

ACreate an index on (region, sale_date DESC) and run SELECT DISTINCT ON (region) region, sale_id, sale_date FROM sales ORDER BY region, sale_date DESC;
BCreate an index on (sale_date DESC) only and run the same query
CRun SELECT DISTINCT ON (region) region, sale_id, sale_date FROM sales ORDER BY sale_date DESC, region;
DUse GROUP BY region and MAX(sale_date) instead of DISTINCT ON
Attempts:
2 left
💡 Hint

Think about how indexes can help ORDER BY and DISTINCT ON.

🔧 Debug
expert
3:00remaining
Why does this DISTINCT ON query return unexpected rows?

Consider this query:

SELECT DISTINCT ON (category) category, product_id, price
FROM products
ORDER BY price DESC, category;

It is intended to return the most expensive product per category, but it returns unexpected results. What is the problem?

AORDER BY does not start with the DISTINCT ON column, so grouping is incorrect
BDISTINCT ON requires all columns in ORDER BY to be in SELECT
CDISTINCT ON cannot be used with ORDER BY on multiple columns
DThe query is missing a GROUP BY clause
Attempts:
2 left
💡 Hint

Check the order of columns in ORDER BY compared to DISTINCT ON.