Inner, left, right and full outer joins
A join combines rows from two inputs using a relationship. Inner joins keep matches; outer joins also keep unmatched rows from a chosen side.
Understand the idea
Imagine matching a customer register to order slips. Customer 1 has two slips, so joining produces two rows for that customer. A join does not promise one output row per customer. First identify the grain of each input and how many matches a key can have.
| Concept | What it means |
|---|---|
| INNER JOIN | Return matching pairs only. |
| LEFT JOIN | Keep every left row, with NULL on the right when unmatched. |
| RIGHT JOIN | Keep every right row, with NULL on the left when unmatched. |
| FULL OUTER JOIN | Keep matches and unmatched rows from both inputs. |
A concrete example
Compare result groups: INNER has 2 rows, LEFT 4, RIGHT 3 and FULL 5. Order 103 has a NULL customer name in RIGHT and FULL. Asha appears twice because she has two orders.
Remember: Two matching order rows produce two pairs. Decide the intended output grain before treating repeated customer values as duplicates.
Explain it step by step · full theory
Let us understand the idea
Imagine matching a customer register to order slips. Customer 1 has two slips, so joining produces two rows for that customer. A join does not promise one output row per customer. First identify the grain of each input and how many matches a key can have.
Our sample has three customers and three orders. Asha owns orders 101 and 102. Ben and Chen have no orders. Order 103 refers to customer 4, who is missing from the register. This makes each join choice visible.
Reason through the SQL
With customers on the left, INNER returns two rows; LEFT returns four; RIGHT returns three; FULL returns five. NULL in an outer-join result can represent a missing match, not necessarily a NULL stored in the source.
Put a right-side filter in ON when you want to preserve left rows that lack a qualifying match. A later WHERE o.amount >= 50 removes NULL-extended rows and can defeat the reason for choosing LEFT JOIN. Count matches before aggregating money to avoid double counting.
Words used in this lesson
- INNER JOIN
- Return matching pairs only.
- LEFT JOIN
- Keep every left row, with NULL on the right when unmatched.
- RIGHT JOIN
- Keep every right row, with NULL on the left when unmatched.
- FULL OUTER JOIN
- Keep matches and unmatched rows from both inputs.
See how it works
Choose a step to explore the idea at your own pace.
Open the interactive explanation
Inner, left, right and full outer joins
Match Asha
Customer 1 matches orders 101 and 102, so there are two pairs.
Automatic playback is off for reduced motion. Select any step or use Previous and Next. Illustrations explain behavior; they are not live measurements.
Read the complete diagram explanation
- Match Asha
Customer 1 matches orders 101 and 102, so there are two pairs.
- Keep unmatched customers
LEFT keeps Ben and Chen with NULL order columns.
- Keep an unmatched order
RIGHT and FULL retain order 103 even though customer 4 is absent.
- Compare counts
The chosen preserved side determines which unmatched rows remain.
Try the SQL or command
Read → predict → run in your learning environment → compare. You can also learn by reading without an account.
New to SQL? Where to run it and how to read it
A query is a question written in SQL. Snowflake runs it and returns a result table. This website shows examples; it does not connect to your account.
- No account yet? Read and predict the output first. Follow the account and Snowsight lesson when you are ready.
- For SQL practice, use a learning account in Snowsight and select an authorized role and warehouse. A role determines permissions; a warehouse supplies processing power. Follow the example’s database/schema setup when it refers to stored objects.
- Read the entire example, including comments beginning with
--. Run the selected statement or labeled block in the stated order. Examples labeled Python, shell or HTTP belong in that client, not in a SQL editor. - Compare values and row counts with the explained result. If an example deliberately causes an error, follow its rollback and cleanup steps before continuing.
- SELECT / FROM
- Choose the values to return / identify their input.
- VALUES / COLUMN1
- VALUES supplies tiny example rows. COLUMN1 means their first column, COLUMN2 their second.
- AS / WITH
- AS gives a result a readable name. WITH names a temporary query step within one statement.
- NULL / ::
- NULL means missing or unknown. The :: notation converts a value to a named data type.
- Session
- One connection and its settings. Keep related transaction steps in the same session.
- Database.schema.table
- A full address for stored data. It is separate from the warehouse that processes queries.
Need the L2_OBJECTS_LAB setup? Open the first database-object lesson →
Help with common practice errors
- No active warehouse: select a warehouse your role can use. Do not assume you need to create a new one.
- Object missing or not authorized: check the full name, required setup and active role. Ask the account owner for access if needed.
- Object already exists: setup may already have run. Read the current state or choose a fresh lab name consistently; do not add OR REPLACE blindly.
- Different result: check whether an earlier INSERT or UPDATE changed the data. VALUES examples provide their own fresh input for each statement.
Before running this lesson
Read first, then run manually in an authorized Snowflake learning session with a warehouse. Each WITH/VALUES example includes its own inputs and creates no stored tables. Expected results are illustrative, not live execution.
Compare four joins using one result
WITH customers AS (
SELECT COLUMN1 AS CUSTOMER_ID, COLUMN2 AS NAME
FROM VALUES (1,'Asha'),(2,'Ben'),(3,'Chen')
), orders AS (
SELECT COLUMN1 AS ORDER_ID, COLUMN2 AS CUSTOMER_ID, COLUMN3 AS AMOUNT
FROM VALUES (101,1,40),(102,1,60),(103,4,25)
)
SELECT 'INNER' AS JOIN_KIND, c.CUSTOMER_ID AS CUSTOMER_ID, c.NAME, o.ORDER_ID
FROM customers c INNER JOIN orders o ON c.CUSTOMER_ID=o.CUSTOMER_ID
UNION ALL
SELECT 'LEFT', c.CUSTOMER_ID, c.NAME, o.ORDER_ID
FROM customers c LEFT JOIN orders o ON c.CUSTOMER_ID=o.CUSTOMER_ID
UNION ALL
SELECT 'RIGHT', c.CUSTOMER_ID, c.NAME, o.ORDER_ID
FROM customers c RIGHT JOIN orders o ON c.CUSTOMER_ID=o.CUSTOMER_ID
UNION ALL
SELECT 'FULL', c.CUSTOMER_ID, c.NAME, o.ORDER_ID
FROM customers c FULL OUTER JOIN orders o ON c.CUSTOMER_ID=o.CUSTOMER_ID
ORDER BY JOIN_KIND, CUSTOMER_ID NULLS LAST, ORDER_ID;What to expect and why
Compare result groups: INNER has 2 rows, LEFT 4, RIGHT 3 and FULL 5. Order 103 has a NULL customer name in RIGHT and FULL. Asha appears twice because she has two orders.
Preserve customers with no large orders
WITH customers AS (
SELECT COLUMN1 AS CUSTOMER_ID, COLUMN2 AS NAME
FROM VALUES (1,'Asha'),(2,'Ben'),(3,'Chen')
), orders AS (
SELECT COLUMN1 AS ORDER_ID, COLUMN2 AS CUSTOMER_ID, COLUMN3 AS AMOUNT
FROM VALUES (101,1,40),(102,1,60),(103,4,25)
)
SELECT c.NAME, o.ORDER_ID
FROM customers c LEFT JOIN orders o
ON c.CUSTOMER_ID=o.CUSTOMER_ID AND o.AMOUNT >= 50
ORDER BY c.CUSTOMER_ID;What to expect and why
The amount filter is part of the match condition. Ben and Chen remain even though neither has a qualifying order.
| NAME | ORDER_ID |
|---|---|
| Asha | 102 |
| Ben | NULL |
| Chen | NULL |
Check your understanding
Choose an answer, then check your reasoning. This is practice, not a scored test.
Try it yourself
Add one order for Ben and predict how INNER and LEFT row counts change.
Compare your reasoning
INNER grows from 2 to 3 rows. LEFT remains at 4: Ben’s NULL-extended row is replaced by one matching order row.