SELECT, WHERE and LIMIT in MySQL: Filtering with LIKE and REGEXP

MySQL 55 min min read Updated: Nov 27, 2026 Beginner
SELECT, WHERE and LIMIT in MySQL: Filtering with LIKE and REGEXP
Beginner Topic 1 of 3

SELECT and Aliases

sql SELECT name, city, joined FROM customers WHERE id <= 3 ORDER BY id;

Result:

namecityjoined
Asha RaoMumbai2024-01-15
Ben CarterLondon2024-02-20
Chen WeiSingapore2024-03-05

Give columns readable names with AS. Aliases with spaces need quotes (backticks in MySQL):

sql SELECT name AS customer, price AS `unit price`, stock * price AS stock_value FROM products WHERE id <= 3 ORDER BY id;

Result:

customerunit pricestock_value
Laptop899.0013485.00
Phone599.0017970.00
Headphones79.007900.00

Backticks for Odd Names

MySQL uses backticks around names that contain spaces or clash with reserved words. Single quotes are for text values.

sql CREATE TABLE `order` (id INT PRIMARY KEY, `group` VARCHAR(10), `select` INT); INSERT INTO `order` VALUES (1, 'a', 10); SELECT id, `group`, `select` FROM `order`;

Result:

idgroupselect
1a10
Better: avoid reserved words Names like order, group and select work only with backticks. Prefer orders, team, choice.

Comparison and Logic

sql SELECT name, category, price FROM products WHERE (category = 'Electronics' OR category = 'Furniture') AND price < 100 ORDER BY price;

Result:

namecategoryprice
LampFurniture45.00
HeadphonesElectronics79.00
sql SELECT name, price FROM products WHERE price BETWEEN 40 AND 130 ORDER BY price;

Result:

nameprice
Lamp45.00
Headphones79.00
Chair129.00
sql SELECT name, city FROM customers WHERE city IN ('London', 'Mumbai') AND id <> 5 ORDER BY id;

Result:

namecity
Asha RaoMumbai
Ben CarterLondon
Fatima KhanMumbai

LIKE: Simple Patterns

% matches any number of characters and _ matches exactly one. With the default case insensitive collation, LIKE ignores case:

sql SELECT name FROM customers WHERE name LIKE 'asha%' OR name LIKE '%wei' OR name LIKE 'B_n%' ORDER BY id;

Result:

name
Asha Rao
Ben Carter
Chen Wei

To match a real % or _, escape it with a backslash or choose an escape character:

sql CREATE TABLE coupons (code VARCHAR(20)); INSERT INTO coupons VALUES ('SAVE10%'), ('SAVE100'), ('SAVE_X'); SELECT code FROM coupons WHERE code LIKE 'SAVE10\%' ORDER BY code;

Result:

code
SAVE10%

REGEXP: Regular Expressions

REGEXP (also RLIKE) supports full patterns. MySQL 8 uses the ICU regular expression library.

PatternMeaning
^AStarts with A
n$Ends with n
a.ba, any one character, b
[0-9]+One or more digits
(red|blue)red or blue
^[A-Z][a-z]+$One capitalized word
sql SELECT name FROM customers WHERE name REGEXP '^(Asha|Ben)' ORDER BY id;

Result:

name
Asha Rao
Ben Carter
sql SELECT name FROM products WHERE name REGEXP 'a.*e' ORDER BY id;

Result:

name
Headphones
sql SELECT email, email REGEXP '^[a-z]+@example\\.com$' AS looks_valid FROM customers WHERE id IN (1, 4) ORDER BY id;

Result:

emaillooks_valid
asha@example.com1
NULLNULL
Pitfall Regex patterns are not indexed. 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:

sql SELECT NULL = NULL AS plain_equal, NULL <=> NULL AS null_safe_equal, 5 <=> NULL AS five_vs_null;

Result:

plain_equalnull_safe_equalfive_vs_null
NULL10
sql SELECT name FROM customers WHERE email <=> NULL;

Result:

name
Dina Cohen

ORDER BY, LIMIT and OFFSET

sql SELECT name, price FROM products ORDER BY price DESC LIMIT 3;

Result:

nameprice
Laptop899.00
Phone599.00
Desk249.00

LIMIT offset, count is a MySQL shorthand. These two queries are the same page of results:

sql SELECT id, name FROM products ORDER BY id LIMIT 3 OFFSET 3;

Result:

idname
4Desk
5Chair
6Notebook
sql SELECT id, name FROM products ORDER BY id LIMIT 3, 3;

Result:

idname
4Desk
5Chair
6Notebook
Argument order of the short form In 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

sql SELECT DISTINCT city FROM customers ORDER BY city;

Result:

city
London
Mumbai
Singapore
Tel Aviv
sql SELECT COUNT(DISTINCT city) AS cities, COUNT(*) AS customers FROM customers;

Result:

citiescustomers
46

Sorting with ORDER BY Tricks

MySQL sorts NULL first in ascending order. FIELD() gives a custom order, and RAND() a random order:

sql SELECT name, email FROM customers ORDER BY email IS NULL, email LIMIT 3;

Result:

nameemail
Asha Raoasha@example.com
Ben Carterben@example.com
Chen Weichen@example.com
sql SELECT status, COUNT(*) AS orders FROM orders GROUP BY status ORDER BY FIELD(status, 'pending', 'shipped', 'cancelled');

Result:

statusorders
pending2
shipped7
cancelled1

ORDER BY RAND() LIMIT 1 picks a random row, but it sorts the whole table, so avoid it on big tables.

Common Beginner Mistakes

MistakeWhat happensFix
Double quotes for textWorks by default but is non-standard and breaks with ANSI_QUOTESUse single quotes
WHERE col = NULLNo rowsIS NULL or <=>
LIMIT 3, 3 confusionWrong pageWrite LIMIT 3 OFFSET 3
LIKE '%word%' on big tablesFull table scanUse FULLTEXT search
Reserved words as table namesSyntax errorsUse clear plural names
ORDER BY RAND() on big tablesVery slowPick a random id differently

Practice

  1. List customers whose name has exactly 8 characters (use _).
  2. Find products whose name starts with a vowel using REGEXP.
  3. Show the second page of 3 customers sorted by name.
  4. 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.

Quick Summary: SELECT, WHERE, LIKE, REGEXP, null-safe equals, LIMIT OFFSET, DISTINCT, ORDER BY tricks.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators