I believe this interview question is trying to direct you to nested choices or general tabular expressions or something like that. TOP 2- A simple answer, and, obviously, TOPwas implemented just for this purpose - the interview wants you to do this "manually."
-. () , , , , , 3 .
MySQL -
():
select row_count, * from salary order by salary desc
:
select * from <nested select> where row_count < 3
, MySQL, SQL Server.
SQL Server, :
declare @Salaries table
(
id int,
salary money
)
insert into @salaries (id, salary)
values (1, 1),
(2, 2),
(3, 3),
(4, 4),
(5, 4)
;WITH Props AS
(
SELECT *,
ROW_NUMBER() OVER (ORDER BY salary desc) AS RowNumber
FROM @Salaries
)
SELECT * FROM Props WHERE RowNumber < 3
4 5.
Sachin Kainth
, . SQL Server:
declare @Salaries table
(
id int,
salary money
)
insert into @salaries (id, salary)
values (1, 1),
(2, 2),
(3, 3),
(4, 4),
(5, 4)
select * from @salaries where salary in
(
SELECT MAX(E1.Salary)
FROM @salaries E1, @salaries E2
WHERE E1.Salary < E2.Salary
union
SELECT MAX(Salary)
FROM @salaries
)
3, 4 5. 4 5. , where salary in 3, 4 5, ( 3 4).