GeekInterview.com
Series: Subject: Topic:
Question: 74 of 74

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?

Asked by: Interview Candidate | Asked on: Mar 8th, 2006
Showing Answers 1 - 16 of 16 Answers
Rakesh Shankpal

Answered On : 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

  
Login to rate this answer.
Stephen

Answered On : 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

  
Login to rate this answer.
mary

Answered On : Mar 22nd, 2006

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

  
Login to rate this answer.
Tahir

Answered On : Jul 3rd, 2006

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

  
Login to rate this answer.
Himu

Answered On : Jul 27th, 2006

select e.ename,d.dname

from employee e,department d

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

  
Login to rate this answer.
Himu

Answered On : Jul 27th, 2006

View all questions by Himu   View all answers by Himu

select e.empname,d.dptname

from employee e,department d

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

  
Login to rate this answer.
Rajesh

Answered On : Aug 11th, 2006

 

Select a.ename, b.dname

from Emp a, Dept b

where a.deptno (+)= b.deptno

  
Login to rate this answer.
maddie.march83

Answered On : 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.

  
Login to rate this answer.
shamimaziz

Answered On : Jul 21st, 2007

View all answers by shamimaziz

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

  
Login to rate this answer.
Pratap Chavda

Answered On : 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

  
Login to rate this answer.
tumeshk

Answered On : Jun 14th, 2008

View all answers by tumeshk

Use the outerjoin  in your query.
For Ex:

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

  
Login to rate this answer.
salilmulay

Answered On : Nov 11th, 2009

View all answers by salilmulay

select e.* from employee e
left outer join department d
on e.id_department = d.id_department

  
Login to rate this answer.

select a.emp_name,isnull(a.dept_nm,' ') from emp a,dept b
where a.dept_nm=b.debt_nm

  
Login to rate this answer.
ajitmishra1983

Answered On : Jan 25th, 2010

View all answers by ajitmishra1983

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

  
Login to rate this answer.
kevinkpatel99

Answered On : Feb 21st, 2010

View all answers by kevinkpatel99

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

  
Login to rate this answer.

The query for this question can be written by using the outer join.

select* from employee a,department b where a.emp_id*=b.emp_id

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Ads

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.