Click here to Skip to main content
15,896,469 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am totally confused about group by and order by clause into sql server what is the way they work i am not getting clear till now ?

i we use more then one column into order by clause then how it should shortrecord based on two column ?

and how to use group by clause and what is the way in which record are grouped ?

please help me to clear these two big tension of my mind???
Posted

If you don't specify an ORDER BY clause, then SQL can return rows in any sequence that it finds convenient at the moment - which means that two sequential identical queries do not necessarily return the same data in the same order. ORDER BY tells SQL exactly how to return the data. This is handy when you SELECT records for display, and want users name in alphabetical sequence, for example. If you use more than one column, then the ordering is done by the first specified, then the second is applied to identical values in the first column, and so forth.

GROUP BY allows you to aggregate rows - so you can ask things like "how much has each salesman sold last month?"
SQL
SELECT SalesManName, SUM(SalesValue)
FROM SalesRecords
WHERE SaleDate BETWEEN '2015-09-01' AND '2015-09-30'
GROUP BY SalesManName

Do note that with GROUP BY that because it aggregates values you can't return any columns that are not listed in the GROUP BY clause list, unless they are in an aggregation function such as SUM, COUNT, and so forth, as that would require returning multiple values in the same row, and SQL doesn't do that!
 
Share this answer
 
Hi,

Check these...

Order By[^]

The following SQL statement selects all customers from the "Customers" table, sorted by the "Country" and the "CustomerName" column:

SQL
SELECT * FROM Customers ORDER BY Country, CustomerName;





Group By[^]



Hope these will help you.

Cheers
 
Share this answer
 
Please see the link below.

[^]

I hope it will help you to understand GROUP BY.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900