0
0
MySQLquery~5 mins

ASC and DESC direction in MySQL

Choose your learning style9 modes available
Introduction
We use ASC and DESC to sort data in a list from smallest to largest or largest to smallest. This helps us find information quickly.
When you want to see a list of products from cheapest to most expensive.
When you want to find the newest orders by date.
When you want to list students by their scores from highest to lowest.
When you want to organize names alphabetically.
When you want to show the oldest events first.
Syntax
MySQL
SELECT column1, column2 FROM table_name ORDER BY column1 ASC;

SELECT column1, column2 FROM table_name ORDER BY column1 DESC;
ASC means ascending order (smallest to largest, A to Z, oldest to newest).
DESC means descending order (largest to smallest, Z to A, newest to oldest).
Examples
This lists products from the cheapest to the most expensive.
MySQL
SELECT name, price FROM products ORDER BY price ASC;
This shows the newest orders first.
MySQL
SELECT name, created_at FROM orders ORDER BY created_at DESC;
This lists students from highest to lowest score.
MySQL
SELECT student_name, score FROM scores ORDER BY score DESC;
This lists city names in alphabetical order.
MySQL
SELECT city FROM cities ORDER BY city ASC;
Sample Program
This creates a table of employees and shows their names and salaries sorted from highest to lowest salary.
MySQL
CREATE TABLE employees (
  id INT,
  name VARCHAR(50),
  salary INT
);

INSERT INTO employees VALUES
(1, 'Alice', 5000),
(2, 'Bob', 3000),
(3, 'Charlie', 7000);

SELECT name, salary FROM employees ORDER BY salary DESC;
OutputSuccess
Important Notes
If you do not write ASC or DESC, the default is ASC (ascending order).
You can sort by multiple columns by adding them separated by commas, e.g., ORDER BY column1 ASC, column2 DESC.
Sorting helps you find top or bottom values easily.
Summary
ASC sorts data from smallest to largest or A to Z.
DESC sorts data from largest to smallest or Z to A.
Use ORDER BY with ASC or DESC to organize your query results.