← Zurück zum Kurs: SQL and databases from scratch

LIKE, IN, BETWEEN and the mysterious NULL

Video folgt in Kürze

Besides the basic operators, SQL offers shortcuts for the most common filters: searching text by pattern (LIKE), comparing with a list of values (IN) and checking whether a number or a date is inside a range (BETWEEN). And there is one concept that confuses almost everyone at first: NULL, the absence of a value. Understanding it well now will save you hours of queries that return fewer rows than expected.

LIKE: searching by pattern

LIKE compares a text with a pattern that uses two wildcards: the percent sign (%) stands for any number of characters, even none, and the underscore (_) stands for exactly one character. So 'M%' finds everything that starts with M; '%Mouse' what ends in Mouse; '%top%' what contains top anywhere. In PostgreSQL LIKE is case sensitive; there is ILIKE, specific to PostgreSQL, which is not. In MySQL and SQLite LIKE already ignores case for basic letters.

sql
-- Products whose name starts with 'M'
SELECT name FROM products WHERE name LIKE 'M%';

-- Names that contain the word 'Desk' or 'Chair'... using two patterns
SELECT name FROM products WHERE name LIKE '%Desk%' OR name LIKE '%Chair%';

-- Customers whose email has exactly 3 letters before the @ (underscore = one character)
SELECT name, email FROM customers WHERE email LIKE '___@%';

IN and BETWEEN

IN replaces a chain of OR on the same column. WHERE city IN ('Madrid', 'Barcelona') is equivalent to city = 'Madrid' OR city = 'Barcelona', but shorter and more readable. BETWEEN a AND b checks a range including both ends: price BETWEEN 20 AND 60 equals price >= 20 AND price <= 60. Be careful with dates that include a time, because the final end may leave out part of the last day; with DATE, as in our shop, there is no problem.

sql
SELECT name, city FROM customers WHERE city IN ('Madrid', 'Barcelona');

SELECT name, price FROM products WHERE price BETWEEN 20 AND 60;

SELECT order_id, order_date FROM orders
WHERE order_date BETWEEN '2024-02-01' AND '2024-03-31';

NULL: the absence of a value

NULL is not zero or an empty string: it means unknown or not applicable. Felipe has no registered email; we do not know what it is. That is why any comparison with NULL (equal, different, greater) gives neither true nor false but unknown, and WHERE only lets true rows through. The practical result: WHERE email = NULL never returns anything. To check whether a value is missing you must use IS NULL, and IS NOT NULL for the opposite.

sql
-- WRONG: returns no rows, ever
SELECT name FROM customers WHERE email = NULL;

-- Right
SELECT name FROM customers WHERE email IS NULL;
SELECT name FROM customers WHERE city IS NOT NULL;

-- Trap: customers NOT in Madrid does not include Hugo (his city is NULL)
SELECT name FROM customers WHERE city <> 'Madrid';

To fix everything in your mind, think of these operators as readable shortcuts. LIKE replaces text comparisons that cannot be written with the equals sign; IN replaces several comparisons joined with OR; BETWEEN replaces two comparisons joined with AND; and IS NULL exists because NULL cannot be compared with the equals sign. When a query does not return what you expect, first check whether NULL values are involved, because they are the most frequent cause of rows that vanish without any error.

✎ Meine Notizen

Nur in diesem Browser gespeichert. Melde dich an, um es im Konto zu behalten.0/5000 Zeichen

← ZurückWeiter →