Question & tables Return customer_id and first_order (earliest ordered_at), ordered by customer_id.
30:00
Start / resume mock Question Finish and review Database schema customers(id, name, country)
orders(id, customer_id, ordered_at, amount, status)
employees(id, name, department, salary, manager_id)
events(id, user_id, event_date, event_type)
products(id, name, category, price, stock) orders.customer_id → customers.id employees.manager_id → employees.id events.user_id is a separate product-user identifier.
All data is synthetic. Dates use YYYY-MM-DD; unknown values are NULL.
Concept hint A scalar subquery returns one value, such as an average. EXISTS checks for a match without multiplying the outer row. NOT EXISTS is useful for absence; NOT IN can surprise you when its list contains NULL.
Reference solution & explanation WITH firsts AS(SELECT customer_id,MIN(ordered_at) AS first_order FROM orders GROUP BY customer_id) SELECT customer_id,first_order FROM firsts ORDER BY customer_id;WITH names an intermediate query. Use names describing the grain, such as customer_totals. CTEs improve readability; they do not promise faster execution. The database chooses an execution plan.
The checker compares output columns, values and requested row order across two fixture variants. Different SQL that produces the same results is accepted. This is a learning aid, not a proof of correctness on every possible dataset.
Read the topic guide → Your SQL query Ctrl / Cmd + Enter to run Run query Check answer Stop Reset query Save draft Download SQL
Loading the SQL engine…
Queries run locally, with an eight-second execution limit and a 500-row result limit. Signed-in learners can save exercise queries to their academy account. Practice results never change course grades or certificates.
This workspace needs JavaScript and WebAssembly. You can still read the question and reference solution.