← Back to SQL, page 1

sql-0002 · Easy fresher

Why do most teams avoid SELECT * in application code, and when is it acceptable?

SELECT * returns every column, which ties the application to the current table layout and can move far more data than the screen needs. Listing columns keeps queries stable when columns are added, lets covering indexes help, and makes the intent clear. SELECT * is fine for quick exploration and inside EXISTS.

Full answer

SELECT * means "every column, in table order". That sounds harmless, but it causes three kinds of trouble in real code.

First, the result shape depends on the table. If someone adds a 2 MB profile_photo column to employees, every SELECT * now drags that blob across the network, even for a page that shows only names. If code reads columns by position, adding or reordering a column can silently shift values into the wrong variables.

Second, it blocks some index tricks. When a query asks only for columns that live inside an index, the database can answer from the index alone (a covering index) and never touch the table. SELECT * almost always forces a trip to the table.

Third, it hides intent. A reviewer reading SELECT name, salary knows exactly what the code needs.

Compare the two forms on a small table:

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 * FROM departments ORDER BY id;
idname
1Engineering
2Sales
3Support
4Legal
sql
SELECT name FROM departments ORDER BY name;
name
Engineering
Legal
Sales
Support

There are fair uses. In an ad-hoc session you often want to see everything. Inside EXISTS (SELECT * ...) the column list is ignored, because EXISTS only checks whether a row exists, so SELECT * and SELECT 1 behave the same there. Views defined with SELECT * are a trap in some engines, because the column list is fixed when the view is created.

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 * FROM departments ORDER BY id;

SELECT name FROM departments ORDER BY name;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • SELECT * couples code to the table layout
  • It can transfer large columns nobody uses
  • Naming columns allows covering-index reads
  • Inside EXISTS the select list is irrelevant, so * is harmless there

Common mistakes

  • Reading result columns by position after a SELECT *
  • Assuming SELECT * inside EXISTS is slower than SELECT 1

Follow-ups

  • What is a covering index?
  • What happens to a view built with SELECT * when a column is added to the base table?

Tips

  • Give a concrete failure, such as a new large column slowing every page
  • Mention EXISTS as the one place where the select list does not matter