JOIN with WHERE Clause
beginnerjoins
2 min read
What is JOIN with WHERE?
After joining tables, you often need to filter the combined result. The WHERE clause filters rows after the join is performed, while the ON clause defines how tables relate.
Syntax
SELECT columns
FROM table1
JOIN table2 ON table1.key = table2.key
WHERE condition;When to Use
- Filtering joined data by a specific value
- Restricting results to a date range after joining
- Combining relationship data with business filters
Key Points
- ON vs WHERE — ON defines the join relationship. WHERE filters the result after joining.
- INNER JOIN — For inner joins, conditions in ON and WHERE produce the same result, but ON is semantically clearer for the join condition.
- OUTER JOIN Difference — For LEFT/RIGHT joins, WHERE filters remove rows (converting them to inner joins). Use ON for conditions you want to be optional.
- Performance — Place selective filters in WHERE; the optimizer may push them down.
- Readability — Keep join conditions in ON and row filters in WHERE for clarity.
Guided Practice
Solve the challenge below. Use hints when stuck and check your answer for instant feedback.
Practice challengeGuided learning mode
JOIN with WHERE Clause Challenge
Write a query that find enrollments for students with grade A.
Expected result
Course names for students who have grade A.
Hidden checks
- Returned rows and values
- Output columns and result shape
- Final database state after the query runs
Lesson guidance
What is JOIN with WHERE?
Initializing database...Each run starts from fresh sample data.
More Examples
Join and filter by price range
Find orders for products costing more than 50.
Initializing database...Each run starts from fresh sample data.
Frequently Asked Questions
Should I put the filter in ON or WHERE?
For INNER JOIN, it does not matter. For OUTER JOIN, put the filter in ON if you want to keep non-matching rows, or in WHERE if you want to remove them.
Does WHERE run before or after JOIN?
Logically, JOIN runs first and WHERE filters the result. The optimizer may reorder operations for efficiency.