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)

- Dhass April 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Can you please explain what the logic is?

Thanks.

- aap67 June 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Saurabh Sundriyal July 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- java.interviews.questions September 13, 2013 | Flag
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

- praveenkcs28 April 20, 2013 | Flag Reply
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)

- Nisha V Krishnan June 29, 2013 | Flag Reply
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.

- Saurabh Sundriyal July 01, 2013 | Flag
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.

- Nisha V Krishnan August 22, 2014 | Flag
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);

- nueman fernandez May 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Saurabh Sundriyal July 01, 2013 | Flag
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

- Saurabh Sundriyal July 01, 2013 | Flag Reply
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

- Anonymous July 05, 2013 | Flag Reply
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);

- aspirant September 07, 2013 | Flag Reply
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 ;

- Anonymous February 26, 2014 | Flag Reply
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 ;

- Samurai February 26, 2014 | Flag Reply
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)

- bijoy July 15, 2014 | Flag Reply
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;

- SUGANYA June 14, 2016 | Flag Reply
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;

- Nimmi Menon February 05, 2017 | Flag Reply
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;

- Nimmi Menon February 05, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.


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