There are 2 tables, Employee and Department. There are few records in employee table, for which, the department is not assigned. The output of the query should contain all th employees names and their corresponding departments, if the department is assigned otherwise employee names and null value in the place department name. What is the query?

Showing Answers 1 - 34 of 34 Answers

Rakesh Shankpal

  • Mar 10th, 2006
 

Use an ouer join to get your query.

Select E.ENAME, D.DNAME

from Employee E, Dept D

where D.DEPTNO (+)= E.DEPTNO

 

 

-Rakesh

  Was this answer useful?  Yes

Stephen

  • Mar 21st, 2006
 

An outer join is correct, but try using the ANSII standard join syntax as it is more flexible with outer joins and filtering of the inner tables

select e.name As Employee, d.name as Dept
from

  Was this answer useful?  Yes

mary

  • Mar 22nd, 2006
 

select name, department from employee left join department on employee.department_id = department.id

  Was this answer useful?  Yes

Tahir

  • Jul 3rd, 2006
 

Select DeptNo, DName, Count(1)FROM Emp a, Dept bWHERE a.deptno*=b.deptnoGroup By DeptNo, DName

  Was this answer useful?  Yes

Himu

  • Jul 27th, 2006
 

select e.ename,d.dname

from employee e,department d

where e.eid=d.eid(+);

  Was this answer useful?  Yes

Rajesh

  • Aug 11th, 2006
 

 

Select a.ename, b.dname

from Emp a, Dept b

where a.deptno (+)= b.deptno

  Was this answer useful?  Yes

maddie.march83

  • Mar 29th, 2007
 

Hi All,

A bit lengthy but a good alternative for the JOINS...

 (Select
  E.Emp_Name as EmployeeName,
  D.Dept_Name as DepartmentName,
  D.Dept_Id as DepartmentID
 from
  #tmp_Emp E,
  #tmp_Dept D
 where
  E.Dept_Id = D.Dept_Id
 )
 UNION
 (Select
  E.Emp_Name as EmployeeName,
  DepartmentName = 'NULL',
  E.Dept_Id as DepartmentID
 from
  #tmp_Emp E
 where
  E.Dept_Id IS NULL
 )


Regards,
Maddie.

  Was this answer useful?  Yes

shamimaziz

  • Jul 21st, 2007
 

What Mr Rakesh written is, I think Oracle query,
Here is the Sybase (TSQL Query for that)

Select a.empid, b.departmentname from employee a, department b where a.empid *= b.empid

  Was this answer useful?  Yes

Pratap Chavda

  • Aug 17th, 2007
 

For Oracle:

Select a.ename, b.dname

from Emp a, Dept b
where a.deptno (+)= b.deptno

For Sybase it should be like this:


Select a.ename,
b
.dname
from Emp a,
Dept b
where a.deptno *= b.deptno

as all the rows from the Emp table are requested

  Was this answer useful?  Yes

tumeshk

  • Jun 14th, 2008
 

Use the outerjoin  in your query.
For Ex:

select * from employee, department where employee.id *= department.id

  Was this answer useful?  Yes

Use a left outer join , it is going to pick all records in employee table and the corresponding records in department table if available

select e.emp,d.dept
from employee e, department d
where e.dept_id*=d.dept_id

  Was this answer useful?  Yes

ANSI Syntax:

SELECT Emp.ENAME, Dept.ENAME
FROM Employee Emp left join Department Dept
ON Emp.DEPTNO = Dept.DEPTNO

Transact-SQL Syntax:

SELECT Emp.ENAME, Dept.ENAME
FROM Employee Emp, Department Dept
WHERE Emp.DEPTNO *= Dept.DEPTNO
       
Use *= for left outer join and =* for right outer join.

Thanks,
Kevin

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions