224

I have seen many queries with something as follows.

Select 1  
From table

What does this 1 mean, how will it be executed and, what will it return?

Also, in what type of scenarios, can this be used?

Bilesh Ganguly
  • 3,792
  • 3
  • 36
  • 58
Microsoft Developer
  • 5,229
  • 22
  • 93
  • 142

15 Answers15

212

select 1 from table will return the constant 1 for every row of the table. It's useful when you want to cheaply determine if record matches your where clause and/or join.

Sahil Muthoo
  • 12,033
  • 2
  • 29
  • 38
150

SELECT 1 FROM TABLE_NAME means, "Return 1 from the table". It is pretty unremarkable on its own, so normally it will be used with WHERE and often EXISTS (as @gbn notes, this is not necessarily best practice, it is, however, common enough to be noted, even if it isn't really meaningful (that said, I will use it because others use it and it is "more obvious" immediately. Of course, that might be a viscous chicken vs. egg issue, but I don't generally dwell)).

 SELECT * FROM TABLE1 T1 WHERE EXISTS (
     SELECT 1 FROM TABLE2 T2 WHERE T1.ID= T2.ID
 );

Basically, the above will return everything from table 1 which has a corresponding ID from table 2. (This is a contrived example, obviously, but I believe it conveys the idea. Personally, I would probably do the above as SELECT * FROM TABLE1 T1 WHERE ID IN (SELECT ID FROM TABLE2); as I view that as FAR more explicit to the reader unless there were a circumstantially compelling reason not to).

EDIT

There actually is one case which I forgot about until just now. In the case where you are trying to determine existence of a value in the database from an outside language, sometimes SELECT 1 FROM TABLE_NAME will be used. This does not offer significant benefit over selecting an individual column, but, depending on implementation, it may offer substantial gains over doing a SELECT *, simply because it is often the case that the more columns that the DB returns to a language, the larger the data structure, which in turn mean that more time will be taken.

Sergio Ivanuzzo
  • 1,820
  • 4
  • 29
  • 59
cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
  • And why "normally" with EXISTS? Do you have proof of *why*? – gbn Aug 24 '11 at 05:59
  • 8
    And a -1 for propagating the myth of `...EXISTS (SELECT 1...` – gbn Aug 24 '11 at 06:00
  • 2
    @Gbn No idea why I've seen it there far more often than anything else. I've just seen it there far more often. – cwallenpoole Aug 24 '11 at 06:01
  • @gbn Fair enough. I've added that as a comment in the answer itself. – cwallenpoole Aug 24 '11 at 06:02
  • Note: the reason I'm so keen is the same myth pops up in different ways: http://stackoverflow.com/questions/1221559/count-vs-count1/1221649#1221649 and http://stackoverflow.com/questions/3271455/whats-the-best-to-check-if-item-exist-or-not-select-countidor-exist/3271464#3271464 So much so, that RDBMS vendors have optimisations to check for these myths and correct them – gbn Aug 24 '11 at 06:23
  • @No'am Nope. Definitely viscous, that is far funnier. (Admittedly, it had been intended to be vicious) – cwallenpoole Aug 24 '11 at 07:08
  • 1
    `where exists` and `in` should be used differently. If you're querying a small table, say 100k records, and you're looking for the id in a big table, 200m using `in` will make the query take ages no matter what, whereas `where exists` could take seconds if the tables are properly indexed. – Ben Aug 24 '11 at 07:20
  • 2
    @Ben: IN and EXISTs will usually optimise to the same plan – gbn Aug 24 '11 at 08:06
  • @gbn; just done some double checking and you're correct in some circumstances; it appears as though if you using tables then they're identical. If it's a view it can have problems ( some of our views are disgusting but extremely speedy ). – Ben Aug 24 '11 at 09:39
  • @Ben: it depends on indexes pretty much. Views are trickier because they are simply macros that expend and you don't always know the internals – gbn Aug 24 '11 at 09:43
  • I have seen ppl using "SELECT 1 FORM table LIMIT 1" to test the existence of a table. – r0n9 Dec 10 '17 at 22:37
  • Yep https://stackoverflow.com/questions/6432178/how-can-i-check-if-a-mysql-table-exists-with-php/6432196#6432196 – cwallenpoole Dec 11 '17 at 10:50
  • One more usage I've recently found on my project is that they use it as a `HealthCheck`. When executing healthCheck API request against the service it will automatically execute `SELECT 1 LIMIT 1` against some table to see if DB/Cluster is available – rodikno Aug 03 '23 at 12:12
49

If you mean something like

SELECT * FROM AnotherTable
  WHERE EXISTS (SELECT 1 FROM table WHERE...) 

then it's a myth that the 1 is better than

SELECT * FROM AnotherTable
  WHERE EXISTS (SELECT * FROM table WHERE...) 

