MariaDB WHERE: Filter Rows by Values, NULL, and Dates

MariaDB’s WHERE clause keeps rows only when its condition is true. It excludes both false and NULL results. If you expected different rows, project the same condition in the SELECT list to see how it evaluates for each row.

Compare predicate values with the filtered rows

Run the setup SQL in a disposable learning database. Create the database beforehand and use an account with CREATE, INSERT, and SELECT privileges. The final exercise also requires UPDATE. If where_demo already exists, use a different practice environment.

USE learning;
CREATE TABLE where_demo (
    id INT PRIMARY KEY,
    category CHAR(1) NOT NULL,
    name VARCHAR(40) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
    price INT NULL,
    active TINYINT NOT NULL,
    created_at DATETIME(6) NOT NULL
) ENGINE=InnoDB;

INSERT INTO where_demo VALUES
    (1, 'A', 'Alpha', 10, 1, '2026-01-01 00:00:00'),
    (2, 'B', 'Beta_1', 20, 0, '2026-01-31 12:00:00'),
    (3, 'A', 'Gamma', NULL, 0, '2026-02-01 00:00:00'),
    (4, 'B', 'Delta', 30, 1, '2026-01-15 09:00:00'),
    (5, 'C', '100% item', 0, 0, '2026-01-31 23:59:59.999999'),
    (6, 'C', 'Alpha2', NULL, 1, '2026-01-10 08:00:00');
SELECT id, price, price >= 20 AS meets_condition
FROM where_demo
ORDER BY id;

SELECT id, price FROM where_demo
WHERE price >= 20
ORDER BY id;

SELECT 2 AS nonzero_value WHERE 2;

For IDs 1 through 6, the first query reports 0, 1, NULL, 1, 0, NULL in meets_condition. Putting that expression in WHERE keeps only 2 and 4. The last query returns 2 because a nonzero numeric condition is true. Place WHERE after FROM and before GROUP BY or ORDER BY. Use ORDER BY with LIMIT when the order of the selected rows matters. MariaDB SELECT syntax

Group AND and OR with parentheses

To get active rows in either category A or category B, group the category tests so active = 1 applies to the whole group. Compare the two queries below.

SELECT id FROM where_demo
WHERE category = 'A' OR category = 'B' AND active = 1
ORDER BY id;

SELECT id FROM where_demo
WHERE (category = 'A' OR category = 'B') AND active = 1
ORDER BY id;

The first query returns 1, 3, and 4. The second returns 1 and 4. AND binds more tightly than OR, so the first query includes inactive row 3 from category A. Use the keyword OR to avoid the alternate meaning of || in SQL modes that treat it as concatenation. MariaDB operator precedence

Match lists, ranges, and text patterns

Use IN for a list of values, BETWEEN for an inclusive range, and LIKE for text patterns.

SELECT id FROM where_demo WHERE price BETWEEN 10 AND 20 ORDER BY id;

SELECT id FROM where_demo
WHERE category IN ('A', 'C') AND price IS NOT NULL
ORDER BY id;

SELECT id FROM where_demo WHERE name LIKE 'Alpha%' ORDER BY id;

SELECT id FROM where_demo WHERE name LIKE '%!_%' ESCAPE '!' ORDER BY id;

SELECT id FROM where_demo WHERE name LIKE '%!%%' ESCAPE '!' ORDER BY id;

The queries return (1, 2), (1, 5), (1, 6), (2), and (5), respectively. With LIKE, % matches zero or more characters and _ matches one. With ESCAPE '!', !_ and !% match a literal underscore and percent sign. The example uses the case-sensitive utf8mb4_bin collation. Check the actual column or expression collation if the matches differ. MariaDB LIKE pattern rules

Handle NULL and NOT IN

Use IS NULL to find missing values. An ordinary comparison with NULL yields NULL because the value is unknown.

SELECT id FROM where_demo WHERE price IS NULL ORDER BY id;

SELECT id FROM where_demo WHERE price = NULL ORDER BY id;

SELECT id FROM where_demo WHERE price NOT IN (10, 20) ORDER BY id;

SELECT id FROM where_demo WHERE price NOT IN (10, NULL) ORDER BY id;

SELECT id FROM where_demo
WHERE price IS NULL OR price NOT IN (10, 20)
ORDER BY id;

The first query returns 3 and 6, and the second returns no rows. NOT IN (10, 20) returns 4 and 5, which both have known prices. NOT IN (10, NULL) returns no rows because the condition is never true. To include missing prices, add a separate IS NULL test as in the final query. If an exclusion list comes from a subquery, filter out its NULL values or consider NOT EXISTS if it matches the intended rule. MariaDB IN and NOT IN with NULL

Filter a whole month and filter groups

For all of January, use an inclusive start at January 1 and an exclusive end at February 1. In an aggregate query, WHERE chooses the input rows and HAVING chooses the resulting groups.

SELECT id FROM where_demo
WHERE created_at >= '2026-01-01 00:00:00'
  AND created_at <  '2026-02-01 00:00:00'
ORDER BY id;

SELECT category, COUNT(*) AS known_price_rows, SUM(price) AS total_price
FROM where_demo
WHERE price IS NOT NULL
GROUP BY category
HAVING COUNT(*) >= 2
ORDER BY category;

The date query returns 1, 2, 4, 5, and 6. It includes row 5 at 23:59:59.999999 and excludes row 3 at midnight on February 1. A date-only upper bound in BETWEEN can omit the hours after midnight on the last day. For TIMESTAMP columns, align the session time zone with the intended business period. MariaDB BETWEEN date examples The aggregate query returns B, 2, 50. After filtering for known prices, B is the only group with two or more rows. To filter a calculated column by its alias, you can expose it from a derived table and use the alias in the outer query.

Preview the rows before an UPDATE

An UPDATE or DELETE without WHERE can target every row. Run a SELECT with the same condition first and inspect the IDs and count. Keep this InnoDB practice example in one connection.

SELECT id, active FROM where_demo WHERE id = 2;

START TRANSACTION;
UPDATE where_demo SET active = 1 WHERE id = 2;
SELECT id, active FROM where_demo WHERE id = 2;

ROLLBACK;
SELECT id, active FROM where_demo WHERE id = 2;

The three queries return (2, 0), (2, 1), and (2, 0) in order. Other sessions may change data after your preview, so verify the conditions at the point of the actual change. Bind application input values as parameters in prepared statements. Use an allowlist for structural parts such as column names. To assess filter performance, inspect the execution plan with representative data and indexes. MariaDB UPDATE syntax