The WHERE clause filters rows that meet specific conditions in a MariaDB SELECT statement.
The WHERE clause narrows a SELECT result set to only rows meeting one or more conditions. MariaDB evaluates the clause after reading the table but before returning data, making queries faster and easier to read.
Use column = value to match a single value exactly. String literals need single quotes, while numbers do not.
SELECT *
FROM Customers
WHERE email = 'alice@example.com';
Combine expressions with AND and OR. Use parentheses to control evaluation order for complex logic.
SELECT *
FROM Orders
WHERE customer_id = 15
AND total_amount > 100
OR (order_date >= '2024-01-01' AND total_amount <= 50);
LIKE supports % (any length) and _ (single-char) wildcards. ILIKE provides case-insensitive matching when running with the appropriate collation.
SELECT name
FROM Products
WHERE name LIKE '%phone%';
Use BETWEEN … AND for inclusive ranges or comparison operators (<, <=, >, >=) for open ranges.
SELECT *
FROM Orders
WHERE order_date BETWEEN '2024-04-01' AND '2024-04-30';
LIMIT n returns up to n rows; combine with OFFSET to paginate results.
SELECT *
FROM Products
WHERE stock > 0
ORDER BY name
LIMIT 10 OFFSET 20;
Create indexes on columns used in frequent WHERE clauses, avoid functions on indexed columns, and prefer parameterized queries to prevent SQL injection.
Yes. MariaDB applies WHERE filtering before projecting columns, so calculated columns in SELECT cannot be referenced in WHERE.
No. Use HAVING to filter after GROUP BY when working with aggregates like COUNT() or SUM().
Absolutely. IN (subquery) or EXISTS (subquery) are common patterns for filtering against related data.