Ordering data

It is a common request showing data in a table ordered accordingly to some requirement. We can achieve that using the ORDER BY directive applied to the SELECT command.

For instance, say that we want to get names and prices of all the pastries with a price less than 3, and we want them ordered by name:

select name, price
from pastry
where price < 3
order by name;

The order is implicitely ascending (ASC) meaning from A to Z, and to 0 to 9.
If we want it backward we can specify DESC (descending) in the ORDER clause.

All pastries name and price, ordered from the most expensive to the cheapest:

select name, price
from pastry
order by price desc;


I'm reading Head First SQL, a fun book good to have an introduction to SQL - based on MySQL implementation.

No comments:

Post a Comment