How to get the top 3 salaries from a MySQL table with record of Employee Salaries?


For this, use LIMIT and OFFSET. Let us first create a table −

mysql> create table DemoTable867(EmployeeSalary int);
Query OK, 0 rows affected (0.64 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable867 values(63737);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable867 values(899833);
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable867 values(23644);
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable867 values(89393);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable867 values(534333);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable867 values(889322);
Query OK, 1 row affected (0.08 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable867;

This will produce the following output −

+----------------+
| EmployeeSalary |
+----------------+
| 63737          |
| 899833         |
| 23644          |
| 89393          |
| 534333         |
| 889322         |
+----------------+
6 rows in set (0.00 sec)

Here is the query to get first highest salary −

mysql> select distinct(EmployeeSalary) from DemoTable867 order by EmployeeSalary DESC LIMIT 1;

This will produce the following output −

+----------------+
| EmployeeSalary |
+----------------+
| 899833         |
+----------------+
1 row in set (0.02 sec)

Here is the query to get second highest salary −

mysql> select distinct(EmployeeSalary) from DemoTable867 order by EmployeeSalary DESC LIMIT 1 OFFSET 1;

This will produce the following output −

+----------------+
| EmployeeSalary |
+----------------+
| 889322         |
+----------------+
1 row in set (0.00 sec)

Following is the query to get third highest salary −

mysql> select distinct(EmployeeSalary) from DemoTable867 order by EmployeeSalary DESC LIMIT 1 OFFSET 2;

This will produce the following output −

+----------------+
| EmployeeSalary |
+----------------+
| 534333         |
+----------------+
1 row in set (0.00 sec)

Updated on: 03-Sep-2019

978 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements