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:
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
id | name | age |
---|---|---|
1 | Juan | 25 |
2 | Ana | 30 |
3 | Luis | 22 |
4 | Marta | 35 |
* Escribe tu consulta SQL y ejecuta para ver los resultados.
Resultados:
No hay resultados disponibles.