SQL ยท Chapter 5 of 42

SQL WHERE

WHERE filters rows using conditions. Combine multiple conditions with AND, OR and NOT.

Operators: `=`, `<>` (not equal), `>`, `<`, `>=`, `<=`, `BETWEEN`, `LIKE`, `IN`, `IS NULL`.

Syntax
SELECT ... FROM table WHERE condition;

Combining conditions

Use AND / OR / NOT. Parentheses control precedence.

Not equal

SQL uses `<>` (standard) or `!=` (many dialects) for 'not equal'.

Example 1 (sql)
SELECT * FROM users WHERE age >= 18 AND country = 'India';
Output
name | age | country
Ana  | 25  | India

Two conditions combined with AND.

Example 2 (sql)
SELECT * FROM products WHERE price BETWEEN 100 AND 500;
Output
product   | price
Book      | 250
Headphone | 499

BETWEEN is inclusive on both ends.

Key points

  • WHERE filters rows before grouping.
  • Combine with AND / OR / NOT.
  • Use `BETWEEN`, `IN`, `LIKE`, `IS NULL` for common patterns.
  • Use `<>` for 'not equal'.
๐Ÿ’ก Note: `WHERE col = NULL` NEVER matches. Use `WHERE col IS NULL` instead.

๐Ÿ“ Quick Quiz

1. The 'not equal' operator in standard SQL is:

2. `WHERE age BETWEEN 10 AND 20` is:

3. Which is correct for NULL check?