How to select top 3 highest salary in sql

Find out more about sql, salary, sql developer, top 3 highest salary in sql , top three highest salary in sql , how to select top 3 highest salary in sql on careerkeg.com

In this article we will show how to select top 3 highest salary in sql. We will use SQL query to fetch top 3 highest salaries from employee table. Now lets jump into our example…

The SQL query below selects the top three highest-paid employees in a given company:

SELECT TOP 3 * FROM Employees WHERE Salary > ALL (SELECT MAX(Salary) FROM Employees)

How to select top 3 highest salary in sql

Introduction

The following SQL statement selects the employees who have a salary that is in the highest third of salaries:

select distinct T.Salary

SELECT DISTINCT T.salary FROM (SELECT TOP 3 salary FROM Employee ORDER BY salary DESC) as T

from (select top 3 Salary

  • from (select top 3 Salary) AS d1, (select top 3 highest salary in sql) AS d2(where d1.Salary d2.Salary) AS s3;

from Employee

In order to get the highest salary in SQL, you should use the following query:

select max(salary) from Employee;

order by Salary desc) as T

In order to sort the result set from high to low, use the following query. The SqlDataReader class in C# provides a property named ReadAheadFillFactor. This property specifies the degree of prefetching for a query and is used to improve performance. The default value of this property is 100, which means that the rows are read into memory in batches of 100 (if your table has more than 1,000 rows). You can limit this value by setting it lower or higher depending on how many batches you want to load into memory at one time. For example, if your table has 10K records and you set ReadAheadFillFactor = 1000 then only 1k records will be loaded at once into memory while reading each time instead of loading all 10K records as they come every time you need them again (which might take longer).

select top 3 highest salary in sql

The following queries are some of the ways to get the top three highest salaries in SQL:

  • Select distinct salary from employee_info where department_id=’10000′ order by salary desc limit 3;
  • Use a subquery to get the first value for each employee, and then order the result set with an ORDER BY clause:
  • select department_id, max(salary) maxsalary from ( select floor(max(salary)) over () as maxsalary from employee_info ) hd left join (select distinct department_id from employee_info) as thd on thd.department_id = hd.department_id group by 1 order by 2 desc;
  • Use a UNION to combine two queries together: * SELECT * FROM ( SELECT MAX(Salary) AS Salary FROM EmployeeInfo WHERE DepartmentID = ‘10000’) AS MaxSalaryGroup INNER JOIN (SELECT DepartmentID FROM EmployeeInfo GROUP BY DepartmentID HAVING COUNT(*) > 5) AS EmpCounted ON MaxSalaryGroup.DepartmentID = EmpCounted.DepartmentID AND EmpCounted.COUNT(*) > 3 ORDER BY Salary DESC LIMIT 2;

Conclusion

The difference between these solutions is subtle. The first solution is the most elegant and compact, but it requires the use of window functions which may not be available in all versions of SQL. The second and third solutions are simpler, more common, but require a subquery to get the result. It’s up to you to choose your own poison; each has its own strengths and weaknesses.

Leave a Reply