Tutorial by Examples

Using the Employees Table, below is an example to return the Id, FName and LName columns in (ascending) LName order: SELECT Id, FName, LName FROM Employees ORDER BY LName Returns: IdFNameLName2JohnJohnson1JamesSmith4JohnathonSmith3MichaelWilliams To sort in descending order add the DESC keywo...
Multiple fields can be specified for the ORDER BY clause, in either ASCending or DESCending order. For example, using the http://stackoverflow.com/documentation/sql/280/example-databases/1207/item-sales-table#t=201607211314066434211 table, we can return a query that sorts by SaleDate in ascending o...
If we want to order the data differently for per group, we can add a CASE syntax to the ORDER BY. In this example, we want to order employees from Department 1 by last name and employees from Department 2 by salary. IdFNameLNamePhoneNumberManagerIdDepartmentIdSalaryHireDate1JamesSmith1234567890NUL...
If you want to order by a column using something other than alphabetical/numeric ordering, you can use case to specify the order you want. order by Group returns: GroupCountNot Retired6Retired4Total10 order by case group when 'Total' then 1 when 'Retired' then 2 else 3 end returns: GroupCountTot...

Page 1 of 1