Full answer
MIN and MAX are not limited to numbers. They return the smallest and largest value by the same comparison ORDER BY uses.
CREATE TABLE departments (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
dept_id INTEGER REFERENCES departments(id),
manager_id INTEGER REFERENCES employees(id),
salary INTEGER,
hire_date TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
(1,'Asha', 1,NULL,120000,'2019-03-01'),
(2,'Ravi', 1,1, 95000,'2020-07-15'),
(3,'Mei', 1,1, 95000,'2021-01-10'),
(4,'Tom', 2,1, 70000,'2022-05-20'),
(5,'Sara', 2,4, 62000,'2023-02-01'),
(6,'Diego',3,1, 55000,'2021-11-30'),
(7,'Lena', NULL,4, NULL, '2024-06-03'),
(8,'Omar', 3,6, 48000,'2024-09-09');SELECT MIN(hire_date) AS first_hire, MAX(hire_date) AS last_hire, MIN(name) AS first_name FROM employees;| first_hire | last_hire | first_name |
|---|---|---|
| 2019-03-01 | 2024-09-09 | Asha |
The earliest hire date is Asha's 2019 date and the latest is Omar's 2024 date, which works because ISO strings put the year first, then month, then day.
Text comparison has surprises. In SQLite's default binary collation, every uppercase letter sorts before every lowercase letter:
WITH t(n) AS (VALUES ('apple'), ('Zara'), ('mango'))
SELECT MIN(n) AS min_name, MAX(n) AS max_name
FROM t;| min_name | max_name |
|---|---|
| Zara | mango |
'Zara' is the minimum, because 'Z' (code 90) comes before 'a' (code 97). Use COLLATE NOCASE in SQLite, or a case-insensitive collation elsewhere, for a human alphabetical order:
WITH t(n) AS (VALUES ('apple'), ('Zara'), ('mango'))
SELECT MIN(n COLLATE NOCASE) AS min_name, MAX(n COLLATE NOCASE) AS max_name
FROM t;| min_name | max_name |
|---|---|
| apple | Zara |
Dates stored as text in another format, such as DD/MM/YYYY, break this completely: '31/01/2026' would be greater than '01/12/2026'. Store dates in ISO format or in a real date type.
CREATE TABLE departments (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
dept_id INTEGER REFERENCES departments(id),
manager_id INTEGER REFERENCES employees(id),
salary INTEGER,
hire_date TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
(1,'Asha', 1,NULL,120000,'2019-03-01'),
(2,'Ravi', 1,1, 95000,'2020-07-15'),
(3,'Mei', 1,1, 95000,'2021-01-10'),
(4,'Tom', 2,1, 70000,'2022-05-20'),
(5,'Sara', 2,4, 62000,'2023-02-01'),
(6,'Diego',3,1, 55000,'2021-11-30'),
(7,'Lena', NULL,4, NULL, '2024-06-03'),
(8,'Omar', 3,6, 48000,'2024-09-09');
SELECT MIN(hire_date) AS first_hire, MAX(hire_date) AS last_hire, MIN(name) AS first_name FROM employees;
WITH t(n) AS (VALUES ('apple'), ('Zara'), ('mango'))
SELECT MIN(n) AS min_name, MAX(n) AS max_name
FROM t;
WITH t(n) AS (VALUES ('apple'), ('Zara'), ('mango'))
SELECT MIN(n COLLATE NOCASE) AS min_name, MAX(n COLLATE NOCASE) AS max_name
FROM t;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- MIN and MAX follow the column's sort order
- ISO date strings sort chronologically
- Binary collation puts uppercase before lowercase
- Use a case-insensitive collation for human alphabetical order
Common mistakes
- Storing dates as DD/MM/YYYY text and using MAX to find the latest
- Expecting MIN(name) to ignore case
Follow-ups
- How do MIN and MAX treat NULLs?
- How would you find the employee hired most recently, not just the date?
Tips
- Mention the date format condition explicitly