How we will Show selected records sorted in an ascending (asc) or descending (desc)

mysql> SELECT col1,col2 FROM tablename ORDER BY col2 DESC;

mysql> SELECT col1,col2 FROM tablename ORDER BY col2 ASC;

In MySQL, you can use the ORDER BY clause to sort selected records in either ascending (ASC) or descending (DESC) order. Here’s the basic syntax:

sql
SELECT column1, column2, ...
FROM table_name
ORDER BY column1 [ASC | DESC], column2 [ASC | DESC], ...;
  • If you want to sort in ascending order, you can omit ASC as it is the default:
sql
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ...;
  • If you want to sort in descending order, use DESC:
sql
SELECT column1, column2, ...
FROM table_name
ORDER BY column1 DESC, column2 DESC, ...;

Here’s an example to illustrate:

sql
-- Sorting in ascending order
SELECT *
FROM employees
ORDER BY last_name, first_name;

-- Sorting in descending order
SELECT *
FROM employees
ORDER BY salary DESC, hire_date;

In the above examples, replace employees, last_name, first_name, salary, and hire_date with your actual table and column names.