SQL ORDER BY ASC
The ASC keyword in SQL is used to sort the result set in ascending order. By default, SQL sorts data in ascending order if no direction is specified, but using ASC explicitly clarifies that the data should be sorted in increasing order. The ASC keyword is commonly used when sorting numerical values, text, or dates from lowest to highest. Sorting data in ascending order is useful when you want to see the smallest values first or the earliest dates, or to organize the data alphabetically.
Example scenarios where ASC is useful:
- Sorting employee salaries from lowest to highest.
- Displaying product names in alphabetical order.
- Sorting dates from the earliest to the latest.
Example
This query will sort the Price column in ascending order (smallest to largest).
Code Example
-- Sorting products by price in ascending order using ASC
SELECT Product, Price
FROM Orders
ORDER BY Price ASC;
Output
Product | Price |
---|---|
Tablet | 600.00 |
Phone | 800.00 |
Laptop | 1200.00 |
Explanation
- The query selects the Product and Price columns from the Orders table.
- The ORDER BY Price ASC sorts the result set by the Price column in ascending order.
This is useful when you want to list products or items from lowest to highest value, as demonstrated in the output.