Home > Ask the Oracle Experts > SQL Questions & Answers > What does SELECT 1 accomplish?
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

What does SELECT 1 accomplish?

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


>
QUESTION POSED ON: 17 October 2002
I have a rather simple question: What does "SELECT 1 FROM ..." accomplish? I am debugging a software program that is sending a "SELECT 1" statement to the database, and I am basically curious. Does it simply select one column from the record it finds?

>
EXPERT RESPONSE

Great question. No, it selects the same column from all the rows it finds!

I would be surprised to see a "SELECT 1" query all by itself as a stand-alone query. By itself, it's useless. Like all SELECT statements, it returns a result set, and in this case, the result set consists of a number of rows, where each row has exactly one column, and where the value of that column is an integer 1. In other words, there's a 1 in each row, and nothing else! The result set has as many rows as there are in the table being selected from, subject to WHERE conditions. Hence the result set, by itself, a table of 1s, is useless. The only meaningful information you can get from it is the number of rows, and that can be obtained a lot more efficiently by using COUNT(*).

More likely, you saw it in a subselect. SELECT 1 or SELECT * or SELECT NULL are constructions commonly used in an EXISTS subselect. In an EXISTS subselect, the database does not actually "retrieve" rows, and it does not always need to scan the entire result set for the subselect, because just one row will provide an answer. That answer is either TRUE or FALSE.

There's a somewhat contrived but illustrative example of an EXISTS subselect in one of my previous answers, How does WHERE EXISTS ( SELECT NULL... ) work? (22 February 2002).


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
SQL
IN list or series of OR conditions?
Connecting tables in a database
SQL query for co-authored books
Querying complex derived tables
SQL string functions
Changing a NULL column to NOT NULL
SQL for hourly totals for the last 48 hours
LEFT OUTER JOIN to a MIN/MAX row
Normalizing a crosstab table
Querying metadata and data at the same time

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice

HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2003 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts