Tutorial by Examples

In this example, we can use GROUP BY not only determined the sort of the rows returned, but also what rows are returned, since we're using TOP to limit the result set. Let's say we want to return the top 5 highest reputation users from an unnamed popular Q&A site. Without ORDER BY This query ...
SELECT DisplayName, JoinDate, Reputation FROM Users ORDER BY JoinDate, Reputation DisplayNameJoinDateReputationCommunity2008-09-151Jeff Atwood2008-09-1625784Joel Spolsky2008-09-1637628Jarrod Dixon2008-10-0311739Geoff Dalgas2008-10-0312567
You can use a column's number (where the leftmost column is '1') to indicate which column to base the sort on, instead of describing the column by its name. Pro: If you think it's likely you might change column names later, doing so won't break this code. Con: This will generally reduce readabilit...
Due to logical query processing order, alias can be used in order by. SELECT DisplayName, JoinDate as jd, Reputation as rep FROM Users ORDER BY jd, rep And can use relative order of the columns in the select statement .Consider the same example as above and instead of using alias use the relat...
To sort this table Employee by department, you would use ORDER BY Department. However, if you want a different sort order that is not alphabetical, you have to map the Department values into different values that sort correctly; this can be done with a CASE expression: NameDepartmentHasanITYusufHR...

Page 1 of 1