Day 13 – SQL LIKE Operator
In SQL, the
LIKE operator is used to search for patterns in a column of text data. The
LIKE operator is often used in combination with wildcard characters to search for specific patterns.
The basic syntax for using the
LIKE operator is as follows:
SELECT column_name FROM table_name WHERE column_name LIKE pattern;
column_name is the name of the column you want to search,
table_name is the name of the table containing the column, and
pattern is the search pattern you want to use.
There are two wildcard characters that can be used with the
%(percent sign): This represents zero or more characters. For example, the pattern
'%test%'would match any string that contains the word “test” anywhere in the column.
_(underscore): This represents a single character. For example, the pattern
'a_%'would match any string that starts with the letter “a” followed by any single character.
For example, suppose you have a table called
employees with columns
email_address. If you want to find all employees whose email address ends with “@example.com”, you would use the following SQL statement:
SELECT * FROM employees WHERE email_address LIKE '%@example.com';
This statement will return all rows from the
employees table where the
email_address column ends with “@example.com”.