Fetch Top 2 Records From Table

 Title: Fetch Top 2 Records from a Table.


Problem StatementYou are given access to a table named Employees, which contains employee details. Your task is to write a query to fetch the top 2 records from the table based on their Salary in descending order.

If two or more employees have the same salary, prioritize by EmployeeID in ascending order.


Sample Input Table: Employees


EmployeeID Name  Department Salary

101        Abhay    HR               60000

102       aditya    IT                      75000

103       Vivek         Finance           72000

104       Shubham    IT                      75000

105       Pradeep    HR             58000


Sample Output:

EmployeeID Name Department Salary
102 aditya    IT 75000
104 Shubham    IT 75000


Approach 1:

SELECT TOP 2 *
FROM Employees
ORDER BY Salary DESC, EmployeeID ASC;


Approach 2:

SELECT TOP 2 *
FROM Employees
ORDER BY Salary DESC, EmployeeID ASC;




Comments

Popular posts from this blog

top 2 from table

Diamond problem in CPP