Primary key
A column (or set of columns) whose value identifies exactly one row of a table and never repeats, such as order_id in an orders table.
FormulaCOUNT(*) = COUNT(DISTINCT key) — and it stays so
Try it · 30 seconds · from Atterna’s practice
| order_id | buyer_id | seller_id | status |
|---|---|---|---|
| O009320 | B02860 | S032 | paid |
| O009319 | B02860 | S007 | paid |
| O009318 | B02368 | S008 | paid |
Which column can be the primary key of orders?
Preparing the practice…
How we work it through
- 1
Guess the key, then prove it with COUNT(*) against COUNT(DISTINCT …).
- 2
Check it on every new extract; keys break silently.
- 3
Join on keys, or know exactly how the join will repeat.
On the practice dataset
The query we run on Meridian, Atterna’s practice marketplace, and what it returns.
SELECT COUNT(*),
COUNT(DISTINCT order_id),
COUNT(DISTINCT checkout_id)
FROM meridian.orders;Result9,320 · 9,320 · 7,138 — order_id is a key, checkout_id only looks like one
The common trap
Assuming a column is unique because it looks like an id. Check it: COUNT(*) against COUNT(DISTINCT id).
When an AI answers
“Each checkout_id is one order, so I joined payments on checkout_id.”
- What it didn’t know
- It assumed an id column is unique; one checkout splits into several seller orders.
- Ask back
- “Is checkout_id unique in orders? Compare COUNT(*) with COUNT(DISTINCT checkout_id).”
Read next
Examples use Meridian, Atterna’s fictional practice marketplace.