The MySQL LIKE operator helps us search for specified patterns in a column. It is useful when we need to find records that match specific patterns, like names starting with a certain letter or containing a specific word. In this article, we will cover the concept, syntax, and examples of using the LIKE operator.
MySQL LIKE Operator
The LIKE operator is used in a WHERE clause to look for a pattern in a column. There are two main wildcards you can use:
- %: Matches zero or more characters.
- _: Matches exactly one character.
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
Where,
- column1, column2, ...: The columns you want to retrieve.
- table_name: The table from which you're getting data.
- columnN: The column to search.
- pattern: The pattern you're looking for.
Demo MySQL Database
To get a good understanding of the MySQL LIKE operator, we'll first create a sample database and table, then run some queries to see how the LIKE operator works.
Create employee table:
CREATE TABLE employees (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
position VARCHAR(255),
salary DECIMAL(10, 2),
location VARCHAR(255)
);
Insert sample data:
INSERT INTO employees (name, position, salary, location) VALUES
('Amit', 'Manager', 60000.00, 'Mumbai'),
('Ganesh', 'Developer', 55000.00, 'Bangalore'),
('Gaurav', 'Designer', 60000.00, 'Hyderabad'),
('Yuvraj', 'Developer', 52000.00, 'Pune'),
('Divya', 'Manager', 62000.00, 'Delhi'),
('Asmita', 'Tester', 48000.00, 'Pune'),
('Farhan', 'Developer', 51000.00, 'Kolkata'),
('Gita', 'Designer', 50000.00, 'Chennai'),
('Harish', 'Manager', 60000.00, 'Mumbai'),
('Akash', 'Tester', 47000.00, 'Bangalore');
Output:
OutputExamples using the LIKE Operator:
Example 1: Search for Names Starting with 'A'
SELECT * FROM employees
WHERE name LIKE 'A%';
Output:
OutputExplanation: This query finds all employees whose names start with 'A'.
Example 2: Search for Names Ending with 'h'
SELECT * FROM employees
WHERE name LIKE '%h';
Output:
OutputExplanation: This query finds all employees whose names end with 'n'.
Example 3: Search for Names Containing 'ar'
SELECT * FROM employees
WHERE name LIKE '%ar%';
Output:
OutputExplanation: This query finds all employees whose names contain 'ar'.
Example 4: Search for Names with 'i' as the Second Character
SELECT * FROM employees
WHERE name LIKE '_i%';
Output:
OutputExplanation: This query finds all employees whose names have 'i' as the second character. It will return:
Conclusion
The MySQL LIKE operator is a powerful tool for pattern matching in SQL queries. It allows you to search for records that match specific criteria using the '%' and '_' wildcards. This can be especially useful for finding records based on partial matches, such as names starting with a certain letter or containing specific sequences of characters, enhancing your data retrieval capabilities.
Similar Reads
MySQL IN Operator
The MySQL IN operator is used to filter data based on a specified set of values. It is a shorthand for multiple OR conditions which allows us to specify a list of values in a WHERE clause to match records that have any of those values. This makes your SQL queries more concise and easier to read. MyS
3 min read
MySQL EXCEPT Operator
Comparison of two different sets of data in a database and finding entries unique to one set is one of the common operations done on databases. The EXCEPT operator in SQL compares the two result sets returned and returns only the rows that are found in one but not the other. Moreover, MySQL, one of
6 min read
MySQL INTERSECT Operator
In MySQL, the INTERSECT operator is used to find common records between two result sets. However, MySQL does not support the INTERSECT operator natively. To achieve similar functionality, you can use alternative methods such as INNER JOINs or subqueries. These techniques allow you to retrieve the in
4 min read
MySQL BETWEEN Operator
MySQL consists of various operators for performing efficient data queries, and the BETWEEN operator is a key operator for filtering records within a specific range. By specifying a lower and upper bound, BETWEEN helps you easily retrieve data that falls between two values. This operator simplifies q
4 min read
MySQL NOT EQUAL Operator
SQL (Structured Query Language) is a powerful language used for managing and manipulating relational databases. It provides a standardized language for querying the databases which are found to be structured, allowing users to define, manipulate, and control the data retrieval with ease. SQL operate
4 min read
SQL LIKE Operator
The SQL LIKE operator is used for performing pattern-based searches in a database. It is used in combination with the WHERE clause to filter records based on specified patterns, making it essential for any database-driven application that requires flexible search functionality. In this article, we w
5 min read
Arithmetic Operators in MySQL
Arithmetic operators in MySQL are tools for performing basic math in database queries. They handle addition (+), subtraction (-), multiplication (*), division (/), and modulus (%) operations. These operators are vital for tasks like calculating totals, differences, products, quotients, and remainder
5 min read
Like Operator in R
In this article, we will discuss the Like Operator in R Programming language and its uses in different scenarios and conditions. Like Operator in RThe "like" operator in R Programming Language is used to perform pattern matching within character vectors. It allows users to search for specific patter
2 min read
MySQL ANY and ALL Operators
When working with databases, there are often scenarios where we need to compare a value against multiple other values. MySQL offers two powerful operators for this purpose such as ANY and ALL Operators. These operators allow for more complex and flexible data retrieval, enabling comparisons between
5 min read
PL/SQL LIKE Operator
The PL/SQL LIKE operator is a powerful tool used in SQL queries to search for patterns in character data. It allows you to match strings based on specific patterns defined by wildcards. This operator is commonly used in SELECT, UPDATE, and DELETE statements to filter records based on partial or comp
6 min read