Menu

How to find missing IDs between two lists (and get the SQL for free)
📰

How to find missing IDs between two lists (and get the SQL for free)

DEV Community: database·kdkumawat·3 days ago
#F5wwx6On
#devtodatabase#devto#dev#list#select
Reading 0:00
15s threshold

You have two exports. One is the source of truth (every paying customer in billing), the other is what you're auditing (customers synced to the CRM). The question is always the same: which IDs are in the first list but missing from the second?

Here are three ways to answer it, depending on where the data lives.

1. Both lists are already in the database

Use an anti-join. NOT EXISTS is the safe default because it behaves correctly when the other side contains NULL s ( NOT IN does not):

SELECT b . customer_id FROM billing b WHERE NOT EXISTS ( SELECT 1 FROM crm c WHERE c . customer_id = b . customer_id );

Enter fullscreen mode

Exit fullscreen mode

Or, if your database supports it:

SELECT customer_id FROM billing EXCEPT SELECT customer_id FROM crm ;

Enter fullscreen mode

Exit fullscreen mode

2. One list is in a spreadsheet, the other in the database

Turn the pasted list into rows with VALUES , then anti-join against the table:

SELECT v .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.