We help IT Professionals succeed at work.

Select 1

TroyCrowe
TroyCrowe asked
on
280 Views
Last Modified: 2010-05-18
I am looking for an explanation for the following select statement.  My question is about "Select 1 (one)", what is the purpose of this?  There is not a column selected; a number is selected instead.
Comment
Watch Question

Chris MangusDatabase Administrator
CERTIFIED EXPERT

Commented:
Some process is looking for any value, or a true condition.

Can you describe where you've seen it used?
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
this is a "constant select", which will return 1 row with 1 column.
if this is in a select 1 [one], ... from table... all the rows will have the same value for that column

Commented:
I use "Select 1" sometimes right before testing @@rowcount in starting a loop. "Select 1" will give me 1 row so the while @@rowcount > 1 is true.

Commented:
If this is in an EXISTS(SELECT 1 FROM...) clause then the purpose is to find out if any rows are returned, it doesn't matter what is in the rows.
--
JimFive

Author

Commented:
Select 1 from media where albumid = @albumid and mediastatusid != 2 and explicitstate=2  this is the complete statement.  Does this mean what you mentioned above angellll?  So you are saying that only one row with one column will be returned.  Which column and what if the columns values are not all the same?
Top Expert 2007

Commented:
I have seen it in these kind of constructions:

select SomeColumn
from   SomeTable
where  exists
(      select  1
       from    AnotherTable)

So if you can return only one result (in a subquery) and the result doesn't matter, a fixed resultset will do. It will never violate the rule that a subquery can only return one row ...

Hope this helps ...
Chris MangusDatabase Administrator
CERTIFIED EXPERT

Commented:
As I and JimFive mentioned, this query is just trying to let you know that you do have 1 or more rows that meet the condition in your query clause.

Author

Commented:
It is in a Exist statement.  However, I still do not understand how you can have a select statement without selecting a column name
Chris MangusDatabase Administrator
CERTIFIED EXPERT

Commented:
It's valid ANSI-92 SQL syntax.  In plain english, your query is saying:

If I have an @albumid on file, and my mediastatusid is not equal to 2 and explicitstate=2 then just inform me that this is all true.

It isn't meant to return data, it's just meant to tell you that data exists.
Top Expert 2007
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
I understand that a condition is being met simply because of the exists statement.  I just have never seen a select statement that selected a number instead of a column and still do not have a complete understanding of the validity of this action.  I appreciate all of your help and believe that there are a couple of you who have helped so I will pass the points the various one whose answers were most helpful.
Chris MangusDatabase Administrator
CERTIFIED EXPERT

Commented:
You can SELECT any data.

You can do SELECT 'TroyCrowe' As FullName and it will run fine.  You're creating a constant value.

Author

Commented:
cmanqus.  Your answer is understood.  I understand that you can select a value and give it a value "as" a column name.  I just have never seen what I was asking about and it did not look as if it followed sql standards.  I am fairly knowledgeable of sql I just have never seen this before.  Thanks for you input.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.