Q:

Display details of first 5 highly paid employees using MySQL

belongs to collection: MySQL Exercises

0

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 |
+----+--------+--------+

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 |
+----+--------+--------+

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

MySQL Exercises

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
<< Display from the 5th row from the MySQL 'empd...