SQL JOIN
- Pronunciation
- ES-kyoo-EL JOYN or SEE-kwul JOYN
In short
A SQL JOIN is a query operation that combines rows from two or more tables into one result, matching them on related columns such as a foreign key.
What is a SQL JOIN?
A JOIN combines data that is spread across several tables into a single result. In a well-designed relational database, customers and their orders live in separate tables, and a JOIN matches each order to its customer by comparing a column in one table, such as orders.customer_id, with a column in the other, such as customers.id. The condition that decides which rows match is written after the ON keyword.
There are several kinds of JOIN. An INNER JOIN, which is what you get when you just write JOIN, returns only rows that have a match in both tables. A LEFT JOIN returns every row from the left table plus matching rows from the right, filling the gaps with NULL; RIGHT JOIN does the reverse, FULL OUTER JOIN keeps unmatched rows from both sides, and CROSS JOIN pairs every row of one table with every row of the other.
Picture two guest lists for a party: an inner join lists only the people who appear on both lists, while a left join lists everyone on the first list and notes who is also on the second. JOINs are also what make normalization practical, because data can be stored once, without duplication, and reassembled at query time.
A common mistake is using an inner join where a left join is needed, which silently drops rows, for example customers who have never ordered. Another is a missing or wrong join condition, which multiplies rows and inflates totals. JOIN is also different from UNION: a JOIN places columns from matching rows side by side, while UNION stacks the rows of two queries on top of each other.
At a glance
Key takeaways
- A JOIN combines rows from multiple tables based on a matching condition.
INNER JOINkeeps only rows that match in both tables.LEFT JOINkeeps every row from the left table, withNULLwhere nothing matches.- Joins usually follow foreign key relationships.
- Index the join columns to keep joins on large tables fast.
Example
-- INNER JOIN: only customers who have placed at least one order
SELECT c.name, o.id AS order_id, o.total
FROM customers AS c
INNER JOIN orders AS o ON o.customer_id = c.id;
-- LEFT JOIN: every customer, including those with no orders (count 0)
SELECT c.name, COUNT(o.id) AS order_count
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.id
GROUP BY c.id, c.name;Readers ask
What is the difference between INNER JOIN and LEFT JOIN?
An INNER JOIN returns only rows with a match in both tables. A LEFT JOIN returns all rows from the left table and fills the right table's columns with NULL when there is no match.
Are SQL JOINs slow?
Not when the join columns are indexed; databases are built to join millions of rows efficiently. Joins become slow when those columns lack indexes, when a query joins many large tables, or when a missing condition creates a huge number of row combinations.
What is a self join?
A self join joins a table to itself using two different aliases. It is useful for hierarchical data, such as an employees table where each row has a manager_id pointing to another employee.
See also
- SQLDatabases, p. 40SQL is the standard language for working with relational databases, used to create tables and to insert, query, update, and delete the data stored in them.
- Foreign KeyDatabases, p. 21A foreign key is a column in one database table that refers to the primary key of another table, linking related rows and keeping those references valid.
- Primary KeyDatabases, p. 36A primary key is a column, or set of columns, whose value uniquely identifies each row in a database table and can never be empty or duplicated.
- Database NormalizationDatabases, p. 9Database normalization is the process of organizing tables so each fact is stored only once, reducing duplicate data and preventing inconsistent updates.
- Database IndexDatabases, p. 7A database index is a data structure that helps a database find rows quickly without scanning a whole table, much like the index at the back of a book.
Spotted a mistake or something missing on this page?Suggest an edit