SQL Wildcards Tutorial
Example Table
We will use the following table named employees
for our examples:
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
name VARCHAR(50),
department VARCHAR(50),
salary DECIMAL(10, 2)
);
INSERT INTO employees (employee_id, name, department, salary) VALUES
(1, 'Alice', 'HR', 50000.00),
(2, 'Bob', 'IT', 60000.00),
(3, 'Charlie', 'Finance', 55000.00),
(4, 'David', 'IT', 62000.00),
(5, 'Eve', 'HR', 48000.00);
Employees Table
employee_id | name | department | salary |
---|---|---|---|
1 | Alice | HR | 50000.00 |
2 | Bob | IT | 60000.00 |
3 | Charlie | Finance | 55000.00 |
4 | David | IT | 62000.00 |
5 | Eve | HR | 48000.00 |
Using SQL Wildcards
SQL wildcards are used with the LIKE
operator to search for a specified pattern in a column.
Example: Using the Percent (%) Wildcard
To select employees whose names start with 'A':
SELECT * FROM employees
WHERE name LIKE 'A%';
Result:
Query | SELECT * FROM employees WHERE name LIKE 'A%'; |
---|---|
Result |
|
Example: Using the Underscore (_) Wildcard
To select employees whose names have 'a' as the second character:
SELECT * FROM employees
WHERE name LIKE '_a%';
Result:
Query | SELECT * FROM employees WHERE name LIKE '_a%'; |
---|---|
Result |
|
Example: Using the Brackets ([]) Wildcard
To select employees whose names start with 'A' or 'E':
SELECT * FROM employees
WHERE name LIKE '[AE]%';
Result:
Query | SELECT * FROM employees WHERE name LIKE '[AE]%'; |
---|---|
Result |
|