Question

Hi I have this two table

table 1

id    Selection  
-------------------
1     John           
2     Ely               
3     Marcus            
4     Steve           
5     Fritz           
6     Orly           
7     Carlo              
8     Lee    

table 2

id    Selected 
-------------------
1     John                         
3     Marcus 
4     Steve                     
5     Fritz           
7     Carlo 

the return would be the unselected rows. What would be the query for this output

id    Selection 
-------------------         
2     Ely                         
6     Orly                  
8     Lee
Was it helpful?

Solution

Use LEFT JOIN to join both table and t2.ID IS NULL to remove common records

SELECT t1.* FROM table1 t1 
  LEFT JOIN table2 t2 
    ON t1.ID = t2.ID 
 WHERE t2.ID IS NULL

Output:

╔════╦═══════════╗
║ ID ║ SELECTION ║
╠════╬═══════════╣
║  2 ║ Ely       ║
║  6 ║ Orly      ║
║  8 ║ Lee       ║
╚════╩═══════════╝

See this SQLFiddle

OTHER TIPS

You can use a Left Join:

 Select t1.id,t2.selection from 
 table1 t1 left join table2 t2 
 ON t1.ID = t2.ID 
 where t2.id is null;

Use This Query. Its worked for you.

select table1.* from table1 where table1.id not in (select id from table2)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top