SELECT and Aliases
Result:
| name | city | joined |
|---|---|---|
| Asha Rao | Mumbai | 2024-01-15 |
| Ben Carter | London | 2024-02-20 |
| Chen Wei | Singapore | 2024-03-05 |
Give columns readable names with AS. Aliases with spaces need quotes (backticks in MySQL):
Result:
| customer | unit price | stock_value |
|---|---|---|
| Laptop | 899.00 | 13485.00 |
| Phone | 599.00 | 17970.00 |
| Headphones | 79.00 | 7900.00 |
Backticks for Odd Names
MySQL uses backticks around names that contain spaces or clash with reserved words. Single quotes are for text values.
Result:
| id | group | select |
|---|---|---|
| 1 | a | 10 |
order, group and select work only with backticks. Prefer orders, team, choice.
Comparison and Logic
Result:
| name | category | price |
|---|---|---|
| Lamp | Furniture | 45.00 |
| Headphones | Electronics | 79.00 |
Result:
| name | price |
|---|---|
| Lamp | 45.00 |
| Headphones | 79.00 |
| Chair | 129.00 |
Result:
| name | city |
|---|---|
| Asha Rao | Mumbai |
| Ben Carter | London |
| Fatima Khan | Mumbai |
LIKE: Simple Patterns
% matches any number of characters and _ matches exactly one. With the default case insensitive collation, LIKE ignores case:
Result:
| name |
|---|
| Asha Rao |
| Ben Carter |
| Chen Wei |
To match a real % or _, escape it with a backslash or choose an escape character:
Result:
| code |
|---|
| SAVE10% |
REGEXP: Regular Expressions
REGEXP (also RLIKE) supports full patterns. MySQL 8 uses the ICU regular expression library.
| Pattern | Meaning |
|---|---|
^A | Starts with A |
n$ | Ends with n |
a.b | a, any one character, b |
[0-9]+ | One or more digits |
(red|blue) | red or blue |
^[A-Z][a-z]+$ | One capitalized word |
Result:
| name |
|---|
| Asha Rao |
| Ben Carter |
Result:
| name |
|---|
| Headphones |
Result:
| looks_valid | |
|---|---|
| asha@example.com | 1 |
| NULL | NULL |
LIKE 'abc%' can use an index, REGEXP cannot. Use REGEXP for small result sets or for validation.
NULL and the Null-Safe Equals <=>
col = NULL never matches. Use IS NULL. MySQL also has <=>, which treats two NULLs as equal:
Result:
| plain_equal | null_safe_equal | five_vs_null |
|---|---|---|
| NULL | 1 | 0 |
Result:
| name |
|---|
| Dina Cohen |
ORDER BY, LIMIT and OFFSET
Result:
| name | price |
|---|---|
| Laptop | 899.00 |
| Phone | 599.00 |
| Desk | 249.00 |
LIMIT offset, count is a MySQL shorthand. These two queries are the same page of results:
Result:
| id | name |
|---|---|
| 4 | Desk |
| 5 | Chair |
| 6 | Notebook |
Result:
| id | name |
|---|---|
| 4 | Desk |
| 5 | Chair |
| 6 | Notebook |
LIMIT 3, 3 the first number is the offset (rows to skip) and the second is the count. This is the opposite of what many people expect. The LIMIT 3 OFFSET 3 form is clearer.
DISTINCT
Result:
| city |
|---|
| London |
| Mumbai |
| Singapore |
| Tel Aviv |
Result:
| cities | customers |
|---|---|
| 4 | 6 |
Sorting with ORDER BY Tricks
MySQL sorts NULL first in ascending order. FIELD() gives a custom order, and RAND() a random order:
Result:
| name | |
|---|---|
| Asha Rao | asha@example.com |
| Ben Carter | ben@example.com |
| Chen Wei | chen@example.com |
Result:
| status | orders |
|---|---|
| pending | 2 |
| shipped | 7 |
| cancelled | 1 |
ORDER BY RAND() LIMIT 1 picks a random row, but it sorts the whole table, so avoid it on big tables.
Common Beginner Mistakes
| Mistake | What happens | Fix |
|---|---|---|
| Double quotes for text | Works by default but is non-standard and breaks with ANSI_QUOTES | Use single quotes |
WHERE col = NULL | No rows | IS NULL or <=> |
LIMIT 3, 3 confusion | Wrong page | Write LIMIT 3 OFFSET 3 |
LIKE '%word%' on big tables | Full table scan | Use FULLTEXT search |
| Reserved words as table names | Syntax errors | Use clear plural names |
ORDER BY RAND() on big tables | Very slow | Pick a random id differently |
Practice
- List customers whose name has exactly 8 characters (use
_). - Find products whose name starts with a vowel using
REGEXP. - Show the second page of 3 customers sorted by name.
- Find customers whose email is NULL or does not end in
example.com.
Conclusion
MySQL filtering works with WHERE, LIKE, REGEXP, IN, BETWEEN and IS NULL, adds <=> and backticks, and uses LIMIT with OFFSET for paging. Know the collation rules for case and avoid RAND() and regex on big tables.

