+-+-+-+ Order by Columns that are Not in the SELECT List The ORDER BY clause accepts column aliases as a column to be ordered by. ORDER BY VendorId DESC, ProductName ASC, ProductPrice DESC You can mix each column with ascending and descending order. If later on we inserted another Left handed screwdriver, but at a different price, then the ProductPrice column’s ordering would take effect, due to the fact that there are duplicate values in the ProductName column. This is not to say that the third column will never have any effect. Therefore, the ordering of the first two columns impacted the results, but the ordering of the third column did not. The reason this column had no effect is because there were no duplicates in the previously sorted column. We can see that the prices are all in ascending order, despite our insistence that they should be in descending order. If we look at the ProductPrice column, we can see that this column had no effect on the sorting, even though we specified ProductPrice DESC. If not, any subsequent columns are irrelevant as far as further sorting goes. The second column only takes effect if there are duplicates in the first ORDER BY column. This is because, we nominated the ProductName as the second column in our ORDER BY clause, and we specified DESC for descending order. If we focus on the three rows with a VendorId of 1001, we can see that the previous example returned the product names in ascending order, but in this example we returned them in descending order. ORDER BY VendorId DESC, ProductName DESC, ProductPrice DESC It’s probably easier to demonstrate this with an example. This enables you to specify how the rows should be sorted when there are multiple rows with the same value in the first sorted column. You can specify multiple columns with which to sort by. To sort it in descending order, use DESC. So we could also write the query like this: SELECT When you use the ORDER BY clause, it defaults to ascending, so you can omit the ASC part if you wish. | 1004 | Bottomless Coffee Mugs (4 Pack) | 9.99 | | 1001 | Left handed screwdriver | 25.99 | | VendorId | ProductName | ProductPrice | Here’s an example to demonstrate the ORDER BY clause. You can also specify multiple columns for sorting. You can specify whether the results are sorted in ascending order or descending order. It allows you to specify the order in which the results are returned. The ORDER BY clause is commonly used in SQL to sort the results of a query.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |