I was called many times by Jr. Programmers in team to debug their SQL. I keep log of most of the problems and review them afterwards. This helps me to evaluate my team and identify most important next thing which I can do to improve the performance and productivity of it. Recently we have many new hires and they had almost similar questions. Since, I have send them following sequence of the SELECT clause I am not interrupted often, which helps me to focus on larger project architectural design.
SELECT yourcolumns
FROM tablenames
JOIN tablenames
WHERE condition
GROUP BY yourcolumns
HAVING aggregatecolumn condition
ORDER BY yourcolumns
Once above order is memorized it guides the basic construction of SELECT statement. This article is written keeping in mind beginners in SQL Server language and presents very higher level outline of SELECT statement.
Reference : Pinal Dave (http://blog.SQLAuthority.com)












Your site has won a Blog of the Day Award (BOTDA)
Award Code
Thank you,
famous quotes
Hi Pinal
I hav a question,
Why we use group by clause before order by clause.
Ashutosh
How Can we generate incremented linear number in sql server
as in oracle we genaerate in via sequence …
I want the o/p like–
1
2
3
4
5
6
7
8
9
[...] a day ago, I received interesting question on this blog. Read original question here. This is very good question and after reading this question I quickly wrote small script as answer. [...]
Hi Ashutosh Gupta,
Once we order something, we can not group them as the group which we desire may not be together. It only make sense to group them first and then order them. This way similar data will be grouped and only distinct data/grouped data is properly ordered.
Regards,
Pinal Dave ( http://www.SQLAuthority.com )
hi ,Pinal
i didnt understand
how to debugging the stored procedures in sqlserver2005
Please give me answer
thnaks & Regards
Hi, pinal
how to use searchable keyword in asp.net with sql server
Please give me answer
thnaks & Regards
select row_number() over(partition by gender order by empname) as Srno,* from Employees
Row_Number Function have following syntax
Row_Number() Over([Partition by columnName] Order by columnName)
where Partition by is optional but Order by is compulsory.
use This code you can get the serial order
select row_number() over(partition by gender order by empname) as Srno,* from Employees
Row_Number Function have following syntax
Row_Number() Over([Partition by columnName] Order by columnName)
where Partition by is optional but Order by is compulsory.
Row_Number funciton returns sequence for each row based on Order by clause, if you mention partition by also row number will split rows based on partition by column and then generate the separate sequence numbers.
Wooow,great solution nagender. thanks alot,a have been looking for this solution for long time ago. Nice job.
[...] SQL SERVER – Easy Sequence of SELECT FROM JOIN WHERE GROUP BY HAVING ORDER BY [...]
Hi pinal,
I have to prepare a SSIS package which takes data from a table say a and then transfers data between two tables say b and c based on information of table a.
is it possible through SSIS?
if it is please tell me how it can be done?
is this in right order:
1. SELECT
2. FROM
3. JOIN
4. ON
5. WHERE
6. GROUP BY
7. HAVING
8. ORDER BY
It is also useful to know the order of evaluation in order to realise why, for example, column aliases are only usable in the ORDER BY clause and not the GROUP BY
(5) SELECT (6) DISTINCT
(1) FROM
(2) WHERE
(3) GROUP BY
(4) HAVING
(7) ORDER BY