Ascending and Descending Order

Ascending and Descending Order

In SQL database management systems, database users can apply the ORDER BY SQL clause to sort the results of a table query in ascending or descending order. This allows database users to arrange table data columns in a particular order, either ascending or descending. The database sorting arrangement order depends on the specific order in which the database user wants to preview and display the table data.

Ascending and Descending Order

Ascending Database Table Order (ASC).

Remember that by default, the sorting order in SQL tables is defined as ascending, which arranges existing database data from smallest to largest (for numeric column values), from A to Z in alphabetical order (for text), and from the smallest integer value to the largest integer value in numeric order.

Ascending Database Order Syntax.

SELECT column_name(s) FROM table_name ORDER BY column_name ASC;

Ascending Database Order element.

  • Here, the ASC keyword means ascending order.
  • By default, ascending is the default behaviour.

Example of Ascending Database Order.

Here, the list of employees in the employee table is previewed in ascending order according to their age.

SELECT * FROM employee ORDER BY age ASC;

Result of employee table (sorted by age column).

employee_id       emp_name        emp_age     department        salary

101                  Siddhi Deora            21             Marketing           74000

201                  Harry Deora             23             Development     44000

301                  Bhavshi Deora         37             Design                63000

In this example, the employee table is sorted and displayed from the youngest age (21) to the oldest age (37).

Descending Database Table Order (DESC).

In SQL databases, the DESC keyword is used to sort the results of a database table record in descending order. This means it arranges the existing database table data from largest to smallest for numeric values ​​and in reverse alphabetical order (Z to A) for text characters.  Generally, the DESC database order arranges and previews table data opposite to the ASC order.

Syntax for Descending Database Table.

SELECT column_name(s) FROM table_name ORDER BY column_name DESC;

Descending Database Order element.

  • Here, DESC means descending order in the database table column arrangement.

Example of a Descending Database Table.

Here, the list of employees in the employee table is displayed in descending order according to their salary.

SELECT * FROM employee ORDER BY salary DESC;

Here is the result in the employee table (sorted by employee salary).

employee_id       emp_name        emp_age     department        salary

101                  Siddhi Deora            21             Marketing           74000

301                  Bhavshi Deora         37             Design                63000

201                  Harry Deora             23             Development     44000

In this example, the employees are sorted and previewed in descending order, from the employee with the highest salary (74,000) to the employee with the lowest salary (44,000).

Sorting Multiple Columns in an SQL Database Table.

In an SQL database management system, database users can sort and preview multiple table column data at once, where each database table column can be sorted and previewed in its own individual order.

Syntax for Sorting Multiple Columns.

SELECT column_name(s) FROM table_name ORDER BY column1 [ASC|DESC], column2 [ASC|DESC];

Example of Sorting Multiple Columns – Here, the database user has previewed the Employee table, sorted first by Employee Department in ascending order and then by Employee Age in descending order.

SELECT * FROM employee ORDER BY department ASC, age DESC;

Multiple Columns Result (Sorting preview by Employee Department and then Age).

employee_id       emp_name        emp_age     department        salary

201                  Harry Deora             23             Development     44000

301                  Bhavshi Deora         37             Design                63000

101                  Siddhi Deora            21             Marketing           74000

In this example,

  • In this employee table example, the employee department is sorted in ascending order and previewed.
  • Similarly, the employee age within each employee department is sorted in descending order and previewed.

Conclusion of Ascending and Descending database table Order.

  • Database ASC order (ascending order) – In the SQL database management system, table column data is sorted and previewed from the smallest number to the largest number (for numeric columns), and similarly, in alphabetical order from A to Z for character text values.
  • Database DESC order (descending order) – In the SQL database management system, table column data is sorted and previewed from the largest value to the smallest numeric value (for number columns), and in reverse alphabetical order from Z to A (especially for text).
  • Multiple database table column order – In the SQL database management system, multiple different table column data can be sorted and previewed according to multiple columns, where the database user can apply and preview different sorting methods for each table column.

Leave a Reply