sqlcmd.net validated sql reference
beginner aggregation MySQL MariaDB SQL Server PostgreSQL SQLite

Find The Smallest And Largest Values With MIN And MAX

Use `MIN` and `MAX` to find the lowest and highest values in a column.

Docker-validated Not currently validation-green

Find the coldest and hottest recorded temperatures

Chicago (15) is the coldest and Miami (32) is the hottest. Both functions scan all rows and return a single aggregate value. Add GROUP BY city to find the min and max per city instead of across the whole table.

Rows loaded before the example query runs.
Setup
CREATE TABLE temperatures (id INT, city VARCHAR(50), temp INT);

INSERT INTO
  temperatures (id, city, temp)
VALUES
  (1, 'NYC', 22),
  (2, 'LA', 28),
  (3, 'Chicago', 15),
  (4, 'Miami', 32);
Shared across supported engines.
SQL
SELECT
  MIN(temp) AS min_temp,
  MAX(temp) AS max_temp
FROM
  temperatures;
Returned rows for the shared example.
min_tempmax_temp
1532

Output is identical across all engines.

Where this command helps.

  • finding the smallest and largest values in a column
  • getting the full range of a dataset in a single query

What the command is doing.

MIN(column) returns the smallest value in the column and MAX(column) returns the largest. Both ignore NULL values. They work on numbers, dates, and strings — for strings, the comparison is lexicographic. Both can be combined in a single query and work with GROUP BY to find extremes per category.