Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 What is "select top 1"

Author  Topic 

macsterling
Yak Posting Veteran

56 Posts

Posted - 2008-12-17 : 20:56:58
I have read some Google searches on it and am not sure I understand. The code I have says "select top 1 column1 from table1 where column2 is null. What is it asking?

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2008-12-17 : 23:09:54
u will get the top 1 record of column1 from table1 when column2 is having null value

col1 col2
1 null
2 5
3 null
4 78


o/p u will get is 1
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-12-17 : 23:10:05
It is saying to return 1 row that meets the condition.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-12-17 : 23:10:41
quote:
Originally posted by bklr

u will get the top 1 record of column1 from table1 when column2 is having null value

col1 col2
1 null
2 5
3 null
4 78


o/p u will get is 1



Without an ORDER BY, you could get col1 = 3.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2008-12-17 : 23:11:43
quote:
Originally posted by tkizer

quote:
Originally posted by bklr

u will get the top 1 record of column1 from table1 when column2 is having null value

col1 col2
1 null
2 5
3 null
4 78


o/p u will get is 1



Without an ORDER BY, you could get col1 = 3.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog




ok just i given a sample data to understand the query
Go to Top of Page

macsterling
Yak Posting Veteran

56 Posts

Posted - 2008-12-18 : 05:41:30
Having "1" as the actual value in the sample table is a little confusing. So does top 1 mean the first "1" (not necessarily row1 but the first you encounter - should the example have had the first null in col2 not in the first row)

select top 1 column1
from table1
where column2 is null

does below give "jones"

col1 col2
-------------
smith 2222
jones null
rae 3434
tall 8834
short null
left 4563
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2008-12-18 : 05:49:36
ur r getting the 1st record of col1 of urtable where col2 is having null
top 1 means to get the top record in the query

if ur want short as output, then use
select top 1 col1
from urt
where col2 is null order by col1 desc
Go to Top of Page

Jai Krishna
Constraint Violating Yak Guru

333 Posts

Posted - 2008-12-18 : 05:52:04
Yes It Gives "jones" as output because it is the first record it encountered

Jai Krishna
Go to Top of Page

NeilG
Aged Yak Warrior

530 Posts

Posted - 2008-12-18 : 06:02:52
Why will you get the third id if you don't use an order by tara, I though that the top 1 clause would return just the first record

Not questioning you but just interested for my own knowledge
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2008-12-18 : 06:12:19
quote:
Originally posted by NeilG

Why will you get the third id if you don't use an order by tara, I though that the top 1 clause would return just the first record

Not questioning you but just interested for my own knowledge



top clause will give top record from the query
Go to Top of Page

macsterling
Yak Posting Veteran

56 Posts

Posted - 2008-12-18 : 07:31:56
Thanks all. I couldn't find documentation that clearly explained it.
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2008-12-18 : 07:35:31
quote:
Originally posted by macsterling

Thanks all. I couldn't find documentation that clearly explained it.



welcome
did u clarify ur doubt on top clause
Go to Top of Page

macsterling
Yak Posting Veteran

56 Posts

Posted - 2008-12-18 : 07:44:54
Yes - Not exactly sure why "they" coded it this way in this context, but now I know what they are doing, if not why!
Go to Top of Page

soorajtnpki
Posting Yak Master

231 Posts

Posted - 2008-12-18 : 07:47:02
quote:
Originally posted by NeilG

Why will you get the third id if you don't use an order by tara, I though that the top 1 clause would return just the first record

Not questioning you but just interested for my own knowledge



hi neil,
me too have the same doubt...
anybody please calrify....

tanx in advance...
Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2008-12-18 : 08:15:08
Because SQL SERVER does not store the data the way you think it does. It may encounter the record that has '3' in it first, REGARDLESS of what order you input the data into the database. This is why you use an ORDER BY clause.

[Signature]For fast help, follow this link:
http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspx
Learn SQL or How to sell Used Cars
For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-12-18 : 12:21:24
quote:
Originally posted by NeilG

Why will you get the third id if you don't use an order by tara, I though that the top 1 clause would return just the first record

Not questioning you but just interested for my own knowledge



It just depends which row it finds first. See DonAtWork's response for more info.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

soorajtnpki
Posting Yak Master

231 Posts

Posted - 2008-12-18 : 23:36:25
quote:
Originally posted by DonAtWork

Because SQL SERVER does not store the data the way you think it does. It may encounter the record that has '3' in it first, REGARDLESS of what order you input the data into the database. This is why you use an ORDER BY clause.

[Signature]For fast help, follow this link:
http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspx
Learn SQL or How to sell Used Cars
For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp



ok tanx for your reply,
then see table structure below.
col1 col2
1 null
2 5
3 null
4 78

so does it means col1=1 is not the top record for the case col2=null
so does it means select top 1* gives a random record to us.

am i right?

tanx in advance
Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2008-12-19 : 07:22:32
Not exactly random. It will give you the FIRST record it encounters, not a random one. To insure you get the exact order you want, you MUST use an ORDER BY clause.

[Signature]For fast help, follow this link:
http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspx
Learn SQL or How to sell Used Cars
For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

NeilG
Aged Yak Warrior

530 Posts

Posted - 2008-12-19 : 07:59:43
Cheers guys
Go to Top of Page
   

- Advertisement -