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
 General SQL Server Forums
 New to SQL Server Programming
 First Row or Last Row?

Author  Topic 

jessiefun
Starting Member

35 Posts

Posted - 2010-11-10 : 01:04:46
There is a table we can call it "Table1"
it contains two columns - ID and Name
---------------------------------------
Run the below scripts

DECLARE @VAR1 INT, @VAR2 VARCHAR(10)
SELECT @VAR1 = ID, @VAR2 = Name
FROM dbo.Table1

SELECT @VAR1 AS AA, @VAR2 AS BB

Question: what's the result?
The first row or last row in Table1 will be returened?


Sachin.Nand

2937 Posts

Posted - 2010-11-10 : 01:12:53
Last one.

PBUH

Go to Top of Page

jessiefun
Starting Member

35 Posts

Posted - 2010-11-10 : 01:21:34
quote:
Originally posted by Sachin.Nand

Last one.

PBUH




Yeah, got it.
PUSH is the point.
Thanks Sachine
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-11-10 : 01:32:57
quote:
Originally posted by jessiefun

quote:
Originally posted by Sachin.Nand

Last one.

PBUH




Yeah, got it.
PUSH is the point.
Thanks Sachine



You are welcome.
Also an order by clause will change the value returned.

PBUH

Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2010-11-10 : 02:27:47
A row.

There's no guarantee (without an order by) what order the rows will be returned in and as a result there's no guarantee what values you'll get back. A table, by definition, is an unordered set of rows, so asking about the first or last row of the table is a meaningless concept.

If you want order, you must put an order by clause and only then can you talk meaningfully about order, first and last.

--
Gail Shaw
SQL Server MVP
Go to Top of Page

jessiefun
Starting Member

35 Posts

Posted - 2010-11-10 : 04:36:08
quote:
Originally posted by GilaMonster

A row.

There's no guarantee (without an order by) what order the rows will be returned in and as a result there's no guarantee what values you'll get back. A table, by definition, is an unordered set of rows, so asking about the first or last row of the table is a meaningless concept.

If you want order, you must put an order by clause and only then can you talk meaningfully about order, first and last.

--
Gail Shaw
SQL Server MVP



Hi Gail,
Thank you for your response.
And I know what you mean. If we want to get something in order, we should put them in order manualy.
While for this simple table - ID (Primary key) and another column "Name" and no other index. The result for the script above is definite. That's the last row.
We can see that as a Stack, the push and pop thing.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-11-10 : 05:12:14
No. As Gail stated, there's no guarantee.
If you want to say last row in this case then I agree with: last row from result set. Nothing else.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

jessiefun
Starting Member

35 Posts

Posted - 2010-11-10 : 05:29:30
quote:
Originally posted by webfred

No. As Gail stated, there's no guarantee.
If you want to say last row in this case then I agree with: last row from result set. Nothing else.


No, you're never too old to Yak'n'Roll if you're too young to die.


Can you please take a look at this post?
http://weblogs.sqlteam.com/tarad/archive/2004/09/14/2077.aspx

And there is one sentence: "The above trigger will only work for the last row in the inserted table."

Can you make some explanation? The same?
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-11-10 : 05:33:58
Yes. The same.
If Tara comes along the she will agree, I am sure.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

jessiefun
Starting Member

35 Posts

Posted - 2010-11-10 : 05:53:25
quote:
Originally posted by webfred

Yes. The same.
If Tara comes along the she will agree, I am sure.


No, you're never too old to Yak'n'Roll if you're too young to die.



Well, thank you then.
Although, I didn't know why?

Thanks, Jessie
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-11-10 : 05:58:22
quote:
A file is ordered and can be accessed by a PHYSICAL location, while a table is not. "first record", "last record", and "next n records" make sense in a file but not in a table.


See this thread: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=152748
Last post of jcelko



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-11-10 : 08:44:01
quote:
Originally posted by webfred

quote:
A file is ordered and can be accessed by a PHYSICAL location, while a table is not. "first record", "last record", and "next n records" make sense in a file but not in a table.


See this thread: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=152748
Last second post of jcelko



No, you're never too old to Yak'n'Roll if you're too young to die.


What happens if he replies to the post?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-11-10 : 09:03:57
I wonder how does mysql determine "first record" or "last record' using Limit clause?

PBUH

Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2010-11-10 : 09:16:13
quote:
Originally posted by jessiefun
And I know what you mean. If we want to get something in order, we should put them in order manualy.


No, if you want something in order, you need to specify and order by on the query that returns it

quote:
While for this simple table - ID (Primary key) and another column "Name" and no other index. The result for the script above is definite. That's the last row.


The result is not definite. In this case, in the tests that you did, that was the 'last' row in the returned resultset. That is not guaranteed. Without an Order By, SQL is under NO obligation to return the rows in any particular order. What appears as the last row today may not appear as the last row tomorrow

quote:
We can see that as a Stack, the push and pop thing.


A table is not a stack. A table is an unordered set.

--
Gail Shaw
SQL Server MVP
Go to Top of Page

jessiefun
Starting Member

35 Posts

Posted - 2010-11-15 : 01:07:47
quote:
Originally posted by madhivanan

quote:
Originally posted by webfred

quote:
A file is ordered and can be accessed by a PHYSICAL location, while a table is not. "first record", "last record", and "next n records" make sense in a file but not in a table.


See this thread: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=152748
Last second post of jcelko



No, you're never too old to Yak'n'Roll if you're too young to die.


What happens if he replies to the post?

Madhivanan

Failing to plan is Planning to fail


Yeah, Table is not like a stack.
And I agree that a table is an unordered set. While for this simple table, it's ordered according to its primary key, the unique clustered index. And actutually, there is a loopoing-work for this assignment (from the first to the last), therefore, the value we get is the last one.

Thanks a lot for all your guys' information.
Regards,
Jessie
Go to Top of Page
   

- Advertisement -