1
Reply

Group By & Order By In SQL SERVER

Rahul Saxena

Rahul Saxena

Apr 7 2008 3:24 AM
8.3k
GROUP BY is a way to sub-total your results, or perform some 
other "aggregate" function on them.

Example:

SELECT department, sum(salary)
FROM tblEmployee
GROUP BY department;

will give you salary totals by department, whereas the sum statement by 
itself would just give you the grand total of all salaries in 
tblEmployee.

ORDER BY is simply a way to sort your results - it does not affect what 
shows up in your resultset, only what order it is displayed.

Example:

SELECT *
FROM tblEmployee
ORDER BY lastname;

will give you all tblEmployee data, in order of last name.  If you want 
the results in descending (reversed) order, simply add DESC to the end 
of the clause:
ORDER BY lastname DESC;

Answers (1)