MySQL Query to Select Maximum and Minimum Salary Row



For this, use sub query along with MIN() and MAX(). To display both the maximum and minimum value, use UNION ALL. Let us first create a table −

mysql> create table DemoTable
   -> (
   -> EmployeeName varchar(20),
   -> EmployeeSalary int
   -> );
Query OK, 0 rows affected (0.70 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('Bob',8800);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable values('Chris',9800);
Query OK, 1 row affected (0.63 sec)
mysql> insert into DemoTable values('David',7600);
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable values('Sam',9600);
Query OK, 1 row affected (0.14 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+--------------+----------------+
| EmployeeName | EmployeeSalary |
+--------------+----------------+
|          Bob |           8800 |
|        Chris |           9800 |
|        David |           7600 |
|          Sam |           9600 |
+--------------+----------------+
4 rows in set (0.00 sec)

Here is the query to select minimum salary row −

mysql> select *from DemoTable
   -> where EmployeeSalary in ( select max(EmployeeSalary) from DemoTable
   -> union all
   -> select min(EmployeeSalary) from DemoTable
   -> );

This will produce the following output −

+--------------+----------------+
| EmployeeName | EmployeeSalary |
+--------------+----------------+
|        Chris |           9800 |
|        David |           7600 |
+--------------+----------------+
2 rows in set (0.05 sec)
Updated on: 2019-12-12T05:32:20+05:30

837 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements