Amazon Interview Question for Software Engineer / Developers






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

select max(sal) from emp
union
select max(sal) from emp where sal not in (select max(sal) from emp)

- yins September 27, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use the sql top command ...

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

Be careful with this answer. A number of interviews I've been on asked this same question with the stipulation that you do it only in standard SQL. "Top" is only valid on MSSQL. "Limit" is for MySQL. "Rownum" is for Oracle. All of these are technically non-standard SQL.

The answer they are looking for is use self joins.

Sumanth has it correct.

- Tazzy July 15, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

LOL. If that is the case, they are idiots, IMO.

- LOLer July 15, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

one has to write a recursive query first find out the max(M1) ,then again find out the max such that max!= M1.implementation may vary from rdbms used but i guess logic is some what same

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

Select Top 2 Salary
From Salary_Table
Order BY Salary DESC;

This works !

- Anonymous July 07, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think question also states about the result in each group. So small addition to your SQL should make it right.

Select Top 2 Salary
From Salary_Table
GROUP BY group_field
Order BY Salary DESC;

- Atomic July 25, 2009 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Group by could not be used because there is no aggregate function in the select statement.

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

select * from emp_tab where ( select count(*) from emp_tab st where st.sal < emp_tab.sal ) <= n ; ( here n is 2 )

- Sumanth July 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

select * from emp_tab where ( select count(*) from emp_tab st where st.sal < emp_tab.sal ) <= n ; ( here n is 2 )

- Sumanth July 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This will not give the correct result..here is the new one:

select * from Emp_Table where
(select count(*) from Emp_Table ET where ET.salary > Emp_Table.salary
and ET.groupID=Emp_Table.groupid) <=1

- DKAM September 21, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

this will not work if two maximum salary are equal

- sandy880 January 29, 2011 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 votes

This will give the top 2 records with top 2 salary from the table emp.

select * from emp where emp.sal >=
(select max(emp.sal) from emp where emp.sal <> (select max(emp.sal) from emp));

- Santa April 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This will give the top 2 records with top 2 salary from the table emp.

select * from emp where emp.sal >=
(select max(emp.sal) from emp where emp.sal <> (select max(emp.sal) from emp));

- Santa April 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I am not that familiar with writing complex sql queries,

but

1) Recursive queries?
2) select from another select?

When we have the 'top' command, which is highly likely to have been optimized by the Database, why does one have to go and try to come up with silly queries which are likely to be inefficient?

Correctness is not the only virtue...

- LOLer July 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

because sometimes you have to write SQL code used in different platforms: e.g. both Oracle and SQL Server

- Anonymous June 08, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I completely agree with LOLer's logic!

- maddy August 30, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

select * from emp e1 where ( select count(*) from emp e2 where e2.sal>e1.sal and e1.grp=e2.grp) < n

This query gives top 'n' salaries of each group..

The "top" and other such things are non standard constructs which are specific to a DBMS..

- Teja October 06, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

select sal, deptno from
(select sal, t.deptno, rank() over (partition by deptno order by sal desc) r
from scott.emp t) 
where r < 3

- puzzle October 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Select * From Employee E1 Where
(N-1) = (Select Count(Distinct(E2.Salary)) From Employee E2 Where
E2.Salary > E1.Salary)

- Anonymous February 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assume salary table only has one column salary:

select t0.salary, count(t1.salary) rank
from
(select distinct salary from salary) t0
left outer join
(select distinct salary from salary) t1
on t0.salary < t1.salary
group by t0.salary
having rank < 2
order by t0.salary desc

- ypliu November 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

select distinct salary
from emp e1
where 2 >= (select count(distinct(salary)) from emp2 where e1.salary<=e2.salary)
order by salary DESC);

- amit March 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Amit's solution works

- Sai June 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

;with ctetst
as
(
SELECT
Grp, Salary, ROW_NUMBER() OVER(PARTITION BY Grp ORDER BY Salary DESC) as rowss
FROM GrpTbl
)

SELECT
Grp, Salary, (CASE WHEN rowss = 1 THEN 'Highest Salary' ELSE 'Second Highest' END) as salaryLevel
FROM ctetst
WHERE rowss IN (1, 2)

- Niraj March 12, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

SELECT e1.empname, e1.`deptid`, e1.salary, COUNT(e2.empid)
FROM emp e1 LEFT OUTER JOIN emp e2
ON e1.`salary` < e2.salary
AND e1.`deptid` = e2.`deptid`
GROUP BY e1.`empname`, e1.`deptid`, e1.salary
HAVING COUNT(e2.empid)  < 2

- Joe P Kumar May 21, 2015 | 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