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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
SELECT 1 FROM TABLE_NAMEmeans, “Return 1 from the table”. It is pretty unremarkable on its own, so normally it will be used withWHEREand oftenEXISTS(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)).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_NAMEwill be used. This does not offer significant benefit over selecting an individual column, but, depending on implementation, it may offer substantial gains over doing aSELECT *, 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.