SELECT *

Difficulty: Beginner

Question

Why is 'SELECT *' usually considered bad practice in production queries?

Answer

SELECT * returns every column whether or not the code needs it, which wastes I/O, memory and network bandwidth, blocks covering-index optimizations, and makes the application fragile when the table's columns change. In production code you should list the columns explicitly. It is still perfectly fine for ad-hoc exploration in a SQL console.

The performance costs

  • More bytes read and sent: a users table may hold a profile bio, a JSONB preferences blob and a resume text. Listing 20 users for a dropdown only needs id and name, yet SELECT * pulls everything across the network and into application memory.
  • Large values are expensive: in PostgreSQL, big text, JSONB and bytea values are stored out of line in TOAST. Selecting them forces extra reads and decompression even if the app ignores them.
  • No index-only scans: if an index covers (user_id, created_at, amount_paise), a query selecting just those columns never touches the table. SELECT * always needs the full row, so the covering index cannot help.
  • Worse sorting and hashing: wider rows mean sorts and hash joins spill to disk sooner because fewer rows fit in work_mem.
  • Waste multiplies at scale: an API endpoint called thousands of times a second turns a few unnecessary kilobytes per call into a real bandwidth and GC cost.

The correctness and maintenance costs

  • Schema changes break code: adding a column can break code that reads columns by position, INSERT INTO archive SELECT * FROM orders when the two tables drift, or views. In PostgreSQL a view defined with SELECT * expands to the column list at creation time, so new columns silently do not appear.
  • Duplicate column names in joins: SELECT * FROM orders JOIN users ... returns two id columns, and many drivers keep only the last one, quietly giving you the wrong id.
  • Accidental data exposure: a new password_hash or pan_number column suddenly flows into API responses if the code serializes the whole row.
  • Unclear intent: explicit columns document what the code depends on and make reviews easier.

When SELECT * is fine

ContextOK?
Ad-hoc queries in psql, DBeaver, a notebookYes
EXISTS (SELECT * FROM ...)Yes, the select list is ignored
COUNT(*)Yes, unrelated to column selection
Small CTE or subquery that the outer query narrowsUsually fine; the planner prunes unused columns
Application code, APIs, views, reportsAvoid

ORMs hide the same problem

ORMs default to fetching all columns (User.findAll(), Model.objects.all()). Use projections such as Prisma select, Django only() or values(), or SQLAlchemy load_only() on hot paths.

Code examples

Explicit columns enable an index-only scan

CREATE INDEX orders_user_idx ON orders (user_id) INCLUDE (amount_paise, status);

-- Bad: needs every column, so the table must be read
SELECT * FROM orders WHERE user_id = 42;

-- Good: answered from the index alone
SELECT amount_paise, status FROM orders WHERE user_id = 42;

-- Bad in joins: two "id" columns, driver may keep only one
-- SELECT * FROM orders o JOIN users u ON u.id = o.user_id;
SELECT o.id AS order_id, u.id AS user_id, u.name
FROM orders o JOIN users u ON u.id = o.user_id;

Listing columns lets the optimizer use the covering index and avoids ambiguous duplicate column names.

Projection in Prisma

// Fetches only what the dropdown needs, not bio or resume blobs
const users = await prisma.user.findMany({
  where: { collegeId: 12 },
  select: { id: true, name: true },
  take: 20,
});

ORM projections give the same benefit as explicit SQL columns and keep sensitive fields out of responses.

Key points

  • SELECT * reads and transfers columns the code does not use.
  • It prevents covering index (index-only) scans.
  • Large TOASTed text, JSONB and bytea columns make it much more expensive.
  • New or reordered columns can break code, INSERT ... SELECT and views.
  • Fine for ad-hoc exploration, EXISTS and COUNT(*); avoid in application code.

Common follow-up questions

  • Is SELECT COUNT(*) slower than SELECT COUNT(id)?
  • How do PostgreSQL views behave when a column is added to the underlying table?
  • How would you find the queries in production that fetch too much data?
  • What is TOAST in PostgreSQL?

Interview tips

  • Group your reasons into performance, correctness and security so the answer sounds structured.
  • Mention covering indexes specifically; it is the most technical reason and impresses interviewers.
  • Acknowledge where SELECT * is harmless, such as EXISTS subqueries, to show nuance.

Concepts covered

SELECT *, Column Projection, Covering Index, Network I/O, Schema Evolution

More Database Design questions