Atterna
RUEN
Get access

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_idbuyer_idseller_idstatus
O009320B02860S032paid
O009319B02860S007paid
O009318B02368S008paid

Which column can be the primary key of orders?

Preparing the practice…

How we work it through

  1. 1

    Guess the key, then prove it with COUNT(*) against COUNT(DISTINCT …).

  2. 2

    Check it on every new extract; keys break silently.

  3. 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

An AI assistant

“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).”
Paste a real AI answer into the checker — it writes these questions for you →

Read next

Examples use Meridian, Atterna’s fictional practice marketplace.