SQL
Database Query
SELECT Statement
SQL Optimization
Data Retrieval

What does it mean SELECT 1 FROM table?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

SELECT 1 FROM table means: return one literal value for every row the FROM clause produces. The 1 is not a column name, and it is not asking the database to look up a field called 1. It is simply a constant expression.

That is why the query is common in existence checks and subqueries. The point is often not the value 1 itself, but the fact that at least one row exists.

What The Query Actually Returns

Suppose you have a table named users with three rows. This query:

sql
SELECT 1 FROM users;

returns three rows, each containing the value 1:

text
1
1
1

The database still scans rows according to the FROM and WHERE clauses. The difference is that the selected expression is a constant instead of a column.

Why It Appears In EXISTS

The most common use is inside an EXISTS subquery:

sql
1SELECT name
2FROM departments d
3WHERE EXISTS (
4    SELECT 1
5    FROM employees e
6    WHERE e.department_id = d.id
7);

Here, SELECT 1 is just a compact way to say "I only care whether a matching row exists." The subquery does not need employee names or salaries. It only needs to prove that at least one employee row matches.

In that context, SELECT 1, SELECT *, and SELECT e.id are usually equivalent in meaning because EXISTS cares about row existence, not returned column values.

It Is Not A Special Performance Trick By Itself

People sometimes assume SELECT 1 is magically faster than selecting a real column. In many databases, that is not the important distinction. The optimizer usually understands that an EXISTS subquery stops when it finds a qualifying row, regardless of whether the projection is 1 or a column.

So the main advantage of SELECT 1 is clarity. It tells readers, "this subquery is about existence, not data retrieval."

Compare It With COUNT(*)

Developers sometimes use COUNT(*) > 0 to answer an existence question. That works, but it can do more work than necessary because counting means evaluating all qualifying rows.

sql
SELECT COUNT(*)
FROM employees
WHERE department_id = 10;

If you only need a yes-or-no answer, EXISTS is often the better expression of intent:

sql
1SELECT EXISTS (
2    SELECT 1
3    FROM employees
4    WHERE department_id = 10
5);

The second version communicates the real question directly.

Why Not SELECT *

Inside an EXISTS subquery, many teams prefer SELECT 1 simply because it makes intent obvious to human readers. The database usually cares about row existence, but the constant tells other developers that the subquery is not trying to project useful data.

Common Pitfalls

  • Thinking 1 refers to the first column in the table.
  • Assuming SELECT 1 always has unique performance benefits outside its query context.
  • Forgetting that SELECT 1 FROM table still returns one row per matching source row.
  • Using COUNT(*) when the real need is only to know whether any row exists.
  • Treating SELECT 1 as mysterious syntax instead of as a constant expression.

Summary

  • 'SELECT 1 FROM table selects a constant value for each row produced by the query.'
  • The 1 is a literal, not a column reference.
  • The pattern is most common inside EXISTS subqueries.
  • Its main value is clarity of intent, not magic speed by itself.
  • Use EXISTS when the real question is whether matching rows exist at all.

Course illustration
Course illustration

All Rights Reserved.