0
0
MySQLquery~5 mins

ORDER BY single column in MySQL

Choose your learning style9 modes available
Introduction

We use ORDER BY to sort data in a table by one column. It helps us see information in order, like from smallest to largest or newest to oldest.

When you want to list students by their scores from highest to lowest.
When you need to see products sorted by price from cheapest to most expensive.
When you want to show events ordered by date, starting with the earliest.
When you want to organize a list of employees alphabetically by their last name.
Syntax
MySQL
SELECT column1, column2 FROM table_name ORDER BY column1 [ASC|DESC];

ASC means ascending order (smallest to largest). It is the default if you don't write it.

DESC means descending order (largest to smallest).

Examples
This sorts users by age in ascending order (youngest to oldest).
MySQL
SELECT name, age FROM users ORDER BY age;
This sorts products by price from highest to lowest.
MySQL
SELECT product_name, price FROM products ORDER BY price DESC;
This sorts events by date from earliest to latest.
MySQL
SELECT event_name, event_date FROM events ORDER BY event_date ASC;
Sample Program

This creates a table of employees, adds three rows, and then selects their names and salaries sorted from highest salary to lowest.

MySQL
CREATE TABLE employees (id INT, name VARCHAR(50), salary INT);
INSERT INTO employees VALUES (1, 'Alice', 5000), (2, 'Bob', 3000), (3, 'Charlie', 4000);
SELECT name, salary FROM employees ORDER BY salary DESC;
OutputSuccess
Important Notes

You can ORDER BY any column in the table, whether or not it appears in the SELECT list.

If two rows have the same value in the ordered column, their order is not guaranteed unless you add more columns to ORDER BY.

Summary

ORDER BY sorts your query results by one column.

Use ASC for ascending (default) or DESC for descending order.

It helps organize data to find or compare values easily.