Menu

How to get records from one table that does not exist in another?

Written by Selva Prabhakaran | 2 min read

Problem

You have two tables, tableA and tableB. You want to retrieve all records from tableA that do not have a matching record in tableB based on a specific column.

Input

Let’s start with creating two tables and populating them with data.

tableA – This will be our primary table where we’ll select data from.

tableB – This is the reference table. If an ID from tableA exists in tableB, we don’t want to select it.

tableA

id name
1 John
2 Jane
3 Doe
4 Smith
5 Eva

tableB

id description
1 First ID
3 Third ID
5 Fifth ID

Try Hands-On: HERE

Source Tables: Gist

Desired Solution

id name
2 Jane
4 Smith

Solution 1:

To get the desired records from tableA, you can use a LEFT JOIN or a NOT IN clause. Here’s how you can do it with both methods:

Using LEFT JOIN

sql
    SELECT A.id, A.name
    FROM tableA A
    LEFT JOIN tableB B ON A.id = B.id
    WHERE B.id IS NULL;

Solution 2:

sql
SELECT id, name
FROM tableA
WHERE id NOT IN (SELECT id FROM tableB);

Explanation:

In the LEFT JOIN method, we are joining tableA with tableB based on the id column.

The LEFT JOIN ensures that even if there’s no matching record in tableB, the record from tableA is still returned. We then use the WHERE B.id IS NULL condition to filter out records that do have a match in tableB.

In the NOT IN method, we are simply checking if the id from tableA exists in tableB. If it doesn’t exist, then that record is selected.

Both of these methods will give the desired result, but depending on the data size and indexes, performance can vary, so it’s good to know both approaches.

  1. SQL for Data Science – Level 1
  2. SQL for Data Science – Level 2
  3. SQL for Data Science – Level 3
  1. Introduction to SQL
  2. SQL Window Functons – Made Simple and Easy
  3. SQL Subquery

More SQL Questions

  1. How to access previous row value in SQL?
  2. How to exclude specific columns using select except?
  3. How to transpose columns to rows in SQL?
  4. How to select first row in each GROUP BY group?
Free Course
Master Core Python — Your First Step into AI/ML

Build a strong Python foundation with hands-on exercises designed for aspiring Data Scientists and AI/ML Engineers.

Start Free Course
Trusted by 50,000+ learners
Related Course
Master SQL — Hands-On
Join 5,000+ students at edu.machinelearningplus.com
Explore Course
Get the full course,
completely free.
Join 57,000+ students learning Python, SQL & ML. One year of access, all resources included.
📚 10 Courses
🐍 Python & ML
🗄️ SQL
📦 Downloads
📅 1 Year Access
No thanks
🎓
Free AI/ML Starter Kit
Python · SQL · ML · 10 Courses · 57,000+ students
🎉   You're in! Check your inbox (or Promotions/Spam) for the access link.
⚡ Before you go

Python.
SQL. NumPy.
All free.

Get the exact 10-course programming foundation that Data Science professionals use.

🐍
Core Python — from first line to expert level
📈
NumPy & Pandas — the #1 libraries every DS job needs
🗃️
SQL Levels I–III — basics to Window Functions
📄
Real industry data — Jupyter notebooks included
R A M S K
57,000+ students
★★★★★ Rated 4.9/5
⚡ Before you go
Python. SQL.
All Free.
R A M S K
57,000+ students  ★★★★★ 4.9/5
Get Free Access Now
10 courses. Real projects. Zero cost. No credit card.
New learners enrolling right now
🔒 100% free ☕ No spam, ever ✓ Instant access
🚀
You're in!
Check your inbox for your access link.
(Check Promotions or Spam if you don't see it)
Or start your first course right now:
Start Free Course →
Scroll to Top
Scroll to Top
Course Preview

Machine Learning A-Z™: Hands-On Python & R In Data Science

Free Sample Videos:

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science