Member-only story
Why 1 Beats * in Certain SQL Queries (And When to Use it)
“1” might seem like just a simple number, but as we know in any programming language or the computing world, 1 plays a significant role. So, what’s its role in SQL, and why do so many developers use 1 in places like EXISTS or dynamic queries?
Whether it’s in subqueries, dynamic scripts, or as a placeholder, understanding how and why to use 1 can make your queries simpler and more efficient. Let’s explore how and when to use “1” in your SQL queries.
The Basics: What Is 1 in SQL
In SQL, 1
is a literal value, meaning it’s just a constant — it doesn’t reference any table columns. Think of it as a placeholder that fits perfectly into SQL’s syntax without causing any extra work. By using 1
, you’re not pulling unnecessary data from tables; you’re simply satisfying the query’s structure, keeping things efficient and clean. Let’s go through a few examples, starting with the most simple one where we will select a constant value without using a table.
SELECT 1 AS constant_value;
In this query, we’re selecting the constant value1
and labelling it as constant_value. No tables are involved, and no data is being fetched — it’s just the number 1
, making it a lightweight and efficient way to generate a result. It may seem simple, but it has practical uses in…