Handling NULL in join condition

In SQL, the behavior of joins involving NULL values in the join conditions depends on the type of join used. Here’s a breakdown:

1. INNER JOIN

Behavior: Rows with NULL values in the join condition are excluded from the result.

Explanation: This is because NULL = NULL evaluates to FALSE, and an inner join only includes rows where the join condition evaluates to TRUE.

Example:

SELECT * FROM TableA INNER JOIN TableB ON TableA.column1 = TableB.column2;

If TableA.column1 or TableB.column2 contains NULL, those rows will not be included.

2. LEFT JOIN (or LEFT OUTER JOIN)

Behavior: Rows from the left table are included in the result regardless of whether the join condition evaluates to TRUE or contains NULL. If the join condition fails due to a NULL in the right table, the result will include NULL values for columns from the right table.

Example:

SELECT * FROM TableA LEFT JOIN TableB ON TableA.column1 = TableB.column2; 

If TableB.column2 contains NULL, rows from TableA will still be included, with NULL values for TableB columns.

3. RIGHT JOIN (or RIGHT OUTER JOIN)

Behavior: Similar to the LEFT JOIN, but ensures all rows from the right table are included, even if the join condition involves NULL.

Example:

SELECT * FROM TableA RIGHT JOIN TableB ON TableA.column1 = TableB.column2; 

If TableA.column1 contains NULL, rows from TableB will still be included, with NULL values for TableA columns

4. FULL OUTER JOIN

Behavior: Combines the behaviors of LEFT JOIN and RIGHT JOIN. Rows from both tables are included regardless of whether the join condition involves NULL.

Example:

SELECT * FROM TableA FULL OUTER JOIN TableB ON TableA.column1 = TableB.column2; 

Rows with NULL values in TableA.column1 or TableB.column2 will be included in the result, with NULL for unmatched columns.

5. CROSS JOIN

Behavior: A cross join produces the Cartesian product of the two tables. NULL values do not affect the result since no join condition is used.

Example:

SELECT * FROM TableA CROSS JOIN TableB;

6. SELF JOIN

  • Behavior: Acts like an INNER JOIN or OUTER JOIN (depending on the type) but is applied within the same table. NULL values are treated the same way as in other joins.

Key Takeaways

  • NULL in equality conditions (=): Evaluates to FALSE.
  • NULL in non-equality conditions (<>, >, <): Evaluates to FALSE.
  • Use functions like IS NULL or IS NOT NULL to handle NULL explicitly in join conditions, if needed.
    • SELECT * FROM TableA LEFT JOIN TableB ON TableA.column1 = TableB.column2 OR TableB.column2 IS NULL;

If you want to match rows where the department_id is NULL in both the employee and department tables, you cannot use the standard = operator, as NULL = NULL evaluates to FALSE. Instead, you need to explicitly handle the NULL values in your join condition using the IS NULL operator.

Query to Handle This Scenario:

SELECT e.employee_id, e.employee_name, d.department_id, d.department_name
FROM employee e
LEFT JOIN department d
ON (e.department_id = d.department_id OR (e.department_id IS NULL AND d.department_id IS NULL));

Explanation:

  1. e.department_id = d.department_id:
    • Matches rows where the department_id values are not NULL and equal in both tables.
  2. (e.department_id IS NULL AND d.department_id IS NULL):
    • Explicitly handles the case where both department_id values are NULL, allowing them to match.
  3. LEFT JOIN:
    • Ensures that all employees are included in the result, even if they do not have a corresponding department.

Example Data:

employee Table:

employee_idemployee_namedepartment_id
1Alice10
2BobNULL
3Charlie20

department Table:

department_iddepartment_name
10HR
20IT
NULLUndefined

Output of the Query:

employee_idemployee_namedepartment_iddepartment_name
1Alice10HR
2BobNULLUndefined
3Charlie20IT

Alternative with FULL OUTER JOIN (If Both Tables Must Be Fully Mapped):

If you want to ensure no rows are missed from either table, you can use a FULL OUTER JOIN:

SELECT e.employee_id, e.employee_name, d.department_id, d.department_name
FROM employee e
FULL OUTER JOIN department d
ON (e.department_id = d.department_id OR (e.department_id IS NULL AND d.department_id IS NULL));

This approach guarantees that all rows from both tables are included, handling NULL matches appropriately.

10 thoughts on “Handling NULL in join condition”
  1. I cling on to listening to the reports lecture about receiving boundless online grant applications so I have been looking around for the most excellent site to get one. Could you tell me please, where could i acquire some?

  2. Thanks for all your valuable work on this website. My aunt really likes setting aside time for investigation and it is easy to understand why. All of us notice all regarding the compelling medium you create very useful tricks through your blog and as well as cause participation from the others on that issue while our simple princess is always being taught a great deal. Have fun with the remaining portion of the year. You’re the one doing a good job.

  3. This blog is definitely rather handy since I’m at the moment creating an internet floral website – although I am only starting out therefore it’s really fairly small, nothing like this site. Can link to a few of the posts here as they are quite. Thanks much. Zoey Olsen

  4. Thanks a lot for giving everyone an extraordinarily brilliant possiblity to discover important secrets from here. It can be very good plus full of a good time for me personally and my office peers to search the blog more than three times in 7 days to learn the fresh things you have got. Not to mention, I’m also always contented considering the gorgeous suggestions you give. Some 3 facts on this page are in fact the very best we have all had.

  5. Can I just say what a relief to find someone who actually knows what theyre talking about on the internet. You definitely know how to bring an issue to light and make it important. More people need to read this and understand this side of the story. I cant believe youre not more popular because you definitely have the gift.

  6. Hi, I think your site might be having browser compatibility issues. When I look at your website in Safari, it looks fine but when opening in Internet Explorer, it has some overlapping. I just wanted to give you a quick heads up! Other then that, fantastic blog!

Leave a Reply

Your email address will not be published. Required fields are marked *