ORDER BY

If you do not specify ascending or descending then the ascending is the default
ORDER BY ASC | DESC 1,2,3 - refers to the order of columns in the corresponding SELECT. ASC is the default
You can type the actual column names
You should always use an ORDER BY clause when you use a "Top n" in order to indicate the sequence that defines what rows are first or top


Although the ASC keyword is used to sort values in Ascending order it is rarely used as the default is ascending order when no sort order is specified.
Never add an ORDER BY to a subquery


Sorting using Numbers


ORDER BY 1, 2, 3
This order is based on the order of the columns in the SELECT statement.


This works in both SQL Server and Access

SELECT     CompanyName 
           ,ContactName
           ,Country
FROM CUSTOMERS
ORDER BY 2 ASC
           ,1 DESC
           ,3


=--this works in both SQL Server and Access

SELECT     Country 
           ,COUNT(Country)
FROM CUSTOMERS
GROUP BY Country
HAVING Count(Country) >=5
ORDER BY Country DESC

--can also do this on date columns to find the last entered date in the table

SELECT     TOP 10   * 
FROM CUSTOMERS
ORDER BY CompanyName DESC


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext