In SQL, the LIKE
operator is used to search for a specified pattern in a column. The double percentage signs (%%
) are not directly used as operators in SQL. Instead, the %
symbol is used as a wildcard character in combination with the LIKE
operator to match any sequence of characters (including zero characters) in a search pattern.
Here’s how the LIKE
operator works with the %
wildcard:
%
: Matches any sequence of characters (including none)._
: Matches a single character.
For example, if you want to find all rows where the “name” column starts with “John”, you can use:
- SELECT * FROM users WHERE name LIKE ‘John%’;
If you want to find all rows where the “email” column contains “example.com”, you can use:
2. SELECT * FROM users WHERE email LIKE ‘%example.com%’;
And if you want to find all rows where the “username” column is exactly four characters long, you can use:
3. SELECT * FROM users WHERE username LIKE '____'; -- Four underscores
In this case, each underscore _ represents a single character, so ____ will match usernames that are exactly four characters long.