Kartik
Kartik

Reputation: 9853

Building a SQL query for this

I am trying to do the following but I dont have enough experience with MySQL. Would it be possible for someone to tell me what the query for this will be.

I have a databse with 2 tables

Brief and Info have various fields but I am interested in only ID field.

This is what I am trying to do.

I want to go through all the IDs from Brief and cross reference them with all the IDs that exists in Info and only get the ones that exist in Info but dont exist in Brief

Thanks

Upvotes: 3

Views: 49

Answers (3)

Question Mark
Question Mark

Reputation: 3606

SELECT * from info i LEFT JOIN brief b ON b.id=i.id WHERE b.id IS NULL

Upvotes: 1

Paul Sonier
Paul Sonier

Reputation: 39480

SELECT ID FROM INFO 
   WHERE ID NOT IN 
   (SELECT ID FROM BRIEF);

Upvotes: 1

The Scrum Meister
The Scrum Meister

Reputation: 30111

SELECT i.ID
FROM Info i 
  LEFT JOIN Brief b USING(ID)
WHERE b.ID IS NULL

Alternatively:

SELECT i.ID
FROM Info i
WHERE NOT EXISTS (
  SELECT 1
  FROM Brief b
  WHERE b.ID = i.ID
)

See these 2 links for performance comparisons:

Upvotes: 4

Related Questions