SQL SERVER – Tips from the SQL Joes 2 Pros Development Series – Finding un-matching Records – Day 5 of 35

Answer simple quiz at the end of the blog post and –
Every day one winner from India will get Joes 2 Pros Volume 1.
Every day one winner from United States will get Joes 2 Pros Volume 1.

Finding un-matching Records

Often time we want to find records in one table that have no matching key in another table. This is common for things like finding products that have never sold, or students who did not re-enroll. Something we were expecting is missing. Records in one table were expecting some related activity in another table and did not find them. There are many ways to find these records.

Basic Subquery

We have probably all heard that subqueries should be avoided if there is a better solution. Often times basic subqueries are used where a simple “Unmatched Records Query” could have been used.

Let’s start off with the subquery example. Looking at the Location table in the figure below we see all the data. In fact, this table does not allow nulls for the LocationID field. Looking further, there are no nulls anywhere in the Location table. So does this mean that all locations have at least one employee? It’s possible that some of these locations might be new and have not employees working there yet.

SQL SERVER - Tips from the SQL Joes 2 Pros Development Series - Finding un-matching Records - Day 5 of 35 j2p_5_1

How can we find the location with no employees. We can use a subquery. For example we can use the following query to find all locations of the JProCo database that have now employee working there.

SELECT *
FROM Location
WHERE LocationID
NOT IN (SELECT DISTINCT LocationID
FROM Employee
WHERE LocationID IS NOT NULL)

Unmatched Records Queries

If you wanted to find all locations with no employees, you could run an “Unmatched Records Query”. Let’s build this piece by piece. In this case, we have to join the Location table with the Employee table to determine the location that has no employees. What type of join will tell us this? Since nulls don’t map through a join, the INNER JOIN drops the record from the result set and we won’t see Chicago. The outer join will show both the matches and the unmatched records, so we see every location. In the figure below we get all location even if there are no employees.

Notice Seattle is listed many times but Chicago is listed once with no employees found. A NULL appears in the fields from the Employee table for Chicago. With the Location table on the left and the NULL on the right, we have part of an unmatched records query. To find just the records that don’t match, we look for null records on the table that the outer join does not favor. In this case, it’s the Employee table.

SQL SERVER - Tips from the SQL Joes 2 Pros Development Series - Finding un-matching Records - Day 5 of 35 j2p_5_2

The outer join will show us the unmatched records with null location details if you set the WHERE clause to look for nulls on a field in the non-dominant table. Unmatched record queries use SQL to return a result set displaying only the unmatched records between the two tables.

When our query criterion specifies NULL, only Chicago shows up in our result set. By doing a LEFT OUTER JOIN and using a NULL value from the Employee table (or “RIGHT” table) as our search condition criteria, our unmatched records query shows us one record.

SQL SERVER - Tips from the SQL Joes 2 Pros Development Series - Finding un-matching Records - Day 5 of 35 j2p_5_3

Note: If you want to setup the sample JProCo database on your system you can watch this video.

Question 5

Q 5) Which of the following queries will show all location that have no employees?

  1. SELECT P.EmployeeID, T.[Name] FROM Employee P LEFT OUTER JOIN Location T
    ON T.TerritoryID = P.TerritoryID
    WHERE T.TerritoryID IS NULL
  2. SELECT P.EmployeeID, T.[Name] FROM Employee P LEFT OUTER JOIN Location T
    ON T.TerritoryID = P.TerritoryID
    WHERE P.TerritoryID IS NULL
  3. SELECT P.EmployeeID, T.[Name] FROM Employee P RIGHT OUTER JOIN Location T
    ON T.TerritoryID = P.TerritoryID
    WHERE T.TerritoryID IS NULL
  4. SELECT P.EmployeeID, T.[Name] FROM Employee P RIGHT OUTER JOIN Location T
    ON T.TerritoryID = P.TerritoryID
    WHERE P.TerritoryID IS NULL

Please post your answer in comment section to win Joes 2 Pros books.

