SQL
Database Query
SELECT Statement
SQL Optimization
Data Retrieval

What does SELECT 1 FROM table mean in SQL?

Codemia Knowledge Hub ·

SELECT 1 FROM table returns the constant value 1 for every row that the FROM and WHERE clauses produce. The 1 is not a column name and does not refer to the first column; it is a literal constant evaluated once per qualifying row. It is most often used inside EXISTS subqueries, where the only question is whether any matching rows exist.

What the query returns

Given a users table with four rows:

sql
SELECT 1 FROM users;

The result has four rows, each with a single unnamed column whose value is 1:

text
1 ?column?
2----------
3        1
4        1
5        1
6        1

The database still does all the FROM processing: it reads rows, applies WHERE filters, and honors JOIN conditions. Only the projection differs. So if the table has a million rows, you get a million rows of 1.

Any constant works. SELECT 42 FROM users, SELECT 'yes' FROM users, and SELECT NULL FROM users behave the same way; 1 is just the convention.

SELECT 1 inside EXISTS subqueries

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

EXISTS does not inspect the values the subquery returns. It only checks whether the subquery produces at least one row, so the projection does not matter: SELECT 1, SELECT *, and SELECT e.id give identical results. Writing SELECT 1 tells the reader the subquery is purely an existence check.

EXISTS versus COUNT(*) > 0

Both answer "is there at least one row?", but they do different amounts of work:

sql
1-- Counts ALL matching rows, then checks if the count is positive
2SELECT CASE
3    WHEN COUNT(*) > 0 THEN 'yes'
4    ELSE 'no'
5END AS has_employees
6FROM employees
7WHERE department_id = 10;
8
9-- Stops as soon as one matching row is found
10SELECT CASE
11    WHEN EXISTS (SELECT 1 FROM employees WHERE department_id = 10) THEN 'yes'
12    ELSE 'no'
13END AS has_employees;

EXISTS can short-circuit: once one qualifying row is found, scanning stops. COUNT(*) must evaluate every qualifying row. Use EXISTS for yes-or-no questions and COUNT(*) only when you need the count.

ApproachScans All Matching RowsShort-CircuitsIntent
EXISTS (SELECT 1 ...)NoYesExistence check
COUNT(*) > 0YesNoCounting (repurposed)
SELECT 1 FROM ... LIMIT 1NoYesExistence check (alternative)

With most modern query optimizers, the difference is negligible for small result sets. For large tables with many matching rows, EXISTS can be significantly faster.

Other places SELECT 1 appears

Health checks and connectivity tests

A bare SELECT 1 with no FROM touches no table and just returns a constant. Connection pools and health check endpoints use it to exercise the connection with minimal overhead.

sql
SELECT 1;
java
// JDBC connection validation query
dataSource.setValidationQuery("SELECT 1");

Oracle releases before 23ai require SELECT 1 FROM DUAL because they do not support SELECT without a FROM clause; Oracle Database 23ai accepts the bare form. MySQL, PostgreSQL, SQL Server, and SQLite all accept the bare SELECT 1.

Conditional logic in stored procedures

sql
1IF (SELECT 1 FROM sys.tables WHERE name = 'audit_log') IS NOT NULL
2BEGIN
3    -- Table exists, proceed
4    INSERT INTO audit_log (event) VALUES ('startup');
5END

Guarding an INSERT against duplicates

sql
1INSERT INTO subscriptions (user_id, plan)
2SELECT 42, 'premium'
3WHERE NOT EXISTS (
4    SELECT 1
5    FROM subscriptions
6    WHERE user_id = 42
7);

The row is inserted only if no matching subscription exists.

Is SELECT 1 faster than SELECT * inside EXISTS?

No. Modern optimizers (PostgreSQL, MySQL, SQL Server, Oracle) recognize EXISTS subqueries and optimize them identically regardless of the projection. You can confirm with execution plans:

sql
1EXPLAIN ANALYZE
2SELECT d.name
3FROM departments d
4WHERE EXISTS (
5    SELECT 1 FROM employees e WHERE e.department_id = d.id
6);
7
8EXPLAIN ANALYZE
9SELECT d.name
10FROM departments d
11WHERE EXISTS (
12    SELECT * FROM employees e WHERE e.department_id = d.id
13);

Both plans show the same operations, because the optimizer does not materialize columns for an existence check. The performance difference that actually matters is EXISTS versus COUNT(*) > 0.

ExpressionInside EXISTSAs Standalone QueryReadability
SELECT 1Identical performanceReturns constant per rowClearly signals existence check
SELECT *Identical performanceReturns all columnsAmbiguous intent inside EXISTS
SELECT e.idIdentical performanceReturns one columnSlightly misleading inside EXISTS

Outside EXISTS, the three differ because the projection determines the result set.

Does the 1 refer to the first column?

No. This is the most common misunderstanding. Columns in the SELECT list are referenced by name, not position, and the 1 is a literal integer. There is nothing special about the syntax: any valid SQL expression can appear in the SELECT list.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design