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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Querying records via a outer join

Status
Not open for further replies.
Apr 20, 2000
41
US
Hi fellow SQlers...

I'm trying to do a sql command that will return the records that are NOT in another associated oracle table.

select * from employees
where employeeid not found in employee_history

Need to retrieve contractors which are in the employees file but do not exist in the employee_history file.

thanks for your help ...

 

Maybe you could try:
Code:
select * from employees 
where employeeid not in (
select employeeid from employee_history)
--or--
Code:
select * from employees e
where employeeid not exists (
select 1 from employee_history h
where h.employeeid = e.employeeid)

[3eyes]

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
In Oracle you have another option:

Code:
SELECT e.* FROM employees e, employee_history h
WHERE e.employeeid = h.employeeid(+)
  AND h.employeeid IS NULL;

JoeB
Use smaller fonts to save disk space...
 

joe, he want to return the records that are NOT in another associated oracle table. [shadeshappy]

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Yes, the query I posted will do just that. Outer joins using the (+) notation denotes the dependant/child table, and the "h.employeeid IS NULL" says only show records in employee where there is no match in history.

I was going off of the sql-pseudo code orginally posted:

select * from employees
where employeeid not found in employee_history

It's similar to your first query, just doing a join instead of a sub-query...

JoeB
Use smaller fonts to save disk space...
 
Hi,
As always, there is yet another way:
Code:
Select * from employee where employee_id IN
(
select employee_id from employee
MINUS
select employee_id from employee_history
);

Will only return records whose employee_id is NOT in employee_history.







[profile]

To Paraphrase:"The Help you get is proportional to the Help you give.."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top