← Back to SQL, page 4

sql-0074 · Hard senior

In SQLite, what does SELECT name, MAX(salary) FROM employees return? Is that behaviour portable?

SQLite has a documented special case: when a query has exactly one MIN() or MAX() aggregate, bare columns are taken from the row that holds that minimum or maximum. So it returns Asha with 120000. If several rows tie, one of them is chosen. No other major engine does this; PostgreSQL rejects the query.

Full answer
sql
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');
sql
SELECT name, MAX(salary) AS top_salary FROM employees;
nametop_salary
Asha120000

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:

sql
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_idnametop_salary
1Asha120000
2Tom70000
3Diego55000

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:

sql
SELECT name, MAX(salary) AS top_salary FROM employees WHERE name <> 'Asha';
nametop_salary
Ravi95000

The second weakness is portability. PostgreSQL rejects the query outright:

sql
SELECT name, MAX(salary) FROM employees;  -- error on purpose
text
Error: column "employees.name" must appear in the GROUP BY clause or be used in an aggregate function

The 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:

sql
SELECT name, salary
FROM employees
WHERE salary = (SELECT MAX(salary) FROM employees WHERE name <> 'Asha')
  AND name <> 'Asha'
ORDER BY name;
namesalary
Mei95000
Ravi95000

Knowing the SQLite rule helps you read existing code. Writing new code that depends on it ties the query to one engine.

sql
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