The 1 or * in the EXISTS is ignored and you can write this as per Page 191 of the ANSI SQL 1992 Standard:

SELECT * FROM AnotherTable
  WHERE EXISTS (SELECT 1/0 FROM table WHERE...) 
Drew Chapin
  • 7,779
  • 5
  • 58
  • 84
gbn
  • 422,506
  • 82
  • 585
  • 676
  • 1
    just verified that postgres respects this, `exists (select 1/0` works like a charm – alex Oct 26 '12 at 09:53
30

it does what it says - it will always return the integer 1. It's used to check whether a record matching your where clause exists.

Neville Kuyt
  • 29,247
  • 1
  • 37
  • 52
  • 25
    uhm - the question was "what does it mean", not "is this good practice". I agree it doesn't make a difference from a performance point of view - but it's a convention that developers use, presumably because 1 is commonly interpreted as "true". – Neville Kuyt Aug 24 '11 at 07:13
27

select 1 from table is used by some databases as a query to test a connection to see if it's alive, often used when retrieving or returning a connection to / from a connection pool.

Tom
  • 43,583
  • 4
  • 41
  • 61
13

The result is 1 for every record in the table. __

TiyebM
  • 2,684
  • 3
  • 40
  • 66
  • Not really, I tried to create an empty table, and execute the query, the result are six 1s. – r0n9 Dec 10 '17 at 22:40
11

To be slightly more specific, you would use this to do

SELECT 1 FROM MyUserTable WHERE user_id = 33487

instead of doing

SELECT * FROM MyUserTable WHERE user_id = 33487

because you don't care about looking at the results. Asking for the number 1 is very easy for the database (since it doesn't have to do any look-ups).

Robert Martin
  • 16,759
  • 15
  • 61
  • 87
11

Although it is not widely known, a query can have a HAVING clause without a GROUP BY clause.

In such circumstances, the HAVING clause is applied to the entire set. Clearly, the SELECT clause cannot refer to any column, otherwise you would (correct) get the error, "Column is invalid in select because it is not contained in the GROUP BY" etc.

Therefore, a literal value must be used (because SQL doesn't allow a resultset with zero columns -- why?!) and the literal value 1 (INTEGER) is commonly used: if the HAVING clause evaluates TRUE then the resultset will be one row with one column showing the value 1, otherwise you get the empty set.

Example: to find whether a column has more than one distinct value:

SELECT 1
  FROM tableA
HAVING MIN(colA) < MAX(colA);
onedaywhen
  • 55,269
  • 12
  • 100
  • 138
  • "Clearly, the SELECT clause cannot refer to any column" - Wouldn't it be more reasonable to select a bool expression: `SELECT MIN(colA) < MAX(colA) FROM tableA`? – Paul Spiegel Feb 26 '19 at 14:32
9

If you don't know there exist any data in your table or not, you can use following query:

SELECT cons_value FROM table_name;

For an Example:

SELECT 1 FROM employee;
  1. It will return a column which contains the total number of rows & all rows have the same constant value 1 (for this time it returns 1 for all rows);
  2. If there is no row in your table it will return nothing.

So, we use this SQL query to know if there is any data in the table & the number of rows indicates how many rows exist in this table.

Chronicler
  • 45
  • 4
mahbub_siddique
  • 1,755
  • 18
  • 22
5

If you just want to check a true or false based on the WHERE clause, select 1 from table where condition is the cheapest way.

3

I see it is always used in SQL injection,such as:

www.urlxxxxx.com/xxxx.asp?id=99 union select 1,2,3,4,5,6,7,8,9 from database;

These numbers can be used to guess where the database exists and guess the column name of the database you specified.And the values of the tables.

EmilyAvon
  • 37
  • 4
3

This means that You want a value "1" as output or Most of the time used as Inner Queries because for some reason you want to calculate the outer queries based on the result of inner queries.. not all the time you use 1 but you have some specific values...

This will statically gives you output as value 1.

DShah
  • 9,768
  • 11
  • 71
  • 127
0

it simple means that you are retrieving the number first column from table ,,,,means select Emply_num,Empl_no From Employees ; here you are using select 1 from Employees; that means you are retrieving the Emply_num column. Thanks

0

The reason is another one, at least for MySQL. This is from the MySQL manual

InnoDB computes index cardinality values for a table the first time that table is accessed after startup, instead of storing such values in the table. This step can take significant time on systems that partition the data into many tables. Since this overhead only applies to the initial table open operation, to “warm up” a table for later use, access it immediately after startup by issuing a statement such as SELECT 1 FROM tbl_name LIMIT 1

0

This is just used for convenience with IF EXISTS(). Otherwise you can go with

select * from [table_name]

Image In the case of 'IF EXISTS', we just need know that any row with specified condition exists or not doesn't matter what is content of row.

select 1 from Users

above example code, returns no. of rows equals to no. of users with 1 in single column