SQL Tutorial
SQL Clauses / Operators
SQL-Injection
SQL Functions
SQL Queries
PL/SQL
MySQL
SQL Server
Misc
Wildcard operators in SQL are used with the SQL LIKE
operator to perform pattern matching in queries. These operators enable you to search for data that fits specific patterns, rather than having to know the exact value to search for.
Here are the most commonly used wildcard operators:
Percent %
: Represents zero or more characters.
Example:
SELECT * FROM employees WHERE name LIKE 'J%';
This query selects all employees whose names start with the letter "J".
Underscore _
: Represents a single character.
Example:
SELECT * FROM employees WHERE name LIKE 'J_n';
This query selects all employees whose names are exactly 3 characters long and start with "J" and end with "n", like "Jon", "Jan", etc.
Square Brackets [...]
(MS SQL Server): Matches any single character within the brackets.
Example:
SELECT * FROM employees WHERE name LIKE 'J[oa]n';
This query will select all employees whose names are either "Jon" or "Jan" (only supported in some database systems like MS SQL Server).
Caret ^
inside Square Brackets (MS SQL Server): Matches any single character not within the brackets.
Example:
SELECT * FROM employees WHERE name LIKE 'J[^oa]n';
This query will select all employees whose names are 3 characters long, start with "J", end with "n", but the middle character is not "o" or "a".
You can combine these wildcard characters to make more complex pattern-matching conditions.
Example:
SELECT * FROM employees WHERE name LIKE 'J%n';
This query will select all employees whose names start with "J" and end with "n".
You can also use NOT LIKE
to exclude rows that match the pattern.
Example:
SELECT * FROM employees WHERE name NOT LIKE 'J%';
This query will select all employees whose names do not start with "J".
Note that the LIKE
query may be case-sensitive or case-insensitive depending on your database. For instance, SQL Server is case-insensitive by default, whereas PostgreSQL is case-sensitive. You may need to use database-specific functions like UPPER()
or LOWER()
to standardize case if necessary.
Example for making a case-insensitive search in PostgreSQL:
SELECT * FROM employees WHERE LOWER(name) LIKE 'j%';
In summary, wildcard operators offer a powerful way to perform pattern matching in SQL queries, enabling more flexible and dynamic data retrieval.
How to Use Wildcards in SQL Queries:
%
and _
.-- Using wildcards SELECT * FROM employees WHERE last_name LIKE 'Sm%';
LIKE Operator in SQL with Wildcard Characters:
LIKE
operator is used with wildcard characters to perform pattern matching.-- Using LIKE with wildcard % SELECT * FROM products WHERE product_name LIKE 'Laptop%';
SQL Wildcard Operators for Pattern Matching:
%
and _
, are used for pattern matching in SQL.-- Using wildcards for pattern matching SELECT * FROM customers WHERE phone_number LIKE '555%';
% and _ Wildcard in SQL:
%
matches any sequence of characters, and _
matches any single character.-- Using % and _ wildcards SELECT * FROM products WHERE product_name LIKE 'Laptop%'; SELECT * FROM employees WHERE last_name LIKE 'A_';
Using Wildcards in WHERE Clause in SQL:
WHERE
clause to filter rows based on patterns.-- Using wildcards in WHERE clause SELECT * FROM orders WHERE order_date LIKE '2023-01%';
SQL Wildcard Operators with Multiple Characters:
-- Using wildcards with multiple characters SELECT * FROM products WHERE product_name LIKE 'S%e';
Escaping Wildcard Characters in SQL:
%
or _
, you can use the ESCAPE
keyword to escape the wildcard character.-- Escaping wildcard character SELECT * FROM text_data WHERE text_value LIKE '%\_%' ESCAPE '\';
SQL Wildcard Operators for Searching:
-- Searching with wildcards SELECT * FROM products WHERE product_name LIKE '%camera%';
Wildcard Search in SQL with Patterns:
-- Wildcard search with patterns SELECT * FROM customers WHERE email LIKE '%@example%';
SQL NOT LIKE Operator with Wildcards:
NOT LIKE
operator is used to filter rows not matching a specific pattern.-- NOT LIKE with wildcards SELECT * FROM products WHERE product_name NOT LIKE '%Out of Stock%';
Combining Wildcards with Other Operators in SQL:
-- Combining wildcards with other operators SELECT * FROM employees WHERE first_name LIKE 'A%' AND salary > 50000;
SQL Wildcard Characters in SELECT Statement:
SELECT
statement for more flexible output.-- Using wildcards in SELECT statement SELECT product_name, SUBSTRING(product_name, 1, 3) AS short_name FROM products WHERE product_name LIKE 'Laptop%';
SQL Wildcard Search for Numeric and Alphanumeric Values:
-- Wildcard search for numeric and alphanumeric values SELECT * FROM orders WHERE order_number LIKE 'ORD%';