SQL interview question: Each customer's first order

medium
ROW_NUMBER
top-N per group

For each customer who has ordered, return their name, the date of their first order and its amount. If a customer placed two orders on their first day, return the one with the lower order id.

Expected columns: name, first_order_date, amount. Column names don't have to match. Runs on SQLite in your browser.

Tables: customers + orders

CREATE TABLE customers (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  signup_date TEXT NOT NULL
);
INSERT INTO customers VALUES
  (1, 'Acme', '2025-12-01'), (2, 'Globex', '2025-12-15'), (3, 'Initech', '2026-01-10'),
  (4, 'Umbrella', '2026-02-01'), (5, 'Hooli', '2026-03-05');

CREATE TABLE orders (
  id INTEGER PRIMARY KEY,
  customer_id INTEGER NOT NULL REFERENCES customers(id),
  order_date TEXT NOT NULL,
  amount REAL NOT NULL
);
INSERT INTO orders VALUES
  (1, 1, '2026-01-05', 120), (2, 2, '2026-01-09', 80), (3, 1, '2026-01-20', 200),
  (4, 3, '2026-02-02', 150), (5, 1, '2026-02-14', 90), (6, 2, '2026-02-14', 60),
  (7, 3, '2026-03-01', 300), (8, 2, '2026-03-18', 75), (9, 1, '2026-03-30', 110),
  (10, 3, '2026-04-04', 220);

Your query

Ctrl/Cmd + Enter

FAQ