SQL
Database Management
SQL JOIN
WHERE clause
ON clause

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.

Practice system design

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:

sql
SELECT Employees.Name, Employees.DepartmentID, Departments.DepartmentName 
FROM Employees 
JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;

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:

sql
1SELECT Employees.Name, Departments.DepartmentName 
2FROM Employees 
3JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID
4WHERE Departments.Location = 'New York';

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 ON clause can limit the rows that enter the join, while the WHERE clause can eliminate rows from the final output. Using the WHERE clause 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

sql
1-- Using ON Clause
2SELECT Employees.Name, Departments.DepartmentName 
3FROM Employees 
4LEFT JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
5
6-- Using WHERE Clause
7SELECT Employees.Name, Departments.DepartmentName 
8FROM Employees 
9LEFT JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID
10WHERE Employees.DepartmentID = Departments.DepartmentID;

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

FeatureON ClauseWHERE Clause
Usage ContextJoins (INNER and OUTER)Any SELECT, including after Joins
Impact on OUTER JOINSpecifies how to match rows, can result in NULLs in the outputCan filter out rows, potential to convert OUTER joins to INNER joins
Functional RoleStructural matching of rowsFiltering 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.