Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple Question on SQL

Status
Not open for further replies.

DB2Problem

Programmer
Oct 17, 2002
53
US
I have two tables as shown below -

Table 1 - emp_rec table

Empl_ID Empl_Name On_Vacation
200 Jhon Y
300 Sean Y
400 Bill N

Table 2 - emp_table

Empl_ID Empl_Name
200 Jhon
300 Sean

I want to select records which are not in Table 2 BUT in Table 1. In this case record of Bill and if we found that there is record available, then set the value of On_Vacation as "Y" for all cases.

Empl_ID is primary key for all above tables.

Thanks for the help.






 
I want to select records which are not in Table 2 BUT in Table 1
One way:
SELECT *
FROM Table1
WHERE Empl_ID NOT IN (SELECT Empl_ID FROM Table2)
Another way:
SELECT Table1.*
FROM Table1 LEFT JOIN Table2 ON Table1.Empl_ID = Table2.Empl_ID
WHERE Table2.Empl_ID IS NULL

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
UPDATE Table1 SET On_Vacation = 'Y'
WHERE Empl_ID NOT IN (SELECT Empl_ID FROM Table2)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top