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.
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.
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.
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?
- SELECT P.EmployeeID, T.[Name]
FROM Employee P LEFT OUTER JOIN Location T
ON T.TerritoryID = P.TerritoryID
WHERE T.TerritoryID IS NULL - SELECT P.EmployeeID, T.[Name]
FROM Employee P LEFT OUTER JOIN Location T
ON T.TerritoryID = P.TerritoryID
WHERE P.TerritoryID IS NULL - SELECT P.EmployeeID, T.[Name]
FROM Employee P RIGHT OUTER JOIN Location T
ON T.TerritoryID = P.TerritoryID
WHERE T.TerritoryID IS NULL - 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)
135 Comments. Leave new
4. SELECT P.EmployeeID, T.[Name]
FROM Employee P RIGHT OUTER JOIN Location T
ON T.TerritoryID = P.TerritoryID
WHERE P.TerritoryID IS NULL
Ramdas
Charlotte,USA
The correct answer is number 4
USA
Hello Pinal,
The Correct option for the above question is : 4
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
Detailed Explanation :
As in the above query we have right outer join between Employee and Location Tables which take’s all rows from Location table whether or not the matching ‘TerritoryID’ rows from the Employee Table.
When the rows from Employee table doesn’t equate or matches with the ‘TerritoryID’ of Location Table then that row will be shown as NULL. And such rows are filtered by condition ‘P.TerritoryID IS NULL’. So this query will display all locations with no employees (i.e.) it selects only result having employees with location null.
OPTION 1.This will return the employees record whose Territory ID not in Location table and employees whose territory ID is null in employee table
OPTION 2.This will return the employees record whose territory ID is null in employee table.
OPTION 3. This will return the records which are having the territoryID as null in location table (If the location table permits null values to the TerritoryID column).
So option 4 is the correct option.
DILIP KUMAR JENA
COUNTRY : INDIA
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
(Sale, Nigeria)
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
Explanation:- RIGHT OUTER JOIN will get all of the location records irrespective of Employee.
COUNTRY:- INDIA
Option 4 is the answer
Sweden
Thank you, Pinal Dave, for this interesting article. I can use this other view on finding un-mathching records in a query I (re)wrote yesterday.
I’ll continue reading your articles as they come.
Many thanks all the way from the Netherlands (Europe).
Ruud van de Beeten
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
It’s a bit of a tricky question as it’s not easy to de-cipher RIGHT JOIN as LEFT JOIN is usually simpler. I always would have written this query as NOT EXISTS.
I’m from USA
Option 4 is correct
India
Hi Pinal sir,
The correct answer for this question is option no. 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
Reason: We have to find out all the location that have no employee.
Above query is fulfilling all the criteria to provide expected results.
To have location with no employee we have to take a help of OUTER JOIN and from the result set of query we required Location id that is null (i.e. TerritoryID). We have RIGHT OUTER JOIN of Employee table with Location Table. Which will provide all the matching and mismatching rows from location table i.e all the location who have employee or not have a single employee.
And in Where clause we are finding TerritoryID of Employee table which is null.
And TerritoryID of Employee table is null only when there is no employee for respective location when used with right outer join.
Following options are not correct because:
Option 1:
SELECT P.EmployeeID, T.[Name]
FROM Employee P LEFT OUTER JOIN Location T
ON T.TerritoryID = P.TerritoryID
WHERE T.TerritoryID IS NULL
TerritoryID will be not null for location table.
Option 2.
SELECT P.EmployeeID, T.[Name]
FROM Employee P LEFT OUTER JOIN Location T
ON T.TerritoryID = P.TerritoryID
WHERE P.TerritoryID IS NULL
Left outer join is used for employee table with location table and TerrirotyID will not be null for employee table.
Option 3.
SELECT P.EmployeeID, T.[Name]
FROM Employee P RIGHT OUTER JOIN Location T
ON T.TerritoryID = P.TerritoryID
WHERE T.TerritoryID IS NULL
TerritoryID will be not null for location table.
Regard$
Chirag Satasiya
The right answer for this Question 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;
The right outer join with null give the value of Location which is not having employee
Paurav
Surat, India
ELECT P.EmployeeID, T.[Name]
FROM Employee P RIGHT 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;
4.SELECT P.EmployeeID, T.[Name]
FROM Employee P RIGHT OUTER JOIN Location T
ON T.TerritoryID = P.TerritoryID
WHERE P.TerritoryID IS NULL
Sudeepta,
USA
Option 4 is Right
Country india
The answer 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
The right outer join gives us all of the matched and unmatched locations when joining to the employee table. Filtering for a null value in the employee table will give us all locations where no employees are at.
Country of Residence: USA
Answer 4.
SELECT P.EmployeeID, T.[Name]
FROM Employee P RIGHT OUTER JOIN Location T
ON T.TerritoryID = P.TerritoryID
WHERE P.TerritoryID IS NULL
Ashish, India
4.SELECT P.EmployeeID, T.[Name]
FROM Employee P RIGHT OUTER JOIN Location T
ON T.TerritoryID = P.TerritoryID
WHERE P.TerritoryID IS NULL
HELLO GUYS
WHEN I WROTE THIS QUERY I WAS VERY EXITING
REQIREMENT:-
I WANT TO GET THE PATIENT DETAILS(;TABLE :- PATIENT)
BUT THESE PATIENT SHOULD NOT IN ADMISSION(;TABLE :- ADT_ADMN)
IN CASE THE ADMISSION PATIENTS ARE IN DISCHARGE(;TABLE :- ADT_DSCHRG) I WANT THE RESULT SET
QUERY:-
CREATE TABLE PATIENT(PATIENT_ID INT IDENTITY,
FIRST_NAME VARCHAR(20),
LAST_NAME VARCHAR(20),
PATIENT_NAME AS(FIRST_NAME+’ ‘+LAST_NAME))
GO
CREATE TABLE ADT_ADMN(ADMN_ID INT IDENTITY,
PATIENT_ID INT)
GO
CREATE TABLE ADT_DSCHRG(DSCHRG_ID INT IDENTITY,
PATIENT_ID INT)
GO
SELECT *
FROM PATIENT P
LEFT JOIN ( SELECT AA.PATIENT_ID
FROM DBO.ADT_ADMN AA
LEFT JOIN DBO.ADT_DSCHRG AD ON AA.ADMN_ID = AD.ADMN_ID
WHERE AD.DSCHRG_ID IS NULL
)AS AAAD ON P.PATIENT_ID = AAAD.PATIENT_ID
WHERE P.PATIENT_ID IS NULL
Right 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
Francisco,
Miami