Relational SQL: INNER & LEFT Joins
Relational databases avoid duplicate data by linking tables. Joins query connected records by matching matching keys across distinct files.
1 Joining Tables
SQL — JOIN Examples
# INNER JOIN: Matches records that exist in BOTH tables
SELECT posts.id, posts.title, users.username
FROM posts
INNER JOIN users ON posts.user_id = users.id;
# LEFT JOIN: Returns ALL left-table records, plus matching right-table records (null if no match)
SELECT users.username, posts.title
FROM users
LEFT JOIN posts ON users.id = posts.user_id;
2 Code Challenge
Challenge: Write a query connecting an
orders table to a customers table using an inner join. Retrieve order date, order total, and customer email address fields.