Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


table:employee
EID ENAME MID(manager ids)
101 rama null
102 sita 101
103 siva 101
104 ganesh 103
. . .
. . .
for 103 ID the manager ID is 101(RAMA) and for 104 manager
is SIVA
if i give employee id (EID) you have to tell the manager for
that EID write query?
eample:if i give 102 .The query output should be manager for
102 ID that it should print RAMA as output

Answers were Sorted based on User's Feedback



table:employee EID ENAME MID(manager ids) 101 rama null 102 ..

Answer / soorai ganesh

create table emp (eid int, ename varchar(50),mid int)

insert into emp values(1,'Ganesh',null)
insert into emp values(2,'Ramesh',1)
insert into emp values(3,'Suresh',1)
insert into emp values(4,'Selvam',2)
insert into emp values(5,'Vignesh',3)

select * from emp

Declare @eid int
Set @eid = 4 -- Your Input ID here..

Select 'Manager For '+CONVERT(VARCHAR,emp2.eid)+' --> '+ emp1.ename
From emp emp1
Inner join emp emp2 on emp1.eid = emp2.mid and emp2.eid = @eid

Is This Answer Correct ?    4 Yes 1 No

table:employee EID ENAME MID(manager ids) 101 rama null 102 ..

Answer / samba shiva reddy . m

select emp.ename from employee emp1
inner join employee emp
on emp1.mid=emp.eid
where emp1.eid=102

Is This Answer Correct ?    2 Yes 0 No

table:employee EID ENAME MID(manager ids) 101 rama null 102 ..

Answer / saravanan p

Declare @eid int
Set @eid=2

select 'The manager for employee id '+convert(varchar
(50),e2.eid)+' is '+e1.ename from emp e1,emp e2
where e1.eid=e2.mid and e2.eid=@eid

Is This Answer Correct ?    1 Yes 0 No

table:employee EID ENAME MID(manager ids) 101 rama null 102 ..

Answer / mohana krishna

create table #emp (eid int, ename varchar(50),mid int)

insert into #emp values(1,'Ganesh',null)
insert into #emp values(2,'Ramesh',1)
insert into #emp values(3,'Suresh',1)
insert into #emp values(4,'Selvam',2)
insert into #emp values(5,'Vignesh',3)

declare @aid int
set @aid =1
select ename from #emp
where eid = (select case isnull(mid,0) when 0 then eid
else mid end mid
from #emp
where eid=@aid
)


select m.ename from #emp e
join #emp m on (m.eid=isnull(e.mid,e.eid))
where e.eid=1

Is This Answer Correct ?    0 Yes 0 No

table:employee EID ENAME MID(manager ids) 101 rama null 102 ..

Answer / suraj

-- create table #Employees (EID int, EName varchar(20), MID
int)
-- insert #Employees values(101,'Rama',NULL)
-- insert #Employees values(102,'Sita',101)
-- insert #Employees values(103,'Shiva',101)
-- insert #Employees values(104,'Ganesh',103)

--for 103 ID the manager ID is 101(RAMA) and for 104
manager is SIVA
--Write a script which displays Shiva's Manager's name.

select a.EID, a.EName, a.MID, b.EName
from #Employees a
inner join #Employees b on a.mid=b.eid and a.EName='Shiva'

Is This Answer Correct ?    1 Yes 1 No

table:employee EID ENAME MID(manager ids) 101 rama null 102 ..

Answer / samba shiva reddy . m

this will also works for u change is id in inner join

select emp1.ename from employee emp1
inner join employee emp
on emp1.eid=emp.mid
where emp.eid=102

Is This Answer Correct ?    0 Yes 0 No

table:employee EID ENAME MID(manager ids) 101 rama null 102 ..

Answer / mohana krishna

select ename from employee
where eid = (select case mid when null then aid
else mid end mid
where eid=@aid
)


select m.name from employee e
join employee m on (m.aid=e.mid)

Is This Answer Correct ?    0 Yes 3 No

Post New Answer

More SQL Server Interview Questions

What is database white box testing?

0 Answers  


What are different types of Keys? Please explain all the keys with a suitable example.

4 Answers  


What is normalization of database?

0 Answers  


i have a table #temp1(id, Name groupname ) and record like this 1 R1 S 2 R3 S 3 R2 S 4 R4 D 5 R5 D 6 R6 K 7 R7 K 8 R8 L 9 R9 L 10 R10 L 11 R11 K and i want to display record based on user defind sorting order e.g. 1 R4 D 2 R5 D 3 R6 K 4 R7 K 5 R11 K 6 R1 S 7 R3 S 8 R2 S 9 R8 L 10 R9 L 11 R10 L

8 Answers  


what r steps to we need to fallow b4 kill the process?

1 Answers   Verizon,


What is blocking?

0 Answers  


how can i store resumes in database?

3 Answers   HCL,


Why I am getting this error when renaming a database in ms sql server?

0 Answers  


How do you optimize Sql queries ?

0 Answers   Cognizant,


hi. suppose one person goal is strong knowledge on SQL.... suggest me ...if he learns SQL SERVER or ORACLE? which is best?

7 Answers   Google, TCS, Unisys,


How to create function without parameter in sql server?

0 Answers  


Differentiate between SQL and ORACLE joins and write their syntax.

0 Answers   Global Logic,


Categories