
- 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
How to find the minimum and maximum values in a single MySQL Query?
To find the minimum and maximum values in a single query, use MySQL UNION. Let us first create a table −
mysql> create table DemoTable ( Price int ); Query OK, 0 rows affected (0.57 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(88); Query OK, 1 row affected (0.30 sec) mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values(98); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(120); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------+ | Price | +-------+ | 88 | | 100 | | 98 | | 120 | +-------+ 4 rows in set (0.00 sec)
Following is the query to find the minimum and maximum values in a single MySQL Query −
mysql> select Price from DemoTable where Price=(select min(Price) from DemoTable) UNION select Price from DemoTable where Price=(select max(Price) from DemoTable);
This will produce the following output −
+-------+ | Price | +-------+ | 88 | | 120 | +-------+ 2 rows in set (0.00 sec)
- Related Articles
- How can I get maximum and minimum values in a single MySQL query?
- How to set all values in a single column MySQL Query?
- MySQL query to select maximum and minimum salary row?
- Select the minimum value from the maximum values of two tables with a single MySQL\nquery?
- How to find the previous and next record using a single query in MySQL?
- Use LIKE % to fetch multiple values in a single MySQL query
- How to use a single MySQL query to count column values ignoring null?
- Fastest way to insert with multiple values in a single MySQL query?
- Count boolean field values within a single MySQL query?
- How to use COUNT() and IF() in a single MySQL query?
- How to add column and index in a single MySQL query?
- How to find the minimum and maximum of columns values in an R data frame?
- How do I insert multiple values in a column with a single MySQL query?
- A single MySQL query to find the highest and lowest among two tables?
- Insert multiple values in a temporary table with a single MySQL query?

Advertisements