JournalSQLJoins5 min

The Difference Between AND and WHERE in Joins

I noticed that many people still don't understand the difference between using AND inside the ON clause versus using WHERE, so I wanted to simplify this for you here and give the topic the attention it deserves.

  • AND
  • ON clause
  • WHERE
  • outer join

Introduction

I noticed that many people still don't understand the difference between using AND inside the ON clause versus using WHERE, so I wanted to simplify this for you here and give the topic the attention it deserves. This is a question you might be asked in an interview, and even if you ask someone with 10 years of experience, they might not know how to answer it.

What Happens When I Use AND in the ON Clause?

For INNER JOIN

In general, what happens is that the ON clause is the basis of joining tables and executes immediately after we write:

...
JOIN table2

That means it executes before the WHERE clause checks its condition. Therefore, we are still in the joining phase between the two tables—while the tables are being joined, the AND condition is checked at the same time:

ON table1.pk = table2.fk
AND age > 28

For OUTER JOIN

Brief Overview of OUTER JOIN

If you don't know what OUTER JOIN is, it is another type of join between tables. There are two common types:

  1. LEFT JOIN: retrieves the matching rows between both tables along with all rows from the left table.
  2. RIGHT JOIN: retrieves the matching rows between both tables along with all rows from the right table.

If you want to visualize the difference more clearly with examples and diagrams, check out this topic: Visual Examples of SQL Joins

The Difference When Using OUTER JOIN with AND in the Join

When we use ON with an OUTER JOIN, the outer table is not filtered out by ON; every row from the outer table is returned, and any row that fails the ON condition will have all columns from the other table set to NULL.

The Difference When Using OUTER JOIN with WHERE in the Join

However, if we use WHERE after ON instead of AND, that means after the join completes—returning the matching rows plus the outer table's rows—the WHERE clause then filters the resulting table after the join happens, not during it.

I know it's hard to grasp just from words, so I will try to clarify with examples so you can understand it easily.

Example

A faculty member can belong to one department, while a department can have more than one faculty member.

Suppose we have a staff table for faculty members:

id  | name  | dep_id | salary
----|-------|--------|------------
1   | Hatem | 1      | 5000
2   | Fouad | 1      | 4500
3   | Tamer | 2      | 6000
4   | Amr   | 2      | 5000
5   | Dalia | NULL   | 34
6   | Amer  | NULL   | 457
7   | Kamel | 1      | 879
8   | Farid | 2      | 7878
9   | Amal  | 5      | 878
10  | Omar  | 5      | 345
11  | Walid | NULL   | NULL

And a departments table:

dep_id | dep_name
-------|------------------------
1      | Electronic Engineering
2      | Mechatronics Engineering
3      | Computer Engineering
4      | Civil Engineering
5      | Mechanical Engineering

First Query

If I ask you to get all staff members and the departments they belong to, you write:

SELECT *
FROM staff AS s
LEFT JOIN departments AS d
ON s.dep_id = d.id

The output will be:

id  | name  | dep_id | salary | dep_name
----|-------|--------|--------|--------
1   | Hatem | 1      | 5000   | Electronic Engineering
2   | Fouad | 1      | 4500   | Electronic Engineering
3   | Tamer | 2      | 6000   | Mechatronics Engineering
4   | Amr   | 2      | 5000   | Mechatronics Engineering
5   | Dalia | NULL   | 34     | NULL
6   | Amer  | NULL   | 457    | NULL
7   | Kamel | 1      | 879    | Electronic Engineering
8   | Farid | 2      | 7878   | Mechatronics Engineering
9   | Amal  | 5      | 878    | Mechanical Engineering
10  | Omar  | 5      | 345    | Mechanical Engineering
11  | Walid | NULL   | NULL   | NULL

As you can see, all rows from the staff table are present with their departments, and if a staff member doesn't have a department, dep_id and dep_name are NULL.

Second Query

If I ask you to get the staff members who are in Electronic Engineering or Mechanical Engineering (using AND in ON), you write:

SELECT *
FROM staff AS s
LEFT JOIN departments AS d
ON s.dep_id = d.id
AND (d.dep_name = 'Electronic Engineering'
      OR d.dep_name = 'Mechanical Engineering')

The output will be:

id  | name  | dep_id | salary | dep_name
----|-------|--------|--------|--------
1   | Hatem | 1      | 5000   | Electronic Engineering
2   | Fouad | 1      | 4500   | Electronic Engineering
3   | Tamer | NULL   | 6000   | NULL
4   | Amr   | NULL   | 5000   | NULL
5   | Dalia | NULL   | 34     | NULL
6   | Amer  | NULL   | 457    | NULL
7   | Kamel | 1      | 879    | Electronic Engineering
8   | Farid | NULL   | 7878   | NULL
9   | Amal  | 5      | 878    | Mechanical Engineering
10  | Omar  | 5      | 345    | Mechanical Engineering
11  | Walid | NULL   | NULL   | NULL

If you notice, the number of rows is the same as the first query, but the only departments shown are Electronic Engineering and Mechanical Engineering. Of course, that's because the filtering happened via ON, so these conditions became part of the join between the two tables.

Third Query

If I ask you to get the staff members who are in Electronic Engineering or Mechanical Engineering (the same idea as the second query, but without filtering using ON), I write:

SELECT *
FROM staff AS s
LEFT JOIN departments AS d
ON s.dep_id = d.id
WHERE d.dep_name = 'Electronic Engineering'
OR d.dep_name = 'Mechanical Engineering'

The expected output will be:

id  | name  | dep_id | salary | dep_name
----|-------|--------|--------|--------
1   | Hatem | 1      | 5000   | Electronic Engineering
2   | Fouad | 1      | 4500   | Electronic Engineering
7   | Kamel | 1      | 879    | Electronic Engineering
9   | Amal  | 5      | 878    | Mechanical Engineering
10  | Omar  | 5      | 345    | Mechanical Engineering

Because the filtering happened after joining the two tables.