Re: SQL Question

From: Daniel A. Morgan <dmorgan_at_exesolutions.com>
Date: Tue, 16 Jan 2001 21:40:13 -0800
Message-ID: <3A65303C.A2789523_at_exesolutions.com>


> I have 2 tables -
> EMPLOYEE : EmployeeID,DepartmentID, Name
> and
> DEPARTMENT : DepartmentID, Description
>
> Both DepartmentIDs are related.
>
> I want to retrieve Employee Names with
> EITHER
> - Employee.DepartmentID = 'SALES'
> OR
> ALL Employees
>
> In another word,
> If no employee is found with the department 'SALES', I would want the whole
> list of Employees returned or else only those with department 'SALES' will
> be returned.
>
> Is this possible ?

Not only possible ... it is a very standard bit of SQL known as an outer join. The syntax would be something like this:

SELECT e.employeeID, e.name, d.description FROM employee e, department d
WHERE e.departmentID = d.departmentID (+) AND d.departmentID = 'SALES'

The (+) signifying a left outer join in which you retrieve all records from the employee table (aliased "e") and where they exist, the department description where there is a corresponding department table record.

Daniel A. Morgan Received on Wed Jan 17 2001 - 06:40:13 CET

Original text of this message