Solve “Deduplicate events with ROW_NUMBER”

Free. Runs in your browser, no install.

SQL · Basics & Joins

What is NATURAL JOIN?

Mediumnatural joinexplicit join

Interview question

What is NATURAL JOIN?

How to answer

The short answer

NATURAL JOIN automatically joins tables using columns with the same names.

Example:

SQL
SELECT *
FROM customers
NATURAL JOIN orders;

The problem is that the join condition isn't explicitly written.

Suppose today both tables have:

Output
customer_id

and tomorrow someone adds another column with the same name, the behavior of the query could unexpectedly change.

That's why production SQL usually prefers explicit joins:

SQL
ON customers.customer_id = orders.customer_id

How to say it

> NATURAL JOIN is dangerous because schema changes can silently change the join condition.

Lock it in with a real drill

You remember an answer longer once you have used it. Try a hands-on problem on this concept.

Open related drill