Understanding the Concept of Wildcards in SQL
Hello dear friend! Today we want to talk about wildcards in SQL. Why? Because these wildcards can help to find the records in database queries. Wildcards are small symbols that can represent one or several characters in search. Interesting, right?
Where Are Wildcards Used?
Let’s give a simple example! Imagine you are in a large database, looking for all products with the column "book" in their name. If you just use regular operators, it might take a long time to find all of them. However, by using wildcards, you can perform this task much faster.
Types of Wildcards in SQL
The most common wildcards in SQL are two: %
and _
. Each of them has its own specific use and can be very versatile in different situations. Correct use of these wildcards can solve many problems for us.
Understanding Each Wildcard
The wildcard %
represents any number of characters. For example, if you use %price
, you can find all entries that end with "price". In contrast, the wildcard _
represents a single character. For instance, with the expression _at
, you can find words like "bat", "cat", and so on.
A Look at How to Use These Wildcards
Below are some example codes using wildcards in SQL:
SELECT * FROM products WHERE product_name LIKE 'book%';. . . SELECT * FROM products WHERE product_name LIKE '%book';. . . SELECT * FROM products WHERE product_name LIKE '_at';. . .
Line-by-Line Explanation of the Code
SELECT * FROM products WHERE product_name LIKE 'book%'; - This line returns all records in the products table where the name starts with "book".
SELECT * FROM products WHERE product_name LIKE '%book'; - This line finds all records that end with "book".
SELECT * FROM products WHERE product_name LIKE '_at'; - This line picks records that exactly have three characters and end with "at".