Course
Wildcards
MySQL Tutorial
This SQL tutorial is structured for beginners to guide them from the foundational concepts to advanced data manipulation and querying techniques in SQL. By the end of this tutorial, you will have developed a robust understanding of SQL, equipping you with the knowledge to efficiently manage and analyze data across various database systems. This guide sets the stage for your growth into a skilled data professional, ready to tackle complex data challenges and contribute to the field of data analysis and database management.
Wildcards
The MySQL wildcards
The MySQL wildcards are special characters used in combination with the LIKE operator to search for text patterns in a table column. MySQL provides two wildcards: percent (%) and underscore (_).
The following table mentions the use case of the two wildcards in MySQL
Syntax
Following is the syntax of % and _ wildcards in MySQL
SELECT * FROM table_nameWHERE column_name LIKE wildcard;
The wildcard characters can be used in combination with each other. The following table demonstrates different ways of using '%' and '_' with the LIKE operator in a WHERE clause
The MySQL Percent % Wildcard
The MySQL % wildcard is a symbol used in SQL queries for pattern matching. It represents any sequence of characters (including zero characters) within a string.
When used with the LIKE operator in a WHERE clause, % allows you to search for values that match a specified pattern.
Example
First, let us create a table with the name CUSTOMERS using the following query
CREATE TABLE CUSTOMERS( ID INT NOT NULL, NAME VARCHAR(20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(25) NOT NULL, SALARY DECIMAL(18, 2), PRIMARY KEY(ID));
Now, let us insert values into the table created above using the INSERT statement as shown below
INSERT INTO CUSTOMERS VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00 ),(2, 'Khilan', 25, 'Delhi', 1500.00 ),(3, 'Kaushik', 23, 'Kota', 2000.00 ),(4, 'Chaitali', 25, 'Mumbai', 6500.00 ),(5, 'Hardik', 27, 'Bhopal', 8500.00 ),(6, 'Komal', 22, 'Hyderabad', 4500.00 ),(7, 'Muffy', 24, 'Indore', 10000.00 );
The CUSTOMERS table obtained is as follows
Here, we are retrieving all the records from the CUSTOMERS table where SALARY starts with 2000
SELECT * FROM CUSTOMERSWHERE SALARY LIKE '2000%';
Output
The output of the above query is as given below
Example
In the following query, we are fetching all the records where ADDRESS starts with 'D' and ends with 'i'
SELECT * FROM CUSTOMERSWHERE ADDRESS LIKE 'D%i';
Output
On executing the given query, the output is displayed as follows
Example
Here, we are finding all the records where ADDRESS ends with 'd'
SELECT * FROM CUSTOMERSWHERE ADDRESS LIKE '%d'
Output
When we execute the above query, the output is obtained as follows
Example
In the following query, we are trying to fetch all the records where SALARY has '1' in any position
SELECT * FROM CUSTOMERSWHERE SALARY LIKE '%1%';
Output
The output produced from the above query is as follows
The MySQL Underscore _ Wildcard
The MySQL underscore Wildcard represents a single character at the position where it is used. When combined with the LIKE operator in a WHERE clause, the underscore wildcard allows you to search for values that match a specific pattern with a single character placeholder.
Example
Here, we are retrieving all the CUSTOMERS with NAME starting with a character, followed by 'ardik'
SELECT * FROM CUSTOMERSWHERE NAME LIKE '_ardik';
Output
Let us compile and run the query, to produce the following result
Example
Now, we are retrieving all CUSTOMERS with NAME starting with 'M', followed by any character, followed by 'f', followed by any character, followed by 'y'
SELECT * FROM CUSTOMERSWHERE NAME LIKE 'M_f_y';
Output
When we execute the above query, the output is obtained as follows
Example
In the below query, we are retrieving all the records where SALARY have '500' in the second, third, and fourth positions
SELECT * FROM CUSTOMERSWHERE SALARY LIKE '_500%';
Output
On executing the given query, the output is displayed as follows
Example
In the following query, we are retrieving all the records where ADDRESS starts with 'M' and is at least 3 characters in length
SELECT * FROM CUSTOMERSWHERE ADDRESS LIKE 'M_%_%';
Output
The output of the above query is produced as given below
Example
The following query retrieves all records where NAME has 'h' in the second position and ends with 'i'
SELECT * FROM CUSTOMERSWHERE NAME LIKE '_h%i';
Output
If we compile and run the query, the result is produced as follows