SQL JOIN what is the difference between WHERE clause and ON clause?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
SQL JOINs are a cornerstone of database operations, allowing data to be combined from two or more tables based on a related column between them. The use of JOINs in SQL queries enables a robust mechanism to extract wide-ranging data that is distributed across various tables. Two critical SQL clauses used in the context of JOINs are the WHERE and ON clauses. Understanding the difference between these two clauses is key to mastering SQL queries.
ON Clause
The ON clause is used specifically with JOIN operations to specify the conditions that dictate how tables should be joined. It fundamentally informs the database management system (DBMS) on how to match rows between the tables.
Example of the ON clause:
In this example, the ON clause is used for matching rows in the Employees table with rows in the Departments table where the DepartmentID is the same.
WHERE Clause
The WHERE clause is a filter that is applied to the results of the SQL query; it restricts which rows are selected based on the specified conditions. The WHERE clause can filter rows before or after the JOIN is processed, depending on whether it's used in conjunction with the JOIN.
Example of the WHERE clause used with JOIN:
In this case, the WHERE clause filters out the departments not located in 'New York' after the tables have been joined based on the DepartmentID.
Differences between ON and WHERE Clauses
The key distinction lies in their functional use cases:
- ON Clause: It is primarily used to specify the join conditions, defining how table rows relate to each other from a structural standpoint.
- WHERE Clause: It is used to filter records from the combined table resulting from join operations based on specified conditions.
Behavioral Differences in Joins
The impact of WHERE and ON clauses becomes very evident in OUTER JOINs:
- LEFT OUTER JOIN: The
ONclause can limit the rows that enter the join, while theWHEREclause can eliminate rows from the final output. Using theWHEREclause to specify the join condition in a LEFT OUTER JOIN can unintentionally convert it into an INNER JOIN by filtering out all rows where the join condition is false or NULL in the second table.
Example: LEFT OUTER JOIN with ON and WHERE Clauses
The first query using the ON clause will include all employees and match department names where possible. The second query, while superficially similar, effectively acts as an INNER JOIN because the WHERE clause filters out all employees who do not have a matching DepartmentID in the Departments table.
Comparison Table
| Feature | ON Clause | WHERE Clause |
| Usage Context | Joins (INNER and OUTER) | Any SELECT, including after Joins |
| Impact on OUTER JOIN | Specifies how to match rows, can result in NULLs in the output | Can filter out rows, potential to convert OUTER joins to INNER joins |
| Functional Role | Structural matching of rows | Filtering of rows based on conditions |
Conclusion
While both ON and WHERE clauses play pivotal roles in SQL, their correct use is crucial for achieving the desired query results, especially in complex database operations involving multiple tables and different types of JOINs. Understanding these differences enhances the ability to manipulate and retrieve data efficiently and accurately.
Related reading

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.