← Back to SQL, page 7

sql-0137 · Medium mid

What happens when CASE branches return different types, such as THEN salary ELSE 'n/a'?

PostgreSQL resolves all branches to one common type, here integer, so the literal 'n/a' fails to convert and the query errors. SQLite has dynamic typing, so each row keeps whatever type its branch produced. Keep branches the same type, for example by casting the number to text or returning NULL instead of a marker string.

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;
namepayt
Asha120000integer
Lenan/atext

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 purpose
text
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;
namepay
Asha120000
Lenan/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