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
Output of the above statement
+----+--------+--------+ | id | ename | salary | +----+--------+--------+ | 6 | Rosy | 75000 | | 10 | Morya | 40500 | | 7 | Aditya | 40000 | | 3 | Mariya | 40000 | | 4 | Zoya | 35000 | +----+--------+--------+