Statements
SELECT
SELECT column1, column2, ...
FROM table_name;
SELECT * FROM table_name;
Filter with WHERE
SELECT firstname, birthday FROM customers WHERE lastname='Mueller';
- an equal value
=
or different value!=
- a higher value
>
(or higher than or equal to>=
), a lower value<
(or lower than or equal to<=
) - a value from several possible values with
IN
- a numerical value (or a date) from within a range
BETWEEN xx AND yy
- a chain starting with or ending with with
LIKE
and the%
joker - a value
IS NULL
orIS NOT NULL
Limit the result
SELECT <champs> FROM <table> LIMIT <nb_results>;
SELECT * FROM customer LIMIT 5 OFFSET 20;
Be careful: it is a common mistake to write LIMIT 25 OFFSET 20, assuming this will retrieve results 21 to 25, but it would actually return results 21 to 45!)
ORDER BY Sorting and filtering
SELECT firstname, lastname FROM customer ORDER BY lastname ASC, birthday DESC;
the youngest Memebers of Müller:
SELECT * FROM customer WHERE lastname='Müller' ORDER BY birthday DESC LIMIT 0,3;
A Challenge - try yourself
Top comments (0)