We use cookies to enhance your experience on the site
CodeWorlds

ORDER BY - arranging the scrolls

A raw shelf returns scrolls in no particular order. ORDER BY lets you arrange them - from oldest, from cheapest, alphabetically - however your heart desires.

Ascending and descending

By default the sorting is ascending (

ASC
). If you give no direction, the Library will still arrange the scrolls ascending:

1SELECT tytul, rok_wydania
2FROM ksiazki
3ORDER BY rok_wydania ASC;

To reverse the order - from largest to smallest - use

DESC
:

1SELECT tytul, rok_wydania
2FROM ksiazki
3ORDER BY rok_wydania DESC;

This query shows the books newest first. And this is how you arrange books cheapest first:

1SELECT tytul, cena
2FROM ksiazki
3ORDER BY cena ASC;

Where does ORDER BY stand?

Always after FROM, near the end of the query:

1SELECT imie, nazwisko FROM autorzy ORDER BY nazwisko ASC;

ORDER BY is the scribe's hand that tidies scattered scrolls upon the shelf. In the next lesson you will learn to sort by several fields at once.

Go to CodeWorlds