← Back to SQL, page 14

sql-0262 · Easy fresher

For each customer, return the date and id of their first order.

Rank each customer's orders by date with ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date, id) and keep row 1. MIN(order_date) per customer gives the date but not the id, and joining back on the date can return two rows if a customer ordered twice that day.

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_idfirst_orderorder_date
11012026-01-05
21032026-02-14
31042026-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_idfirst_date
12026-01-05
22026-02-14
32026-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_idfirst_dateid
12026-01-05101
22026-02-14103
32026-03-01104

It is SQLite-specific, and PostgreSQL rejects it:

sql
SELECT customer_id, MIN(order_date), id FROM orders GROUP BY customer_id;  -- error on purpose
text
Error: column "orders.id" must appear in the GROUP BY clause or be used in an aggregate function
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);

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