If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > What is Select 1 For?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-07-09, 15:58
rockdave35 rockdave35 is offline
Registered User
 
Join Date: Jan 2009
Posts: 43
What is Select 1 For?

Hey guys,

This may be a stupid question, but I can't figure out what the SELECT 1 is actually used for. For example, the query statement shows it here:


AND NOT EXISTS

(SELECT 1 from MBR_HIS where HIST_CD = '10')


I tried running the statement by itself and the display result is a bunch of 1's. Why not use SELECT * instead?
Reply With Quote
  #2 (permalink)  
Old 04-07-09, 16:06
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
What difference does it make? The subselect doesn't return anything and is only used for an existence check. Using "SELECT 1" is the typical convention for that. You can also use "SELECT *" (but that is usually frowned upon because you should always specify exactly what you want). DB2 will optimize the "*" away because executing it would be more expensive than just returning the constant 1.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #3 (permalink)  
Old 04-07-09, 17:12
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
It is a bad idea to use "select *" in any application program, even if the DB2 optimizer converts it to "select 1" or something similar.

In the old days, the DB2 optimizer did not convert "select *" to "select 1" in a "where exists" or similar query, so you ended up having DB2 actually process all those columns in the sub-query, which was not very efficient. In fact, there were serious debates at the time about whether "select '1' " was more efficient than "select 1" or "select <primary-key-column>". That was awhile ago.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #4 (permalink)  
Old 04-07-09, 17:39
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
instead of SELECT 1, i will often use either SELECT 937 or SELECT NULL

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 04-07-09, 20:56
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
Quote:
Originally Posted by r937
instead of SELECT 1, i will often use either SELECT 937 or SELECT NULL
Dogs and consultants like to leave to leave their mark so everyone knows they were there.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #6 (permalink)  
Old 04-07-09, 20:59
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by Marcus_A
Dogs and consultants like to leave to leave their mark so everyone knows they were there.
not everyone -- just those who, in the immortal words of mike wilbon, can "slurp the love"
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On