We use cookies to enhance your experience on the site
CodeWorlds

Back to the Gate - SELECT, WHERE, ORDER BY

Before we reach for the hardest tricks, let us return to the fundamentals. Every great query begins with three simple tools: selection, filter and order.

SELECT - what we want to see

SELECT
names the columns, and
FROM
names the shelf we read from:

1SELECT tytul, cena FROM ksiazki;

WHERE - filtering rows

WHERE
lets through only the rows that meet a condition. We can use the operators
=
,
>
,
<
,
>=
,
<=
,
<>
:

1SELECT tytul, cena FROM ksiazki WHERE dostepna = 1;

This question returns only books that are on the shelf.

ORDER BY - sorting results

ORDER BY
arranges results by a chosen column.
ASC
is ascending order (the default),
DESC
is descending:

1SELECT tytul, cena FROM ksiazki ORDER BY cena DESC;

LIMIT - only the top

LIMIT
restricts the number of returned rows. Together with sorting it produces rankings:

1SELECT tytul, cena FROM ksiazki ORDER BY cena DESC LIMIT 3;

This query shows the three most expensive books in the Library. Filter, order and limit - the everyday tools of every scribe!

Go to CodeWorlds