Click here to Skip to main content
15,901,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a SQL Query that compares one database table to another. When I run the query it gives me the data but it also gives me data that I know that is in the database. What I am trying to do is to take table 1 and compare it to Table 2. I want to display the data that Table 2 doesn't have that Table 1 has.

Here is my Query:

SQL
SELECT User_ID, Name FROM Table1

EXCEPT 

SELECT User_ID, NAME FROM Table2 where YEAR = 2016

GROUP BY

    User_ID, Name


Did I miss something?

What I have tried:

I have tried to switch the query around.
Posted
Updated 14-Feb-17 0:48am

select * from table1 where user_id not in (select user_id from table2 where year='2016')


i don't *think* EXCEPT works over different tables
 
Share this answer
 
Comments
Computer Wiz99 14-Feb-17 9:18am    
Thanks for the help. I do have a question. When I use EXCEPT in other queries it works for different tables. Why do you think it started now?
Richard Deeming 14-Feb-17 9:39am    
There is no such restriction - EXCEPT works across different tables, different databases, and even different servers. The only restriction is that both inputs must have the same number of columns, with compatible data types.

EXCEPT and INTERSECT (Transact-SQL)[^]
Computer Wiz99 14-Feb-17 10:18am    
So you are saying that Table1 and Table2 must have the same number of columns in order for EXCEPT to work correctly? Even if I SELECT those columns?
Richard Deeming 14-Feb-17 10:21am    
No - the input queries must have the same number of columns.

It doesn't matter how many columns the source table has. It doesn't matter if your joining multiple tables. It doesn't matter if you've got calculated columns in the query.

So long as the two queries have the same number of columns, and the data types are compatible, EXCEPT will work.
You might be missing something, it works as expected.
SQL
declare @t1 table (
 col1 varchar(10)
)

declare @t2 table (
 col1 varchar(10),
 id int)

 insert into @t1 (col1) values ('a')
 insert into @t1 (col1) values ('b')
 insert into @t1 (col1) values ('c')

  insert into @t2 (col1,id) values ('a',0)
 insert into @t2 (col1,id) values ('b',1)
 insert into @t2 (col1,id) values ('c',0)

 select col1 from @t1 except
 select col1 from @t2 where id = 1
 group by col1


it returns 'a' and 'c'
 
Share this answer
 
(SELECT User_ID, Name FROM Table1)

MINUS

(SELECT User_ID,Name from Table2 where Year =2016)
 
Share this answer
 
Comments
Computer Wiz99 14-Feb-17 9:15am    
Member 12999924, I got a syntax error when running your code.

Msg 102, Level 15, State 1, Line 3
Incorrect syntax near 'MINUS'.
AnvilRanger 14-Feb-17 13:29pm    
MINUS is the Oracle equivalent of EXCEPT

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900