W3. SQL Wildcards Flashcards
Q: What is a wildcard character in SQL?
A: It is used to substitute one or more characters in a string, often with the LIKE operator in a WHERE clause.
Q: Which wildcard character represents zero or more characters?
%
Q: Which wildcard character represents a single character?
_
Q: What does the [] wildcard do?
A: It matches any single character within the brackets.
Q: Write a query to select all customers whose CustomerName starts with “a”.
SELECT * FROM Customers
WHERE CustomerName LIKE ‘a%’;
Q: Write a query to return all customers whose CustomerName ends with “es”.
SELECT * FROM Customers
WHERE CustomerName LIKE ‘%es’;
Q: Write a query to return all customers from cities starting with any character, followed by “ondon”.
SELECT * FROM Customers
WHERE City LIKE ‘_ondon’;
Q: How do you use the [] wildcard to match specific characters?
A: Place characters within brackets, e.g., [bsp]% to match customers starting with “b”, “s”, or “p”.
Q: Write a query to select customers whose CustomerName starts with either “b”, “s”, or “p”.
SELECT * FROM Customers
WHERE CustomerName LIKE ‘[bsp]%’;
Q: What does the - wildcard do when used within []?
A: It specifies a range of characters.
Q: Write a query to select customers whose CustomerName starts with any letter from “a” to “f”.
SELECT * FROM Customers
WHERE CustomerName LIKE ‘[a-f]%’;
Q: How can % and _ be combined in a pattern?
A: They can be combined to create specific patterns, e.g., LIKE ‘a__%’ for names starting with “a” and at least 3 characters long.
Q: Write a query to return all customers that have “r” in the second position.
SELECT * FROM Customers
WHERE CustomerName LIKE ‘_r%’;
Q: If no wildcard is specified in LIKE, what happens?
A: The search requires an exact match.
Q: Write a query to select all customers from Spain using LIKE.
SELECT * FROM Customers
WHERE Country LIKE ‘Spain’;