Sorting Results with ORDER BY


  The ORDER BY command is fundamental for ordering the results of your SQL queries. It allows you to specify the order in which the data should be presented, whether in ascending or descending order.


Syntax of the ORDER BY command

The basic syntax is:
SELECT [columns] FROM [table] ORDER BY [column] [ASC|DESC];


Purpose of the Lesson

  The purpose of this lesson is to teach you how to use the ORDER BY command to sort results in SQL queries, allowing you to present data more clearly and organized.


Ascending or Descending. [ASC|DESC]


  To sort all clients by age in ascending order, you would use:
SELECT * FROM clientes ORDER BY YEAR ASC;

  To sort all clients by age in ascending order, you would use:
SELECT * FROM clientes ORDER BY YEAR DESC;



Exercises



Interactive Quiz 1: Drag and Drop.

Drag in the corresponding order.


Drag the options:

SELECT
FROM
ORDER BY
YEAR
ASC
*

Completa el código:

______
______
______
______
______
______

Interactive Quiz 2: Fill in the Blanks

Fill the Blanks.


SELECT  FROM  ORDER BY  ;

Practical Exercise:


Statement:

  Write the necessary Query (query) to display all clients from a table ordered by age.


Datos de Ejemplo

idnameage
1John25
2Anna30
3Kevin22
4Angela35

* Escribe tu consulta SQL y ejecuta para ver los resultados.

Resultados:

No hay resultados disponibles.


What is the SQL command to sort results by a specific column?