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)