SQL Tutorial: SELECT & WHERE
Filter rows with WHERE conditions, comparison operators, LIKE, IN, and BETWEEN.
The WHERE Clause
Filter rows that match a condition:
```sql
SELECT name, email FROM users WHERE active = 1;
SELECT name, price FROM products WHERE price < 20;
```
---
Comparison Operators
| Operator | Meaning |
|---|---|
| `=` | Equal |
| `<>` or `!=` | Not equal |
| `<` `>` | Less / greater than |
| `<=` `>=` | Less / greater than or equal |
| `IS NULL` | Value is null |
| `IS NOT NULL` | Value is not null |
```sql
SELECT name FROM users WHERE age >= 18 AND age <= 65;
SELECT name FROM products WHERE description IS NOT NULL;
```
---
AND, OR, NOT
```sql
-- Both conditions must be true
SELECT * FROM orders WHERE status = 'shipped' AND total > 100;
-- Either condition is true
SELECT * FROM users WHERE plan = 'pro' OR is_admin = 1;
-- Negate a condition
SELECT * FROM products WHERE NOT discontinued;
```
---
LIKE — Pattern Matching
```sql
-- % matches any sequence of characters
SELECT name FROM users WHERE email LIKE '%@gmail.com';
-- _ matches exactly one character
SELECT code FROM products WHERE code LIKE 'SKU-___';
```
---
IN — Match a List
```sql
SELECT name FROM products WHERE category IN ('electronics', 'clothing', 'books');
-- NOT IN
SELECT * FROM orders WHERE status NOT IN ('cancelled', 'refunded');
```
---
BETWEEN
```sql
SELECT name, price FROM products WHERE price BETWEEN 10 AND 50;
SELECT * FROM orders WHERE created_at BETWEEN '2024-01-01' AND '2024-12-31';
```
---
What's Next?
Next: **ORDER BY & LIMIT** — sort results and retrieve top N rows.
What you'll learn in this SQL select & where tutorial
This interactive SQL tutorial has 4 hands-on exercises. Estimated time: 12 minutes.
- Filter with WHERE — find the engineers — The `WHERE` clause filters rows before they're returned. Every column in the table can be a filter.
- BETWEEN — salary band analysis — `BETWEEN x AND y` filters rows where a value falls in a range (inclusive). This is cleaner than writing `salary >= 10000…
- IN — filter across multiple values — `IN (v1, v2, v3)` is cleaner than writing `OR` for each value. It reads naturally and scales well.
- LIKE — search by partial text — `LIKE` matches patterns in text. `%` matches any sequence of characters. It's how you build search features.
SQL SELECT & WHERE concepts covered
- The WHERE Clause
- Comparison Operators
- AND, OR, NOT
- LIKE — Pattern Matching
- IN — Match a List
- BETWEEN
- What's Next?