Filtering with WHERE
The
WHERE clause is used to filter records. It ensures that only the rows that fulfill a specific condition are returned.sql
Logical Operators
Wait! You can combine multiple conditions using
AND, OR, and NOT.sql
Range and Set Filtering
BETWEEN: Filters values within a specific range (inclusive).IN: Filters values that match any value in a list.LIKE: Used with wildcards (%for any string,_for a single character) to search for patterns.
sql
Sorting with ORDER BY
The
ORDER BY keyword is used to sort the result-set in ascending (ASC) or descending (DESC) order. By default, it sorts in ascending order.sql
Handling NULLs
In SQL,
NULL represents a missing or unknown value. You cannot use = to check for NULL; you must use IS NULL or IS NOT NULL.sql
Summary
| Keyword | Description |
|---|---|
| WHERE | Filtering rows based on conditions |
| ORDER BY | Sorting results (ASC/DESC) |
| AND / OR | Combining multiple filters |
| BETWEEN | Matching a range of values |
| IN | Matching any value in a set |
| LIKE | Pattern matching with wildcards (%, _) |
| IS NULL | Checking for missing values |
| Logic | Filtering happens BEFORE sorting in the execution plan |
| Safety | Always filter your queries to avoid loading huge datasets into memory |
| Modern | Indexes are used by the database to make WHERE clauses blazing fast |
| Wildcard | % matches zero or more characters; _ matches exactly one |