What is the difference between = and LIKE in SQL?
SQLLevel: FoundationFundamentals (Easy)
What is the difference between = and LIKE in SQL?
Hints
- Define one output row and the source-table keys.
- Check duplicates, NULLs and the order of filtering and aggregation.
Answer
= is an exact equality check. LIKE is a pattern match using wildcards: % matches any sequence of characters, _ matches exactly one character.
-- Exact match
SELECT * FROM users WHERE email = 'alice@example.com';
-- Pattern: starts with 'alice'
SELECT * FROM users WHERE email LIKE 'alice%';
-- Pattern: any email at example.com
SELECT * FROM users WHERE email LIKE '%@example.com';
-- Pattern: exactly 3 chars then something
SELECT * FROM products WHERE code LIKE '___-%';
LIKE is generally slower because it can't use a standard B-tree index for leading wildcard patterns (%alice). Some databases offer full-text search or trigram indexes for these cases.
Source and licence: SQL Interview Questions — Arda Kocadoru · MIT