Complete the code to select unique users by their country using DISTINCT ON.
SELECT DISTINCT ON ([1]) user_id, country FROM users ORDER BY country, user_id;The DISTINCT ON (country) clause selects the first row for each unique country.
Complete the code to order users by signup date within each country.
SELECT DISTINCT ON (country) user_id, country, signup_date FROM users ORDER BY country, [1] DESC;Ordering by signup_date DESC ensures the newest user per country is selected.
Fix the error in the query to correctly select the first order per customer.
SELECT DISTINCT ON (customer_id) order_id, customer_id, order_date FROM orders ORDER BY [1];The ORDER BY must start with the same column(s) as DISTINCT ON, here customer_id.
Fill both blanks to select the earliest order per customer sorted by order date.
SELECT DISTINCT ON ([1]) order_id, [2], order_date FROM orders ORDER BY customer_id, order_date;
DISTINCT ON (customer_id) groups by customer, and selecting order_id shows the order details.
Fill all three blanks to select the latest login per user with their email.
SELECT DISTINCT ON ([1]) [2], email, login_time FROM logins ORDER BY [3] DESC;
DISTINCT ON groups by user_id, selecting email and ordering by login_time DESC picks the latest login per user.