The LIKE operator matches text using simple patterns. Use % for any sequence of characters and _ for a single character. It's handy for partial string matches in WHERE clauses.
WHERE.LIKE is usually case-insensitive depending on collation; use BINARY to force case-sensitivity.
-- Syntax: select columns where a column matches a pattern
SELECT column1, column2
FROM table_name
WHERE column_name LIKE pattern;
-- Find students whose names start with 'J'
SELECT * FROM students
WHERE name LIKE 'J%';
-- Find students whose names end with 'son'
SELECT * FROM students
WHERE name LIKE '%son';
-- Find students whose names contain 'an'
SELECT * FROM students
WHERE name LIKE '%an%';
-- Use underscore to match a single character (e.g., any name with 'a' as second char)
SELECT * FROM students
WHERE name LIKE '_a%';
-- Force case-sensitive match using BINARY
SELECT * FROM students
WHERE BINARY name LIKE 'J%';
Using LIKE 'J%' returns rows where name begins with "J". Using '%son' returns rows ending with "son". The % and _ wildcards let you control how strict or loose the match is.
Leading wildcards (e.g., %term) prevent index usage on the column and can be slow on large tables. Consider full-text indexes or computed columns for heavy pattern-search workloads.
LIKE 'abc%' when you can (no leading %) so indexes may be used.REGEXP) if your DB supports it.BINARY if needed.a% and %z patterns.)_.BINARY to compare case-sensitive vs case-insensitive results.