
- 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 can I get maximum and minimum values in a single MySQL query?
To get maximum and minimum values in a single query, use the aggregate function min() and max(). Let us first create a table:
mysql> create table DemoTable ( FirstValue int, SecondValue int ); Query OK, 0 rows affected (0.66 sec)
Following is the query to insert some records in the table using insert command:
mysql> insert into DemoTable values(10,30); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(30,60); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(100,500); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(50,80); Query OK, 1 row affected (0.18 sec)
Following is the query to display records from the table using select command:
mysql> select *from DemoTable;
This will produce the following output
+------------+-------------+ | FirstValue | SecondValue | +------------+-------------+ | 10 | 30 | | 30 | 60 | | 100 | 500 | | 50 | 80 | +------------+-------------+ 4 rows in set (0.00 sec)
Following is the query to get maximum and minimum values in a single query:
mysql> select min(FirstValue),min(SecondValue),max(FirstValue),max(SecondValue) from DemoTable;
This will produce the following output:
+-----------------+------------------+-----------------+------------------+ | min(FirstValue) | min(SecondValue) | max(FirstValue) | max(SecondValue) | +-----------------+------------------+-----------------+------------------+ | 10 | 30 | 100 | 500 | +-----------------+------------------+-----------------+------------------+ 1 row in set (0.00 sec)
- Related Articles
- How to find the minimum and maximum values in a single MySQL Query?
- How can I get the output of multiple MySQL tables from a single query?
- How do I insert multiple values in a column with a single MySQL query?
- Get multiple count in a single MySQL query for specific column values
- How to get multiple rows in a single MySQL query?
- How can I get enum possible values in a MySQL database?
- How to set all values in a single column MySQL Query?
- MySQL query to select maximum and minimum salary row?
- Can I get Age using BirthDate column in a MySQL query?
- Select the minimum value from the maximum values of two tables with a single MySQL\nquery?
- Can we use WHERE, AND & OR in a single MySQL query?
- How can I get enum possible values in a MySQL database using PHP?
- Count boolean field values within a single MySQL query?
- How to merge queries in a single MySQL query to get the count of different values in different columns?
- Can we get records “Jone Deo” or “Deo Jone” with a single MySQL query?

Advertisements