Basic INNER JOIN Syntax
You'll learn: How to write the fundamental SQL statement that combines rows from two tables based on a matching condition.
The Core Idea
An INNER JOIN connects two tables by matching values in related columns. Think of it like merging two spreadsheets: one has customer IDs and names, another has customer IDs and orders. The INNER JOIN uses the customer ID to pair up the related information, showing only rows where a match exists in both tables.
The Standard Syntax
The basic structure looks like this:
SELECT columns
FROM table1
INNER JOIN table2
ON condition
Here's what each part does:
- SELECT columns: Choose which columns to display from either or both tables
- FROM table1: Your starting table (often called the "left" table)
- INNER JOIN table2: The table you're joining to (the "right" table)
- ON condition: The matching rule, usually comparing a column from each table
A Real Example
SELECT customers.name, orders.order_date, orders.amount
FROM customers
INNER JOIN orders
ON customers.customer_id = orders.customer_id
This query:
- Takes the
customerstable - Joins it with the
orderstable - Matches rows where
customer_idis the same in both - Returns only the name, order date, and amount for matched rows
Notice we use table.column notation to specify which table each column comes from—this prevents confusion when both tables have similarly named columns.
Key Takeaway: The INNER JOIN syntax follows a simple pattern: SELECT what you want, FROM the first table, INNER JOIN the second table ON a matching condition between them.