Skip to content
We areTOP 1%

What is the difference between an equi join, a natural join, a theta join and a self join?

MediumConceptSQL: Joins
Track your progress
First attempt
Re-solve after a few days
Final pass before interviews

Answer

These names describe the join condition, not how unmatched rows are handled.

  • Theta join: joins on any comparison (=, <, >, <> and so on), such as o.amount BETWEEN b.low AND b.high to place each order in a price band.
  • Equi join: a theta join that uses only equality, such as e.dept_id = d.id. Both join columns appear in the result.
  • Natural join: an equi join on all columns that have the same name in both tables, with each such column shown once. It is risky, because adding an unrelated column with a matching name silently changes the result.
  • Self join: a table joined with itself through two aliases, used for hierarchies or for comparing rows.

Example: emp(id, name, manager_id)

id | name | manager_id
1  | Rao  | NULL
2  | Asha | 1
3  | Ravi | 1
SELECT e.name, m.name AS manager
FROM emp e JOIN emp m ON e.manager_id = m.id;
-- Asha | Rao, Ravi | Rao   (a self join that is also an equi join)

More in SQL: Joins

SQL: Joins questions
#ProblemPlatformDifficultyOpen1st1st Visit2nd2nd VisitRevRevise
1What are the different types of joins in SQL?
EasyConcept
ConceptEasy Read the answer to What are the different types of joins in SQL?
2What is the difference between UNION and UNION ALL, and how is UNION different from a JOIN?
EasyConcept
ConceptEasy Read the answer to What is the difference between UNION and UNION ALL, and how is UNION different from a JOIN?
3Combine Two TablesLeetCodeEasy Solve Combine Two Tables on LeetCode (opens in a new tab)
4Employees Earning More Than Their ManagersLeetCodeEasy Solve Employees Earning More Than Their Managers on LeetCode (opens in a new tab)
5Customer Who Visited but Did Not Make Any TransactionsLeetCodeEasy Solve Customer Who Visited but Did Not Make Any Transactions on LeetCode (opens in a new tab)
6Students and ExaminationsLeetCodeEasy Solve Students and Examinations on LeetCode (opens in a new tab)
7What is the difference between an equi join, a natural join, a theta join and a self join?
MediumConcept
ConceptMedium Read the answer to What is the difference between an equi join, a natural join, a theta join and a self join?
8What is a correlated subquery, and when would you use a join instead of a subquery?
MediumConcept
ConceptMedium Read the answer to What is a correlated subquery, and when would you use a join instead of a subquery?
9Managers with at Least 5 Direct ReportsLeetCodeMedium Solve Managers with at Least 5 Direct Reports on LeetCode (opens in a new tab)

Sign in to track your progress

Your checkboxes, streak and activity heatmap are saved to your account and sync across devices. It is free.
Sign in with email

New here? Create an account