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 edad ASC;

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



Exercises



Interactive Quiz 1: Drag and Drop.

Arrastra en el orden correspondiente.


Arrastra las opciones:

SELECT
FROM
ORDER BY
edad
ASC
*

Completa el código:

______
______
______
______
______
______

Interactive Quiz 2: Fill in the Blanks

Rellena los huecos en cada casilla.


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
1Juan25
2Ana30
3Luis22
4Marta35

* 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?