SQL and MySQL course Β· Module 3: Filtering (WHERE)
Scriptorium mastered - summary
In this lesson3
Wonderful! You are leaving the Scriptorium a true master of filtering. You can now point the Library to exactly the scrolls you need - not a single one more.
What you have learned
- WHERE selects only the rows that satisfy a condition (always after
FROM). - Comparison operators:
=,<>,<,>,<=,>=. - We wrap text in single quotes
' ', but not numbers. - AND - all conditions at once; OR - one is enough; NOT - negation.
- Parentheses
( )order things when we mixANDandOR. - IN (...) - a list of allowed values (a shorter way of writing many
OR). - BETWEEN a AND b - a range together with both ends.
- LIKE with the characters
%(any sequence) and_(one character);NOT LIKEis the negation. - IS NULL / IS NOT NULL - checking for emptiness (never
= NULL!).
An example tying it all together
SELECT * FROM ksiazki
WHERE cena BETWEEN 20 AND 80
AND kategoria_id IN (1, 2)
AND tytul LIKE 'O%';This query will return books priced from 20 to 80 coins, from category 1 or 2, whose title starts with O. Three conditions in one - the full power of the Scriptorium!
What's next?
In the next location you will learn to order results (ORDER BY) and limit their number (LIMIT), so that from the filtered pile of scrolls you can pick the most important ones. You already have the filter - now it is time to give it order. Onward, deeper into the Library!
Spotted a mistake in this lesson?