SQL JOIN
A SQL JOIN is a clause that combines rows from two or more tables of a relational database into a single result by pairing the rows whose column values satisfy a join condition.
Joins are how SQL reassembles data that a normalized schema splits across tables. The condition usually matches a foreign key in one table to the primary key it references in another, such as linking each order to the customer who placed it. The ON clause accepts any Boolean expression as the condition.
Two shorthands cover the common case of equality on shared column names. USING lists the columns that both tables name the same way, and NATURAL applies that equality to every shared column name automatically.
The join type decides what happens to rows that find no partner:
INNER JOIN: Keeps only the pairs of rows that satisfy the condition.LEFT OUTER JOIN: Keeps every row from the left table and fills the right table’s columns withNULLwhere no match exists.RIGHT OUTER JOIN: Mirrors the left join, keeping every row from the right table.FULL OUTER JOIN: Keeps every row from both tables, pairing the matches and padding the missing side withNULLfor rows without a partner.CROSS JOIN: Pairs every row of one table with every row of the other, producing a Cartesian product.
Pick a join type below to see which rows of a customers table and an orders table make it into the result, and where NULL fills in for a missing partner:
Where a filter goes matters for outer joins. A condition in ON decides which rows count as a match during the join, while one in WHERE filters the joined result afterward. A WHERE test on the right table’s columns can therefore discard the NULL-padded rows that a left join was meant to keep. A table can also be joined to itself, a pattern called a self-join.
Because SQL is declarative, a query names the tables and the condition but not the algorithm. The query planner picks a strategy such as a nested loop join, a hash join that builds a hash table from one input, or a merge join over inputs sorted on the join key. Python programs usually send join queries through the DB-API, for example with the sqlite3 module.
Related Resources
Tutorial
Data Management With Python, SQLite, and SQLAlchemy
Store and retrieve data with Python, SQLite, and SQLAlchemy. You'll compare flat files with SQL and work with your data as Python objects.
For additional information on related topics, take a look at the following resources:
- Introduction to Python SQL Libraries (Tutorial)
- Python and MySQL Database: A Practical Introduction (Tutorial)
- Combining Data in pandas With merge(), .join(), and concat() (Tutorial)
- Understanding CRUD Operations in SQL (Course)
- SQLite and SQLAlchemy in Python: Move Your Data Beyond Flat Files (Course)
- Data Management With Python, SQLite, and SQLAlchemy (Quiz)
- Introduction to Python SQL Libraries (Quiz)
- MySQL Databases and Python (Course)
- Combining Data in pandas With concat() and merge() (Course)
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Martin Breuss • Updated Nov. 18, 2026