Answer simple quiz at the end of the blog post and –
Every day one winner from India will get Joes 2 Pros Volume 1.
Every day one winner from United States will get Joes 2 Pros Volume 1.
Wildcard ranges
If you have ever been to a convention where they have a morning registration desk that must handle thousands of people in a short time you know they must put some pre-planning thought into how to handle this burst of volume. In fact often they will have many registration desks running in parallel to make things run faster. The first registration desk might handle all customer last names starting from A to K. Desk 2 will handle names from L to Q and the third desk will handle from R to Z.
With my last name being Morelan I would naturally head to registration desk #2 knowing that desk has the list with my name on it and the other desks don’t. Now let’s say you are in charge of creating these three separate lists and sending them out to the right registration workers. You know how to sort but how can you separate this lists using wildcards?
Wildcard Basics Recap
Lets start off with something most of us know already. Most SQL folks understand the usefulness and power of the basic uses of wildcards. Using wildcards allows you to do pattern matches in a column. In this case our criteria does not want to use the = sign to find a pattern match. The operator that allows you to do approximate predicates is LIKE. The LIKE operator allows you to do special relative searches to filter your result set.
--Find all LastNames that start with the letter A
SELECT *
FROM Employee
WHERE LastName LIKE 'A%'
To find everyone whose last name starts with the letter B, you need “B” to be the first letter. After the letter B you can have any number of characters. Using B% in single quotes after the LIKE operator gets all last names starting with the letter B.
--Find all LastNames that start with the letter B
SELECT *
FROM Employee
WHERE LastName LIKE 'B%'
Wildcard ranges or set specifiers
If you want to find all LastName values starting with the letters A or B you can use two predicates in your WHERE clause. You need to separate them with the OR operator.
--Find all LastNames that start with the letter B
SELECT *
FROM Employee
WHERE LastName LIKE 'A%'
OR LastName LIKE 'B%'
Finding names beginning with A or B is easy. How about the registration desk example where want the names ranging from A-K? This works well until you want a range of A-K as in the example below:
--Find all LastNames ranging from A-K
SELECT *
FROM Employee
WHERE LastName LIKE 'A%'
OR LastName LIKE 'B%'
OR LastName LIKE 'C%'
OR LastName LIKE 'D%'
OR LastName LIKE 'E%'
OR LastName LIKE 'F%'
OR LastName LIKE 'G%'
OR LastName LIKE 'H%'
OR LastName LIKE 'I%'
OR LastName LIKE 'J%'
OR LastName LIKE 'K%'
The previous query does find LastName values starting from A-K. However, if you need a range of letters, the LIKE operator has many better options. We only really care about the first letter of the last name and there a several first letters that fit with what were looking for. The first letter of the last name can be A,B,C,D,E,F,G,H,I,J or K. Simply list all the choices you want for the first letter inside a set of square brackets.
--LastNames ranging from A to K using a set of 11 letters
SELECT *
FROM Employee
WHERE LastName LIKE '[ABCDEFGHIJK]%'
Square brackets with wildcards enclose ranges or sets for 1 position. In this case the first position is a set of 11 different possible letters. This is not a series of letter but a multiple choice of letters. For example this works regardless of the order you put your letters in. This code sample below does the exact same thing.
--LastNames ranging from A to K using a set of 11 letters
SELECT *
FROM Employee
WHERE LastName LIKE '[KBCDEFGHIJA]%'
Again the set is how many letters you put in the square brackets. The code below is a logical mistake where you won’t get A to K but you just get A or K for the first letter.
--Find all LastNames starting with A or K (Mistake
SELECT *
FROM Employee
WHERE LastName LIKE '[AK]%'
Since we’re looking for the first letter to be within a range from A to K, we specify that range in square brackets. This is even easier than using a set. The wildcard after the brackets allows any number of characters after the range.
--LastNames ranging from A to K using a range
SELECT *
FROM Employee
WHERE LastName LIKE '[A-K]%'
Note: this range will not work if your LIKE was changed to an equal (=) sign. The following code will not return any records to your result set:
--Bad query (it won’t error but returns no records)
SELECT *
FROM Employee
WHERE LastName = '[A-K]%'
Note: If you want to setup the sample JProCo database on your system you can watch this video.
Question 1
Q. 1) You want to find all first names that start with the letters A-M in your Customer table. Which SQL code would you use?
- SELECT * FROM Customer
WHERE Firstname <= ‘m%’ - SELECT * FROM Customer
WHERE Firstname = ‘a-m%’ - SELECT * FROM Customer
WHERE Firstname like ‘a-m%’ - SELECT * FROM Customer
WHERE Firstname = ‘[a-m]% ‘ - SELECT * FROM Customer
WHERE Firstname like ‘[a-m]%’
Please post your answer in comment section to win Joes 2 Pros books.
Rules:
Please leave your answer in comment section below with correct option, explanation and your country of resident.
Every day one winner will be announced from United States.
Every day one winner will be announced from India.
A valid answer must contain country of residence of answerer.
Please check my facebook page for winners name and correct answer.
Winner from United States will get Joes 2 Pros Volume 1.
Winner from India will get Joes 2 Pros Volume 1.
The contest is open till next blog post shows up at which is next day GTM+2.5.
Reference: Pinal Dave (https://blog.sqlauthority.com)
334 Comments. Leave new
Good post….
Answer is 5 – SELECT * FROM Customer WHERE Firstname like ‘[a-m]%’
Is there any prize for early bird ;)
Good post. The correct answer is #5.
I’m from USA
SELECT * FROM Customer
WHERE Firstname like ‘[a-m]%’
Good post….
Answer is 5 – SELECT * FROM Customer WHERE Firstname like ‘[a-m]%’
Is there any prize for early bird ;)
I’m from INDIA
5) SELECT * FROM Customer
WHERE Firstname like ‘[a-m]%’
COUNTRY:- INDIA
If we need to find all first names that start with the letters A-M (i.e A to M) in your Customer table.
then query should be as:
SELECT * FROM Customer
WHERE Firstname like ‘[a-m]%’
Varinder Sandhu from India
Correnct Answer is : Option 5
Thanks
Dhananjay Kumar
Correct Answer is : Option 5
explanation : using wildcard character ‘-‘ , we can query over a range. If we use = instead of like we will get no eroor but also no result.
Country : India [Pune]
The answer is option 5
SELECT * FROM Customer
WHERE Firstname like ‘[a-m]%’
Since we are searching for the range of character we need to use like operator. As we are searching for the first name that starts with a to m we need to use [a-m] and followed by ‘%’
—
Sunitha
Hyderabad,India
I would use #5 as it creates the A-M set with the wildcard on the outside of the set. Using the LIKE statement is mandatory to enable the use of the “%” wildcard. I am located in the United States. If I were in a database that enforces case sensitivity, I would then use “like ‘[A-M]%'” to match your question.
option 5 gives the correct result
I am living in India
Answer number 5 is correct :
SELECT * FROM Customer
WHERE Firstname like ‘[a-m]%’
We have use the square bracket and put ‘A’ and ‘M’ in it with ‘-‘ which shows a range. All the name staring with ‘A’ to ‘M’ range will be display in the final output.
SELECT * FROM Customer
WHERE Firstname like ‘[a-m]%’‘
Option 5 is the correct answer
because its a range from a-m and we need to use Square brackets and as it can starts with a-m, we must use Like operator.
Country of Resident – INDIA
Answer is option 5:
SELECT * FROM Customer WHERE Firstname like ‘[a-m]%’
because wildcard characters [] returns the data with in the specified range that starts with the letters a-m
Country of Residence – INDIA.
SELECT * FROM Customer
WHERE Firstname like ‘[a-m]%’
Hi the correct answer is option 5
SELECT * FROM Customer
WHERE Firstname like ‘[a-m]%’
this query will return all records from Customer table whose firstname starts with a or b or……..or m. Case dosent matter.
Sumit Kumar Gaud
India
SELECT * FROM Customer
WHERE Firstname like ‘[a-m]%’
ans is option 5
because using like only wildcard checking possible
in option 3 not properly where condition so, 5 is answer
Last Option is the correct answer
5. SELECT * FROM Customer
WHERE Firstname like ‘[a-m]%’
Sorry Forgot to mention.
The reason is that :
1 st option will not work as it is not integer comparison.
other options return names starting with “a-m” and not from range.
The forth option will not return any results as mention in post range will not work with “=” but like.
I am from India
5) SELECT * FROM Customer
WHERE Firstname like ‘[a-m]%’
The above is the correct answer.
The LIKE operator is used & the correct syntax for range selection is used here.
Option 1 does not work bcos it displays names starting with A-L range omitting M.
Country – Bangalore,India
This is Ritesh from India.
I would consider 5th query as best choice which is as per below.
SELECT * FROM Customer
WHERE Firstname like ‘[a-m]%’
Actually the search we are looking for, could be done by two queries from the list which is 1st and 5th. remaining queries wouldn’t return desired result.
Actually if you have only alphabetic value in your column and if you are using any of the two query below, would return result.
SELECT * FROM Customer
WHERE Firstname <= ‘m%’
SELECT * FROM Customer
WHERE Firstname like ‘[a-m]%’
but the problem with "<='m%'" is, it will compare ASCII and anything less that the ASCII of m would come in the list, obviously, it will return range a,b,c……n,m
but not only this, any data which start with numeric value or space, even come in the result set that is why I would like to go for [a-m]% with LIKE operator.
The correct Answer:
5.SELECT * FROM Customer
WHERE Firstname like ‘[a-m]%’
Acoording to me it is correct b’coz we are filtering for the FirstName which start with SPECIFIC character range (i.e. a-m). And we are only checking for the first character, so we should use [ ].
Hey, I am very new to SQL Server, so please forgive me if any silly explanation is there.
Country : India