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');SQLite accepts the mixed CASE and returns integers for some rows and text for others:
sql
SELECT name, CASE WHEN salary IS NOT NULL THEN salary ELSE 'n/a' END AS pay,
typeof(CASE WHEN salary IS NOT NULL THEN salary ELSE 'n/a' END) AS t
FROM employees WHERE id IN (1, 7) ORDER BY id;| name | pay | t |
|---|---|---|
| Asha | 120000 | integer |
| Lena | n/a | text |
typeof shows the mix. Sorting or comparing such a column later gives surprising results, because SQLite orders all integers before all text.
PostgreSQL refuses the same query:
sql
SELECT name, CASE WHEN salary IS NOT NULL THEN salary ELSE 'n/a' END AS pay
FROM employees WHERE id IN (1, 7) ORDER BY id;
-- error on purposetext
Error: invalid input syntax for type integer: "n/a"Two portable versions: cast the number so every branch is text, or return NULL and let the display layer print a placeholder.
sql
SELECT name, CASE WHEN salary IS NOT NULL THEN CAST(salary AS TEXT) ELSE 'n/a' END AS pay
FROM employees WHERE id IN (1, 7) ORDER BY id;| name | pay |
|---|---|
| Asha | 120000 |
| Lena | n/a |
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, CASE WHEN salary IS NOT NULL THEN salary ELSE 'n/a' END AS pay,
typeof(CASE WHEN salary IS NOT NULL THEN salary ELSE 'n/a' END) AS t
FROM employees WHERE id IN (1, 7) ORDER BY id;
SELECT name, CASE WHEN salary IS NOT NULL THEN CAST(salary AS TEXT) ELSE 'n/a' END AS pay
FROM employees WHERE id IN (1, 7) ORDER BY 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
- PostgreSQL needs one common result type for all branches
- SQLite lets each row keep its own type
- Mixed types cause odd sorting and comparisons
- Cast explicitly or return NULL for missing values
Common mistakes
- Returning a marker string like 'n/a' from a numeric CASE
- Testing only on SQLite and failing on PostgreSQL
Follow-ups
- How does COALESCE(salary, 'n/a') behave on each engine?
- Where should placeholders for missing values be produced?
Tips
- Keep data typed in SQL and format at the edge