
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
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)
- Related Articles
- MySQL query to select one specific row and another random row?\n
- Program to find average salary excluding the minimum and maximum salary in Python
- Select highest salary in MySQL?
- MySQL query to generate row index (rank) in SELECT statement?
- MySQL query to select rows except first row in descending order?
- How to find the minimum and maximum values in a single MySQL Query?
- MySQL query to select a random row value (Id and Name) having multiple occurrences (Name)?
- MySQL query to delete row
- How can I get maximum and minimum values in a single MySQL query?
- How to select last row in MySQL?
- MySQL LIMIT to select a single row
- Select minimum row value from a column with corresponding duplicate column values in MySQL
- Delete rows with duplicate and similar content & get row with maximum number with MySQL select statement?
- Getting Minimum and Maximum Value in MySQL
- MySQL query to insert row with date?

Advertisements