Full answer
sql
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
country TEXT
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id),
order_date TEXT NOT NULL,
status TEXT NOT NULL
);
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders(id),
product TEXT NOT NULL,
qty INTEGER NOT NULL,
unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
(101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
(103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
(105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
(101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
(104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);sql
WITH r AS (
SELECT customer_id, id, order_date,
ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date, id) AS rn
FROM orders WHERE customer_id IS NOT NULL
)
SELECT customer_id, id AS first_order, order_date FROM r WHERE rn = 1 ORDER BY customer_id;| customer_id | first_order | order_date |
|---|---|---|
| 1 | 101 | 2026-01-05 |
| 2 | 103 | 2026-02-14 |
| 3 | 104 | 2026-03-01 |
The id tie-breaker makes the choice deterministic when two orders share a date. The guest order with no customer is excluded.
If only the date is needed, a plain aggregate is simplest:
sql
SELECT customer_id, MIN(order_date) AS first_date FROM orders WHERE customer_id IS NOT NULL GROUP BY customer_id ORDER BY customer_id;| customer_id | first_date |
|---|---|
| 1 | 2026-01-05 |
| 2 | 2026-02-14 |
| 3 | 2026-03-01 |
SQLite also has a documented shortcut: in a query with a single MIN or MAX aggregate, bare columns come from the row that held the minimum, so the id comes from each customer's earliest order:
sql
SELECT customer_id, MIN(order_date) AS first_date, id FROM orders WHERE customer_id IS NOT NULL GROUP BY customer_id ORDER BY customer_id;| customer_id | first_date | id |
|---|---|---|
| 1 | 2026-01-05 | 101 |
| 2 | 2026-02-14 | 103 |
| 3 | 2026-03-01 | 104 |
It is SQLite-specific, and PostgreSQL rejects it:
sql
SELECT customer_id, MIN(order_date), id FROM orders GROUP BY customer_id; -- error on purposetext
Error: column "orders.id" must appear in the GROUP BY clause or be used in an aggregate functionsql
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
country TEXT
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id),
order_date TEXT NOT NULL,
status TEXT NOT NULL
);
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders(id),
product TEXT NOT NULL,
qty INTEGER NOT NULL,
unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
(101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
(103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
(105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
(101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
(104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);
WITH r AS (
SELECT customer_id, id, order_date,
ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date, id) AS rn
FROM orders WHERE customer_id IS NOT NULL
)
SELECT customer_id, id AS first_order, order_date FROM r WHERE rn = 1 ORDER BY customer_id;
SELECT customer_id, MIN(order_date) AS first_date FROM orders WHERE customer_id IS NOT NULL GROUP BY customer_id ORDER BY customer_id;
SELECT customer_id, MIN(order_date) AS first_date, id FROM orders WHERE customer_id IS NOT NULL GROUP BY customer_id ORDER BY customer_id;Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.
Key points
- ROW_NUMBER per customer ordered by date, keep row 1
- Add a tie-breaker for same-day orders
- MIN gives the date but not the row
- SQLite's bare-column MIN rule is not portable
Common mistakes
- Joining back on MIN(date) and getting duplicates
- Relying on SQLite's bare columns in portable code
Follow-ups
- How would you get each customer's latest order instead?
- How would you get the first order's total amount?
Tips
- Mention the tie-breaker