## Citigroup Interview Question for Financial Software Developers

Country: United States

Comment hidden because of low score. Click to expand.
6
of 6 vote

SELECT *
FROM Employee Emp1
WHERE (N-1) = (
SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary)

Comment hidden because of low score. Click to expand.
1
of 1 vote

Can you please explain what the logic is?

Thanks.

Comment hidden because of low score. Click to expand.
0

This gives the correct solution. But if you check the execution plan for this query, it takes an aweful lot of time to execute.
Best would be to use ranking functions as below:

Select top 1 Salary.Employee_Salary from (
select dense_rank() over (order by Employee_Salary desc) as 'Salary_Rank', Employee_Salary from Employee) as Salary
where Salary.[Salary_Rank] = N

Comment hidden because of low score. Click to expand.
0

The logic is if some number is top N in table then there will be (n-1) number more than that in table take a example
you have 9,8,7,6 in table as id and Top 2 will have only one number more than that so it is used

1 = SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary

Comment hidden because of low score. Click to expand.
3
of 3 vote

select * from questionbank order by salary desc limit 1 offset n-1

Comment hidden because of low score. Click to expand.
1
of 1 vote

--Below query is used to find out 3rd maximum salary.Replace the below subquery with --the number you want to.

SELECT TOP (1) EMP,SALARY FROM YOURTABLE
WHERE SALARY NOT IN (SELECT TOP(2) SALARY FROM YOURTABLE ORDER BY SALARY DESC)

Comment hidden because of low score. Click to expand.
1
of 1 vote

There can be multiple employees having same salary, then this logic fails.

Comment hidden because of low score. Click to expand.
1
of 1 vote

All you need is Nth max salary.So it does not count on duplicates.

Comment hidden because of low score. Click to expand.
0
of 0 vote

``SELECT DISTINCT (a.sal) FROM EMP A WHERE &N = (SELECT COUNT (DISTINCT (b.sal)) FROM EMP B WHERE a.sal < = b.sal);``

Comment hidden because of low score. Click to expand.
0

This gives the correct solution. But if you check the execution plan for this query, it takes an aweful lot of time to execute.
Best would be to use ranking functions as below:

Select top 1 Salary.Employee_Salary from (
select dense_rank() over (order by Employee_Salary desc) as 'Salary_Rank', Employee_Salary from Employee) as Salary
where Salary.[Salary_Rank] = N

Comment hidden because of low score. Click to expand.
0
of 0 vote

Select top 1 Salary.Employee_Salary from (
select dense_rank() over (order by Employee_Salary desc) as 'Salary_Rank', Employee_Salary from Employee) as Salary
where Salary.[Salary_Rank] = N

Comment hidden because of low score. Click to expand.
0
of 0 vote

//42 with the required n
select top 42 distinct salary into #temp from employee order by salary desc
select min(salary) from #temp

Comment hidden because of low score. Click to expand.
0
of 0 vote

replace 4 with require number to get the required nth max

``SELECT  max(Quantity) as ntop from OrderDetails where Quantity not in (select  distinct Quantity from OrderDetails order by Quantity desc limit 4);``

Comment hidden because of low score. Click to expand.
0
of 0 vote

select max(t.salary) from employee t where (select count(*) from employee b where t.salary-b.salary > 0) < n ;

Comment hidden because of low score. Click to expand.
0
of 0 vote

select max(t.salary) from employee t where (select count(*) from employee b where t.salary-b.salary > 0) < n ;

Comment hidden because of low score. Click to expand.
0
of 0 vote

Select min(salary) from (select top n from employee group by salary desc)

Comment hidden because of low score. Click to expand.
0
of 0 vote

SYNTAX:
SELECT MAX(column_name) FROM table_name;
EX:
SELECT MAX(Price) AS HighestPrice FROM Products;

Comment hidden because of low score. Click to expand.
0
of 0 vote

select salary from (select @rowid := @rowid + 1 as rank, salary from employee , (select @rowid:=0)a order by salary desc)b where rank = n;

Comment hidden because of low score. Click to expand.
0
of 0 vote

``select salary from (select @rowid := @rowid + 1 as rank, salary from employee , (select @rowid:=0)a order by salary desc)b where rank = 3;``

Comment hidden because of low score. Click to expand.

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.

### 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.