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');The query as written:
sql
SELECT d.name, (SELECT e.name FROM employees e WHERE e.dept_id = d.id) AS employee FROM departments d ORDER BY d.id;| name | employee |
|---|---|
| Engineering | Asha |
| Sales | Tom |
| Support | Diego |
| Legal | NULL |
Engineering has three employees, but SQLite returns one of them without any warning, and which one is not defined. Legal has none, so it shows NULL. PostgreSQL stops instead:
sql
SELECT d.name, (SELECT e.name FROM employees e WHERE e.dept_id = d.id) AS employee FROM departments d ORDER BY d.id; -- error on purposetext
Error: more than one row returned by a subquery used as an expressionA corrected version:
sql
SELECT d.name, (SELECT e.name FROM employees e WHERE e.dept_id = d.id ORDER BY e.salary DESC, e.id LIMIT 1) AS top_earner FROM departments d ORDER BY d.id;| name | top_earner |
|---|---|
| Engineering | Asha |
| Sales | Tom |
| Support | Diego |
| Legal | NULL |
Ordering inside the subquery and taking one row makes the intent explicit: the top earner per department. The same corrected query succeeds in PostgreSQL:
sql
SELECT d.name, (SELECT e.name FROM employees e WHERE e.dept_id = d.id ORDER BY e.salary DESC, e.id LIMIT 1) AS top_earner FROM departments d ORDER BY d.id;| name | top_earner |
|---|---|
| Engineering | Asha |
| Sales | Tom |
| Support | Diego |
| Legal | NULL |
When all names are wanted, aggregate them instead (group_concat in SQLite, string_agg in PostgreSQL).
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 d.name, (SELECT e.name FROM employees e WHERE e.dept_id = d.id) AS employee FROM departments d ORDER BY d.id;
SELECT d.name, (SELECT e.name FROM employees e WHERE e.dept_id = d.id ORDER BY e.salary DESC, e.id LIMIT 1) AS top_earner FROM departments d ORDER BY d.id;Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.
Key points
- Scalar subqueries must return one row
- SQLite takes the first row silently
- PostgreSQL raises an error
- Make the choice explicit with ORDER BY and LIMIT 1
Common mistakes
- Relying on SQLite's silent first row
- Using LIMIT 1 without ORDER BY
Follow-ups
- How would you list all employees per department in one column?
- How would a LATERAL join express this?
Tips
- Contrast the engines