A short video covering the ORDER BY clause within a SQL SELECT statement.
For beginners. How to use the ORDER BY clause in the SELECT statement. Covers multiple sort keys, ascending, descending, how NULLs are handled, and best practice.Here is the SQL that goes with the demo.
-- Database by Doug -- Douglas Kline -- 9/16/2015 -- all about ORDER BY USE Northwind -- refresher -- the result of a SELECT statement is a table -- it has columns and rows, defined by the SELECT statement SELECT ProductName, ProductID, supplierID FROM Products WHERE unitprice > 10 -- the SELECT statement above creates a two-column, many-row table -- the SELECT *clause* defines the which columns are in the resulting table, and their order -- the WHERE clause defines which rows are in the resulting table -- refresher over... -- but what about the the *order* of the rows? -- is there a default ordering? SELECT ProductName FROM Products SELECT ProductName, ProductID FROM Products -- looking at the results, it seems like they are ordered by productID -- let's try the above in different orders of operation? -- changes in ordering of the rows, even though the actual records included do not change -- in short, the database engine will return the records in the most convenient order (for itself) -- it will NOT spend any extra time ordering rows for YOUR convenience, unless you ask -- so here's how you do it - the ORDER BY clause SELECT ProductName FROM Products WHERE unitprice > 10 ORDER BY ProductName ASC -- the ORDER BY clause always appears at the end of the SELECT statement -- the query above orders by the productname in ascending order -- the ordering is alphabetical, because ProductName is not numeric - it is character-based SELECT ProductName FROM Products WHERE unitprice > 10 ORDER BY ProductName DESC -- orders descending -- in these examples, productName is the sort key, used to order either ascending or descending -- by default, the ordering of a sort key is ascending SELECT ProductName FROM Products WHERE unitprice > 10 ORDER BY ProductName -- so the above and the below are equivalent SELECT ProductName FROM Products WHERE unitprice > 10 ORDER BY ProductName ASC -- another example, with numeric data SELECT ProductName, unitprice FROM Products ORDER BY unitprice DESC -- " show the products with the most expensive at the top" -- notice that the sort key does not need to be the first column -- furthermore, the sort key need not be in any other clause of the SELECT statement SELECT ProductName, unitprice FROM Products WHERE reorderlevel > 10 ORDER BY productID ASC -- note that each clause (SELECT, WHERE, ORDER BY) has different fields in it -- However, the above query can be diconcerting to users -- it is more common to have the sort keys in the SELECT clause, and in the same order as in the SELECT clause -- like this SELECT ProductID, ProductName, unitprice FROM Products WHERE reorderlevel > 10 ORDER BY productID ASC -- now the ordering is clear to the user -- it is also possible to use multiple sort keys SELECT SupplierID, ProductID, ProductName, unitprice FROM Products ORDER BY SupplierID ASC, ProductID ASC -- this means... -- sort first by the SupplierID, ascending -- then, when multiple records have the same SupplierID, sort by ProductID, ascending -- note also how NULLs are handled for numerics -- NULLs are considered to be smaller than numeric values SELECT SupplierID, ProductID, ProductName, unitprice FROM Products ORDER BY SupplierID DESC, ProductID ASC -- how about characters? SELECT QuantityPerUnit, ProductName FROM Products ORDER BY QuantityPerUnit -- again, NULLs are considered to be smaller than characters -- for SQL embedded in systems, you should always include an order by -- consumers of the SELECT results may assume an ordering and actually *depend* on it -- furthermore, it is best to have a *unique* ordering -- again, downstream systems may assume and *depend* on a unique ordering -- a non-unique ordering: SELECT ProductName FROM Products ORDER BY ProductName -- improvement to make the ordering unique: SELECT ProductName FROM Products ORDER BY ProductName, ProductID -- all about ORDER BY, the end :-)
No comments:
Post a Comment