Rules:

Please leave your answer in comment section below with correct option, explanation and your country of resident.
Every day one winner will be announced from United States.
Every day one winner will be announced from India.
A valid answer must contain country of residence of answerer.
Please check my facebook page for winners name and correct answer.
Winner from United States will get Joes 2 Pros Volume 1.
Winner from India will get Joes 2 Pros Volume 1.
The contest is open till next blog post shows up at which is next day GTM+2.5.

Reference:  Pinal Dave (https://blog.sqlauthority.com)

Joes 2 Pros, SQL Scripts
Previous Post
SQL SERVER – Tips from the SQL Joes 2 Pros Development Series – Efficient Query Writing Strategy – Day 4 of 35
Next Post
SQL SERVER – Row Constructors – Day 6 of 35

Related Posts

135 Comments. Leave new

  • Answer for question 5:
    Above all option will give error, since the above all option join with common column TerritoryID which is not there in employee table nor in location table

    INDIA
    Brijesh

    Reply
    • Correct , I have not looked at the table columns .It definitely produce an error.
      None of option is correct.

      Reply
  • The correct answer is option 4. that is

    SELECT P.EmployeeID, T.[Name]
    FROM Employee P RIGHT OUTER JOIN Location T
    ON T.TerritoryID = P.TerritoryID
    WHERE P.TerritoryID IS NULL

    Here we have used right outer join it means it will bring all records from Location Table and will fatch NULL wherever TerritoryID is NULL for Employee table. So we can match records from Employee table which have TerritoryID as null.

    From
    INDIA
    AHMEDABAD

    Reply
  • The correct option is 4.

    SELECT P.EmployeeID, T.[Name]
    FROM Employee P RIGHT OUTER JOIN Location T
    ON T.TerritoryID = P.TerritoryID
    WHERE P.TerritoryID IS NULL

    Gets all locations that have no employees. The where condition will list all the territoryID from right table(i.e. Employee Table) which contains null values

    By
    Yasodha.N (India)

    Reply
  • Option No. 4 is correct:
    SELECT P.EmployeeID, T.[Name]
    FROM Employee P RIGHT OUTER JOIN Location T
    ON T.TerritoryID = P.TerritoryID
    WHERE P.TerritoryID IS NULL

    Country- India

    Reply
  • Dinesh Khatroja
    August 5, 2011 10:24 am

    OPTION 2 & 4 is Right

    2. SELECT P.EmployeeID, T.[Name]
    FROM Employee P LEFT OUTER JOIN Location T
    ON T.TerritoryID = P.TerritoryID
    WHERE P.TerritoryID IS NULL

    4..SELECT P.EmployeeID, T.[Name]
    FROM Employee P RIGHT OUTER JOIN Location T
    ON T.TerritoryID = P.TerritoryID
    WHERE P.TerritoryID IS NULL

    Reply
  • Partha Pratim Dinda
    August 5, 2011 10:35 am

    Ans:
    4. SELECT P.EmployeeID, T.[Name]
    FROM Employee P RIGHT OUTER JOIN Location T
    ON T.TerritoryID = P.TerritoryID
    WHERE P.TerritoryID IS NULL

    This queries will show all location that have no employees.

    Partha
    India

    Reply
  • Hi,

    4th is Correct:

    SELECT P.EmployeeID, T.[Name]
    FROM Employee P RIGHT OUTER JOIN Location T
    ON T.TerritoryID = P.TerritoryID
    WHERE P.TerritoryID IS NULL

    I am from India

    Reply
  • I think option 4 is the correct answer

    SELECT P.EmployeeID, T.[Name]
    FROM Employee P RIGHT OUTER JOIN Location T
    ON T.TerritoryID = P.TerritoryID
    WHERE P.TerritoryID IS NULL

    Country- India

    Reply
  • Nikhil Mahajan
    August 5, 2011 11:25 am

    Correct Answer is option : 4

    SELECT P.EmployeeID, T.[Name]
    FROM Employee P RIGHT OUTER JOIN Location T
    ON T.TerritoryID = P.TerritoryID
    WHERE P.TerritoryID IS NULL

    India

    Reply
  • The Correct Answer is Option-4

    Thanks
    Narendra (India)

    Reply
  • Correct answer is option 4.

    4. SELECT P.EmployeeID, T.[Name]
    FROM Employee P RIGHT OUTER JOIN Location T
    ON T.TerritoryID = P.TerritoryID
    WHERE P.TerritoryID IS NULL

    Country – India

    Reply
  • Correct option is 4.

    SELECT P.EmployeeID, T.[Name]
    FROM Employee P RIGHT OUTER JOIN Location T
    ON T.TerritoryID = P.TerritoryID
    WHERE P.TerritoryID IS NULL

    India
    Shilpa Sharma

    Reply
  • 4. SELECT P.EmployeeID, T.[Name]
    FROM Employee P RIGHT OUTER JOIN Location T
    ON T.TerritoryID = P.TerritoryID
    WHERE P.TerritoryID IS NULL;

    Here right outer join with null give the value of Location which is not having employee

    Ghanshyam
    Bangalore, India

    Reply
  • The 1, 2 3 are wrong
    Bocs
    1. will give the employees list whose Territory ID not in Location table
    and employees whose territory ID is null in employee table

    2. will give the employees list whose territory ID is null in employee table

    3. will give the records which having territoryID is null in location table
    (If the location table permits null values to the TerritoryID column)

    So option 4 is the correct answer

    4. which will Give list of location which have no employees
    and If the location table permits null values to the TerritoryID column
    those rows also will include.

    Govindaraj P bangalore India.

    Reply
  • The correct option is 4.

    SELECT P.EmployeeID, T.[Name]
    FROM Employee P RIGHT OUTER JOIN Location T
    ON T.TerritoryID = P.TerritoryID
    WHERE P.TerritoryID IS NULL

    Gets all locations that have no employees. The where condition will list all the territoryID from right table(i.e. Employee Table) which contains null values

    From
    Malay Shah
    Ahmedabad (India)

    Reply
  • The correct option is 4.

    When we apply right join then the result is all row from right table which is location in our case and respective match records from the left table and null in case of no match.
    And applying the where cause of null (P.TerritoryID IS NULL) will only select result having employees with location null

    Karan
    India

    Reply
  • Option 4 is correct
    SELECT P.EmployeeID, T.[Name]
    FROM Employee P RIGHT OUTER JOIN Location T
    ON T.TerritoryID = P.TerritoryID
    WHERE P.TerritoryID IS NULL

    Country INDIA

    Reply
  • Soumya Ranjan das
    August 5, 2011 12:58 pm

    Answer is Option# 4.
    SELECT P.EmployeeID, T.[Name]
    FROM Employee P RIGHT OUTER JOIN Location T
    ON T.TerritoryID = P.TerritoryID
    WHERE P.TerritoryID IS NULL

    Soumya Ranjan Das,
    Bhubaneswar, India

    Reply
  • The answer is:
    SELECT P.EmployeeID, T.[Name]
    FROM Employee P RIGHT OUTER JOIN Location T
    ON T.TerritoryID = P.TerritoryID
    WHERE P.TerritoryID IS NULL

    First two options does not work because, it is LEFT OUTER JOIN. If we do this, in the first step itself it does not show the location which does not have employee.

    Thirst option does not work because, it is checking for ‘T.TerritoryID IS NULL’ which is can not be null.

    So the fourth option will meet the requirement.

    Country: India

    Reply
  • Hi Pinal,

    Correct Answer is Option 4

    SELECT P.EmployeeID, T.[Name]
    FROM Employee P RIGHT OUTER JOIN Location T
    ON T.TerritoryID = P.TerritoryID
    WHERE P.TerritoryID IS NULL

    Thanks,
    Fazal(India)

    Reply

Leave a Reply