Full answer
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 name, MAX(salary) AS top_salary FROM employees;| name | top_salary |
|---|---|
| Asha | 120000 |
The name really is the top earner's. SQLite documents this: with a single min() or max() aggregate, bare columns take their values from the row that produced the min or max. Per department it works too:
SELECT dept_id, name, MAX(salary) AS top_salary
FROM employees
WHERE dept_id IS NOT NULL
GROUP BY dept_id
ORDER BY dept_id;| dept_id | name | top_salary |
|---|---|---|
| 1 | Asha | 120000 |
| 2 | Tom | 70000 |
| 3 | Diego | 55000 |
Ties are the first weakness. Ravi and Mei share the second-highest salary. If Asha were removed, only one of the two would be returned, and which one is not defined:
SELECT name, MAX(salary) AS top_salary FROM employees WHERE name <> 'Asha';| name | top_salary |
|---|---|
| Ravi | 95000 |
The second weakness is portability. PostgreSQL rejects the query outright:
SELECT name, MAX(salary) FROM employees; -- error on purposeError: column "employees.name" must appear in the GROUP BY clause or be used in an aggregate functionThe portable way to get "the row with the maximum" is a subquery or a window function, which also lets you decide what happens with ties:
SELECT name, salary
FROM employees
WHERE salary = (SELECT MAX(salary) FROM employees WHERE name <> 'Asha')
AND name <> 'Asha'
ORDER BY name;| name | salary |
|---|---|
| Mei | 95000 |
| Ravi | 95000 |
Knowing the SQLite rule helps you read existing code. Writing new code that depends on it ties the query to one engine.
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 name, MAX(salary) AS top_salary FROM employees;
SELECT dept_id, name, MAX(salary) AS top_salary
FROM employees
WHERE dept_id IS NOT NULL
GROUP BY dept_id
ORDER BY dept_id;
SELECT name, MAX(salary) AS top_salary FROM employees WHERE name <> 'Asha';
SELECT name, salary
FROM employees
WHERE salary = (SELECT MAX(salary) FROM employees WHERE name <> 'Asha')
AND name <> 'Asha'
ORDER BY name;Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.
Key points
- SQLite: with one MIN or MAX, bare columns come from that row
- Ties return one arbitrary matching row
- PostgreSQL and standard SQL reject bare columns
- Use a subquery or window function for a portable 'row with max'
Common mistakes
- Relying on the SQLite rule and losing tied rows
- Porting SQLite queries with bare columns to PostgreSQL and hitting errors
Follow-ups
- How would you return all employees tied for the top salary?
- Does the rule hold if the query has two aggregates?
Tips
- Mention ties and portability; those are the two reasons not to depend on it