← Back to SQL, page 16

sql-0314 · Easy fresher

What does SELECT COUNT(*) FROM orders o JOIN order_items i ON i.order_id = o.id return compared with SELECT COUNT(*) FROM orders?

The join count is the number of order lines that match an order, 8 here, not the number of orders, 6. A one-to-many join repeats each order once per line. Use COUNT(DISTINCT o.id) to count orders after such a join, or count before joining.

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
SELECT (SELECT COUNT(*) FROM orders) AS orders, COUNT(*) AS joined_rows, COUNT(DISTINCT o.id) AS distinct_orders
FROM orders o JOIN order_items i ON i.order_id = o.id;
ordersjoined_rowsdistinct_orders
686

Orders 101 and 104 have two lines each, so they appear twice after the join. Every order has at least one line, so the distinct count matches the order count.

The same fan-out inflates SUMs of order-level columns, such as a shipping fee stored on the order. Aggregate lines per order first, then join, when mixing order-level and line-level values.

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);

SELECT (SELECT COUNT(*) FROM orders) AS orders, COUNT(*) AS joined_rows, COUNT(DISTINCT o.id) AS distinct_orders
FROM orders o JOIN order_items i ON i.order_id = o.id;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • One-to-many joins repeat the parent row
  • COUNT(*) after a join counts joined rows
  • COUNT(DISTINCT parent.id) counts parents
  • Aggregate before joining for parent-level totals

Common mistakes

  • Counting orders after joining to lines
  • Summing order-level fees after a line join

Follow-ups

  • How would you sum shipping fees correctly?
  • What if some orders had no lines?

Tips

  • Name the join cardinality