SQL query to find third highest salary

Kailash Singh

SQL query to find third highest salary

Published Sep 11,2022 by Kailash Singh

0 Comment     752 Views    


In this tutorial, we are going to teach you, how to find third highest salary using SQL.

We are using three ways to get the third highest salary using SQL: 

  1. SELECT TOP
  2. SUB-QUERY
  3. LIMIT

 

1. What is SELECT TOP Query?

The SELECT TOP statement in SQL shows the limited number of records or rows from the database table. The TOP clause in the statement specifies how many rows are returned.

Example:

SELECT TOP 1 salary 
FROM 
    (SELECT TOP 3 salary 
     FROM Table_Name 
     ORDER BY salary DESC) AS Comp 
ORDER BY salary ASC

 

2. What is Sub-query?

A Subquery or Inner query or a Nested query is a query within another SQL query and embedded within the WHERE clause.

A subquery is used to return data that will be used in the main query as a condition to further restrict the data to be retrieved.

Subqueries can be used with the SELECT, INSERT, UPDATE and DELETE statements along with the operators like =, <, >, >=, <=, IN, BETWEEN, etc.

Example : 

SELECT salary  
FROM 
    (SELECT salary 
     FROM Table_Name 
     ORDER BY salary DESC 
     LIMIT 3) AS Comp 
ORDER BY salary 
LIMIT 1;

 

3. What is LIMIT?

The LIMIT clause is used to specify the number of records to return.

The LIMIT clause is useful on large tables with thousands of records. Returning a large number of records can impact performance.

Example:

SELECT salary 
FROM Table_Name 
ORDER BY salary DESC 
LIMIT 2, 1


Comments ( 0 )