Python MySQL Join
In this page:
cursor.execute(
"SELECT a.column, b.column FROM table_a AS a "
"INNER JOIN table_b AS b ON a.key = b.key"
)
Basic INNER JOIN
cursor.execute("SELECT users.name, products.name FROM users INNER JOIN products ON users.fav = products.id") combines matching rows from the users and products tables wherever a user's favorite (fav) column equals a product's id -- users with no matching product are excluded entirely from the result.
Example: Basic INNER JOIN
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute(
"SELECT users.name, products.name FROM users "
"INNER JOIN products ON users.fav = products.id" # only rows with a match in both tables are included
)
print("Only users with a matching favorite product are included")
LEFT JOIN: Including Unmatched Rows
LEFT JOIN returns every row from the left (first-named) table regardless of whether it has a match in the right table -- when there is no match, the columns from the right table simply come back as NULL, which is useful for finding, for example, every customer including ones who have never placed an order.
Example: LEFT JOIN: Including Unmatched Rows
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute(
"SELECT customers.name, orders.id FROM customers "
"LEFT JOIN orders ON customers.id = orders.customer_id" # every customer included, even with no matching order
)
print("Customers with no orders still appear, with NULL order id")
Joining Three or More Tables
Multiple JOIN clauses can be chained together in a single query to combine data from three or more related tables -- for instance linking orders to customers, and separately linking each order to the products it contains, all in one statement.
Example: Joining Three or More Tables
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute(
"SELECT c.name, o.id, p.name FROM customers c "
"JOIN orders o ON c.id = o.customer_id " # first join links customers to orders
"JOIN products p ON o.product_id = p.id" # second join links orders to products
)
print("Three tables combined with short aliases")
Avoiding Ambiguous Column Errors
When two joined tables share a column name (both having an "id" column, for instance), referencing that name alone in the SELECT list or WHERE clause is ambiguous and raises an error -- qualifying it with the table name (or alias), like customers.id, resolves the ambiguity.
Example: Avoiding Ambiguous Column Errors
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute(
"SELECT customers.id, orders.id FROM customers "
"JOIN orders ON customers.id = orders.customer_id" # table names qualify the ambiguous "id" column
)
print("Qualifying 'id' with a table name avoids ambiguity")
Joins with WHERE, ORDER BY, and LIMIT
A JOIN combines naturally with every other SELECT clause already covered -- WHERE to filter the joined result, ORDER BY to sort it, and LIMIT to cap how many combined rows come back -- applied in that same logical order: join, then filter, then sort, then limit.
Example: Joins with WHERE, ORDER BY, and LIMIT
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute(
"SELECT c.name, o.total FROM customers c JOIN orders o ON c.id = o.customer_id "
"WHERE o.total > %s ORDER BY o.total DESC LIMIT 5", # join, then filter, then sort, then limit
(100,),
)
print("Join, then filter, then sort, then limit")
- Forgetting the ON clause that specifies how the two tables relate, which either causes a syntax error or (with certain join forms) an unintended cross join matching every row against every other row.
- Confusing INNER JOIN (only rows with a match in both tables) with LEFT JOIN (all rows from the left table, with NULLs filling in where there is no match on the right).
- Not qualifying a column name with its table when the same column name exists in both joined tables, causing an ambiguous column reference error.
- JOIN combines rows from two tables based on a related column between them, specified in an ON clause.
- INNER JOIN returns only rows that have a match in both tables.
- LEFT JOIN returns all rows from the left table, with NULL filled in for any columns from the right table that has no match.
JOIN syntax is standard SQL, executed identically across all mysql-connector-python versions.
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first:
- Python MySQL Get Started
- Python MySQL Create Database
- Python MySQL Create Table
- Python MySQL Insert Into Table
- Python MySQL Select From Table
- Python MySQL Select With a Filter
- Python MySQL Order By
- Python MySQL Delete Record
- Python MySQL Drop Table
- Python MySQL Update Table
- Python MySQL Limit
- Python MySQL Join