Interview Question


Country: United States
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
4
of 8 vote

SQLServer:

WITH T1(Emp,Manager,Salary) AS
(
SELECT tt2.[Emp],tt2.[Manager],tt2.[Salary]
FROM [YourTable] AS tt1
RIGHT OUTER JOIN [YourTable] AS tt2 ON tt1.[Emp]=tt2.[Manager]
WHERE tt1.[Emp] is NULL
UNION ALL
SELECT r.[Emp],T1.[Manager],r.[Salary]
FROM [YourTable] AS r
INNER JOIN T1 ON r.[Manager]=T1.[Emp]
)
SELECT [Manager],SUM([Salary]) AS Salary
FROM T1
GROUP BY [Manager]
ORDER BY SUM([Salary]) DESC

- EchoJi May 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Great work EchoJi . It took me a little while to understand your brilliance!

- Interested July 28, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This query wasn't giving me the right results. i have modified it in the following way

SELECT SUM(EMP_SAL) AS COUNT_SAL
FROM Employee_Test
WHERE EMP_NAME IN 
(
SELECT TT2.Emp_name --,TT2.Emp_Sal
FROM Employee_Test AS tt1 
RIGHT OUTER JOIN Employee_Test AS tt2 ON tt1.[Emp_Name]=tt2.[Manager] 
WHERE tt1.[Emp_Name] is NULL )

- moham316 April 04, 2015 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

SELECT S1.MANG,SUM(S1.SALARY) FROM SALARY S1 LEFT JOIN SALARY S2 ON
S1.MANG = S2.EMP WHERE S2.EMP IS NULL
GROUP BY S1.MANG

- mailayushi6 June 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It can be done using Hierarchical queries in Sql. Use Connet by Prior clause. I will post the query soon.

- Prashant Kesarwani May 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use union-find data structure to solve this problem.

get_parent(manager){
    if parent[manager] != manager
        m = get_parent(manager)
        parent[manager] = m
        return m
    else
        return manager
}
        
get_total(){
    for e in (Emp and Manager):
        parent[e] =  e
        total[e] = 0


    for  each (Emp Manager Salary ) record:{
        p = get_parent(Manager)
       parent[Emp] = p
       total[p] += Salary
    }
    for p in parent:
        if parent[p]  <> p:
            print p, total[p]
}

- jigs May 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

correcting

get_parent(manager){
    if parent[manager] != manager
        m = get_parent( parent[[manager])
        parent[manager] = m
        return m
    else
        return manager
}

- jigs May 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

for p in parent:
        if parent[p]  == p:
            print p, total[p]

- jigs May 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Thanks EchoJi, a very good illustration of recursive CTE.

- DB May 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The query will be looking something like this

SELECT TOP_LVL, SUM(SAL) FROM (SELECT CONNECT_BY_ROOT MNGR AS TOP_LVL, SAL FROM EMP CONNECT BY PRIOR EMP =  MNGR) GROUP BY TOP_LVL

- Raj July 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

SELECT E2.ENAME,
(SELECT SUM(e1.SAL)
FROM EMP1 E1
START WITH E1.EMPNO = e2.empno
CONNECT BY PRIOR e1.empno = e1.mgr
)sum_sal
FROM EMP1 E2
WHERE LEVEL =1
START WITH MGR IS NULL
CONNECT BY PRIOR EMPNO =MGR;

- Anonymous September 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

could you please explain the working of this clause

- Anonymous September 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

use connect by prior

SELECT top_lvl, SUM(sal)
FROM (SELECT connect_by_root mngr AS top_lvl, sal
FROM emp
CONNECT BY PRIOR emp = mngr)
GROUP BY top_lvl

- Asis Dey December 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

create table t1(
emp int,
mang int,
sal int)

insert into t1 values (1,null,100)
insert into t1 values (2,1,90)
insert into t1 values (3,1,90)
insert into t1 values (4,2,80)
insert into t1 values (5,3,80)
insert into t1 values (6,2,80)
insert into t1 values (7,3,80)

insert into t1 values (8,4,70)
insert into t1 values (9,5,70)
insert into t1 values (10,6,70)
insert into t1 values (11,7,70)

with temp1(emp,mang,sal,level) as (
select a.emp,a.mang,a.sal,1
from t1 a
where a.mang is null

union all

select a.emp,a.mang,a.sal,level+1
from t1 a
join temp1 b on a.mang = b.emp

)

select distinct level,(select sum(sal) from temp1 b where a.level <= b.level) from temp1 a
where level in (1,2)

- Ganesh Vel T December 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

SQL> select Top_Lvl, Sum(S)
2 from
3 (select connect_by_root M as Top_Lvl, S
4 from empl
5 connect by prior E=M)
6 where Top_Lvl Not In ('A', 'B')
7 group by Top_Lvl;

T SUM(S)
- ----------
T 26
F 13

- Puneet Agrawal February 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

;with t1 (employeeID, manager_EID, salary) as(
select e1.employeeID, e1.employeeID, e1.salary
from employee_manager_tbl e1
where e1.manager_EID is null
UNION ALL
select e3.employeeID, t1.manager_EID, e3.salary
from employee_manager_tbl e3 inner join t1
on e3.manager_EID=t1.employeeID)
select manager_EID, SUM(salary)
from t1
group by manager_EID
order by manager_EID

- Wendy March 31, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

SELECT S1.MANG,SUM(S1.SALARY) FROM SALARY S1 LEFT JOIN SALARY S2 ON
S1.MANG = S2.EMP WHERE S2.EMP IS NULL
GROUP BY S1.MANG

- mailayushi6 June 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Select m.eid,sum(sal) from emp m, emp e on(e.mid = m.eid) group by m.eid order by sum(sal) desc;

- Amit May 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Select m.eid,sum(sal) from emp m, emp e on(e.mid = m.eid) group by m.eid order by sum(sal) desc;

- Amit May 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

SELECT *
FROM employee_h e
start with manager = 'T'
CONNECT BY PRIOR emp = manager;

- Deepak April 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

SELECT *
FROM employee_h e
start with manager = 'T'
CONNECT BY PRIOR emp = manager;

- Deepak April 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

SELECT *
      FROM employee_h e
      start with manager = 'T'
CONNECT BY  PRIOR emp = manager;

- Deepak April 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

with recursive avgsalary as ( select employeeid, managerid, salary from salary where managerid='T' union select s.employeeid, s.managerid, s.salary from salary s inner join avgsalary a on s.managerid=a.employeeid ) select sum(salary) from avgsalary;

- tourist12334 August 19, 2020 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More