WHERE Clause Conditions with OUTER Joins

Issues that arise out of using Where clause in Outer Joins and how to avoid that.
0
(0)

One common mistake that people make while writing Outer Joins is incorrectly specifying a condition in the query. It’s such an innocent mistake that unless you have an idea of the expected result you won’t even realize that you made a mistake.

human computer blaming each other

Let me explain using the following two tables:

Table Name: Emp_Dept

 EmpID EmpNameDept
1SuriOps
2DivyaHR
3MayaHR
4RahulIT

Table Name: Emp_Loc

 EmpID StateCity
1UPKanpur
3RajasthanJaipur
4TelanganaHyderabad

Let’s assume that you are doing a Left Join (aka Left Outer Join) on tables Emp_Dept & Emp_Loc.

Select a.EmpId, a.EmpName, a.Dept, b.City
 From Emp_Dept a Left Join Emp_Loc b
 On a.EmpId=b.EmpId

Result:

 EmpID EmpNameDeptCity
1SuriOpsKanpur
2DivyaHR 
3MayaHRJaipur
4RahulITHyderabad

Now if you include a condition in your query using a WHERE clause that applies to the secondary table (i.e. the table on the RIGHT of a LEFT JOIN), the query will not act like an OUTER join, because you’ve limited the results with conditions on both tables. You’ve essentially overridden the OUTER JOIN by limiting the results to only records that exist in the secondary table, thus making it behave like an INNER join.

Confused?

Let’s understand this by applying a WHERE clause condition on the CITY column of Emp_Loc – which is on the RIGHT of a LEFT JOIN:

Select a.EmpId, a.EmpName, a.Dept, b.City
 From Emp_Dept a Left Join Emp_Loc b
 On a.EmpId=b.EmpId
 Where b.City='Jaipur'

In this case, we’ll get only the rows for which the given condition is true – making the query behave like an INNER join:

See also  7 Tips to Optimize SQL Queries
 EmpID EmpNameDeptCity
3MayaHRJaipur

If this is not how you want your output to be, you should use the condition in the Join clause itself, as shown below:

Select a.EmpId, a.EmpName, a.Dept, b.City
 From Emp_Dept a Left Join Emp_Loc b
 On a.EmpId=b.EmpId
 And b.City='Jaipur'

The result of the above query will be:

 EmpID EmpNameDeptCity
1SuriOps 
2DivyaHR 
3MayaHRJaipur
4RahulIT 

So, when you specify the condition using a Where clause, it applies the filter on the result of the entire query, whereas when you apply the condition in the Join clause, it applies the filter only on the Join.

A small concept, which if ignored, can inject huge errors in the data!

Rate the article

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this article.

I'm sorry you didn't like this article.

Could you please tell me how I can improve it?

I write about a mix of topics, including productivity, tech, books, personal finance, and more. If you’d like to stay updated, here are two ways:

  • Real-Time Alerts: Join my WhatsApp channel to get instant notifications for new articles, fascinating book excerpts, useful web finds, and more.
  • Monthly Email Digest: Subscribe to my Email Newsletter and receive a curated end-of-month roundup of everything I’ve written, along with handpicked gems from across the web.
See also  7 Tips to Optimize SQL Queries

I also create Google Sheets templates to automate and streamline workflows. You can check them out here. Feel free to reach out if you need a custom template made for you.

If you’ve enjoyed reading, please consider supporting the blog with any amount you like. Your contribution helps cover server and domain costs, ensuring the blog keeps running.

Photo Credit:

  1. Featured Image: Photo by Kevin Ku on Unsplash

One comment

  1. […] remember that use of WHERE with OUTER JOIN can lead to the incorrect results. Check this article for […]

Comments are closed.