Get row data for the lowest and highest values in a MySQL column


For the lowest values in a MySQL column, use the MIN() method and for highest, use the MAX() method. Let us first create a table −

mysql> create table DemoTable
(
   CustomerName varchar(20),
   ProductAmount int
) ;
Query OK, 0 rows affected (1.03 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('Chris',3599);
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('David',7843);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values('Mike',97474);
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable values('Bob',65884);
Query OK, 1 row affected (0.19 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+--------------+---------------+
| CustomerName | ProductAmount |
+--------------+---------------+
| Chris        |          3599 |
| David        |          7843 |
| Mike         |         97474 |
| Bob          |         65884 |
+--------------+---------------+
4 rows in set (0.00 sec)

Following is the query to get the lowest and highest values using MAX() and MIN() −

mysql> select *from DemoTable where ProductAmount=(select max(ProductAmount) from DemoTable)
   or ProductAmount=(select min(ProductAmount) from DemoTable);

This will produce the following output −

+--------------+---------------+
| CustomerName | ProductAmount |
+--------------+---------------+
| Chris        |          3599 |
| Mike         |         97474 |
+--------------+---------------+
2 rows in set (0.00 sec)

Updated on: 09-Oct-2019

377 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements