0
0
SQLquery~5 mins

ORDER BY single column in SQL

Choose your learning style9 modes available
Introduction
We use ORDER BY to sort data in a table by one column so it is easier to read or find what we want.
When you want to see a list of students sorted by their names alphabetically.
When you want to find the cheapest product by sorting prices from low to high.
When you want to see recent orders first by sorting dates from newest to oldest.
When you want to organize employees by their ID numbers in ascending order.
Syntax
SQL
SELECT column1, column2 FROM table_name ORDER BY column1 [ASC|DESC];
ASC means ascending order (smallest to largest or A to Z). It is the default if you don't write it.
DESC means descending order (largest to smallest or Z to A).
Examples
Sorts the list of student names alphabetically from A to Z.
SQL
SELECT name FROM students ORDER BY name;
Sorts product prices from highest to lowest.
SQL
SELECT price FROM products ORDER BY price DESC;
Shows orders starting with the newest date first.
SQL
SELECT order_id, order_date FROM orders ORDER BY order_date DESC;
Sample Program
This creates a fruits table, adds three fruits with prices, then lists them sorted by price from lowest to highest.
SQL
CREATE TABLE fruits (id INT, name VARCHAR(20), price DECIMAL(5,2));
INSERT INTO fruits VALUES (1, 'Apple', 0.99), (2, 'Banana', 0.59), (3, 'Cherry', 2.99);
SELECT name, price FROM fruits ORDER BY price ASC;
OutputSuccess
Important Notes
If you order by a column that has duplicate values, the order of those duplicates is not guaranteed.
You can only order by columns that appear in the SELECT list or exist in the table.
ORDER BY always comes at the end of the SELECT statement.
Summary
ORDER BY sorts your query results by one column.
Use ASC for ascending (default) or DESC for descending order.
It helps you organize data to find or compare information easily.