top 2 from table

SQL Query: Fetch Top 2 Records by Salary

Fetch Top 2 Records from a Table

🧩 Problem Statement:

You 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
101AbhayHR60000
102adityaIT75000
103VivekFinance72000
104ShubhamIT75000
105PradeepHR58000

✅ Sample Output

EmployeeID Name Department Salary
102adityaIT75000
104ShubhamIT75000

💡 SQL Query (Approach 1 & 2):


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

Comments

Popular posts from this blog

Fetch Top 2 Records From Table

diamond problem CPP(most asked in interview)