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
 precedence

Author  Topic 

ghostrider
Starting Member

11 Posts

Posted - 2013-02-10 : 07:56:33
Why is it necessary to precede group by before order by and not vice versa in a sql query??
as the foll code gives an error
select name from student order by age group by name;

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2013-02-10 : 08:11:50
Why do think about it?
Your question could also be: Why can't we write "FROM Student SELECT name"?
But it is clear: ORDER BY works on the resultset so it should be at the end of the statement.


Too old to Rock'n'Roll too young to die.
Go to Top of Page

ghostrider
Starting Member

11 Posts

Posted - 2013-02-10 : 08:48:48
Thank you @webfred!!!
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-02-10 : 11:31:36
Another way to look at it is that, that is just the way the syntax is. In LINQ queries, the syntax starts with the from clause as WebFred suggested. Doesn't matter how the server processes the query or how the physical processing takes place, it is just that the syntax rules require it.

If you are interested in the logical query processing phases, sqlauthority.com has a brief description http://blog.sqlauthority.com/2009/04/06/sql-server-logical-query-processing-phases-order-of-statement-execution/ As suggested in that article, a more detailed explanation is available in the the book "Inside Microsoft SQL Server 2008: T-SQL Querying"
Go to Top of Page
   

- Advertisement -