SQL ORDER BY Keyword
The ORDER BY clause is used to sort the results of a query in ascending (default) or descending order.
SQL ORDER BY Syntax
SQL
x
1
SELECT column1, column2, ...
2
FROM table_name
3
ORDER BY column1 [ASC | DESC], column2 [ASC | DESC], ...;
SQL ORDER BY Default (Ascending) Example
This query selects all patients and sorts them by last_name in ascending order.
SQL
1
1
SELECT *
2
FROM Patients
3
ORDER BY last_name;

SQL ORDER BY ASC Example
This query selects all doctors and sorts them by specialty in ascending (A–Z) order.
SQL
1
1
SELECT *
2
FROM Doctors
3
ORDER BY specialty ASC;

SQL ORDER BY DESC Example
This query selects all appointments and sorts them by appointment_date in descending (latest first) order.
SQL
1
1
SELECT *
2
FROM Appointments
3
ORDER BY appointment_date DESC;
