[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Using a Left Join to find records that are Not equal.

Posted on 2012-08-22
Medium Priority
Last Modified: 2012-08-22

I am trying to reconcile the data from two tables.  Basically, I need to figure out which entries exists in one table (members), that don't exist in the other (access).

I know by using a LEFT JOIN I can compare the data when they are equal but, how do I display the information that is not equal?

My query is working but, I only want to show the rows that are not equal to each other.

Here is my query:

$get_missing_NUIDs = "SELECT members.NUID FROM members LEFT JOIN access ON members.NUID=access.NUID";

I need to show the members.NUID that are found in the members table but, are not found in the access table.

Thanks for your help!
Question by:evibesmusic
  • 2
  • 2
LVL 143

Accepted Solution

Guy Hengel [angelIII / a3] earned 2000 total points
ID: 38322056
this query will return the result you need:

$get_missing_NUIDs = "SELECT members.NUID FROM members LEFT JOIN access ON members.NUID=access.NUID WHERE access.NUID IS NULL";

Author Comment

ID: 38322100

OK, this works but, help me understand some thing here.

Using 'IS NULL' usually represents a value that blank or empty.  In this instance it seems to mean a value that isn't there, or is not in the table.

Am I thinking of this usage of IS NULL correctly?

Thanks for your help.

LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38322116
yes, you understand correctly.

if one used a INNER join here, the condition IS NOT NULL cannot be true, as the field checked is the joining condition field, and CANNOT be null.
for left join, you want to find the rows that don't match, so where the resulting field is returned "null".

Author Closing Comment

ID: 38322204
Perfect solution. To the point.

Explanation of usage of IS NULL in this instance helped cement the concept for me too.

Thanks angelIII!
LVL 12

Expert Comment

ID: 38322223
If it makes it more intuitive, you could also do something like this:
$get_missing_NUIDs = "SELECT members.NUID FROM members
    where members.NUID not in (select access.NUID from access)";

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month18 days, 19 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question