Display details of first 5 highly paid employees using MySQL

This is a most general asked question in a MySQL database interview to find the nth highest paid salary of the employee. There are several ways to get this, but in this article, we will use the easiest process to fetch the first nth highest paid salary of the employee.

Suppose, we have the following employee salary records -

CREATE TABLE IF NOT EXISTS `empsalary` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `ename` varchar(100) NOT NULL,
  `salary` int(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=11 DEFAULT CHARSET=latin1;

INSERT INTO `empsalary` (`id`, `ename`, `salary`) VALUES
(1, 'Smith', 30000),
(2, 'Joney', 20000),
(3, 'Mariya', 40000),
(4, 'Zoya', 35000),
(5, 'Lorya', 25000),
(6, 'Rosy', 75000),
(7, 'Aditya', 40000),
(8, 'Kirti', 34000),
(9, 'Joya', 20500),
(10, 'Morya', 40500);

+----+--------+--------+
| id | ename  | salary |
+----+--------+--------+
|  1 | Smith  |  30000 |
|  2 | Joney  |  20000 |
|  3 | Mariya |  40000 |
|  4 | Zoya   |  35000 |
|  5 | Lorya  |  25000 |
|  6 | Rosy   |  75000 |
|  7 | Aditya |  40000 |
|  8 | Kirti  |  34000 |
|  9 | Joya   |  20500 |
| 10 | Morya  |  40500 |
+----+--------+--------+

Solution to get nth highest paid salary

mysql> SELECT * FROM `empsalary` ORDER BY salary DESC LIMIT 5;

Output of the above statement

+----+--------+--------+
| id | ename  | salary |
+----+--------+--------+
|  6 | Rosy   |  75000 |
| 10 | Morya  |  40500 |
|  7 | Aditya |  40000 |
|  3 | Mariya |  40000 |
|  4 | Zoya   |  35000 |
+----+--------+--------+
MySQL concatenate
MySQL Get Current Date, User, Version
MySQL Get Maximum Value
MySQL Administrator
MySQL where clause
MySQL order by
MySQL get difference between two dates
MySQL Pattern Matching
MySQL Join
MySQL Regular Expression
MySQL delete duplicate row
MySQL update multiple rows
Get nth highest salary using MySQL
Display the nth row from MySQL
Commit and rollback in mysql
MySQL display the alternate rows



Read more articles


General Knowledge



Learn Popular Language