0% found this document useful (0 votes)
27 views3 pages

SQL Like Opt.

The SQL LIKE operator is used to search for patterns in text data within a database table. It supports two wildcard characters: % to match zero or more characters, and _ to match a single character. Examples demonstrate using LIKE to find names starting with or containing specific strings, or matching a certain pattern of characters.

Uploaded by

Bhavuk Tomar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views3 pages

SQL Like Opt.

The SQL LIKE operator is used to search for patterns in text data within a database table. It supports two wildcard characters: % to match zero or more characters, and _ to match a single character. Examples demonstrate using LIKE to find names starting with or containing specific strings, or matching a certain pattern of characters.

Uploaded by

Bhavuk Tomar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

LIKE OPERATOR

 %: Represents zero, one, or multiple characters. For


example, '%apple%' would match any string that
contains "apple" as a substring, regardless of what
comes before or after it.
 _: Represents a single character. For example,
'h_t' would match "hat," "hot," or any other
three-letter word with "h" as the first character, "t"
as the last character, and any character in between.

EXAMPLES ARE GIVEN BELOW:


LIKE Operator Description

WHERE CustomerName LIKE 'a%' Finds any values that start with "a"

WHERE CustomerName LIKE '%a' Finds any values that end with "a"

WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any position

WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second position

WHERE CustomerName LIKE 'a_%' Finds any values that start with "a" and are at least 2 characters in length

WHERE CustomerName LIKE 'a__%' Finds any values that start with "a" and are at least 3 characters in length

WHERE ContactName LIKE 'a%o' Finds any values that start with "a" and ends with "o

SQL LIKE OPERATOR


ChatGPT
The SQL LIKE operator is used to search for patterns in text data within a database table. It is commonly used in conjunction
with the % (percent) and _ (underscore) wildcards to perform partial matching of strings. The LIKE operator is case-sensitive in
most database systems, but some may offer a case-insensitive version (e.g., ILIKE in PostgreSQL).

Here's the basic syntax of the LIKE operator:

sqlCopy code
SELECT column1, column2, ... FROM table_name WHERE column_name LIKE pattern ;

The pattern can contain the wildcards:

 %: Represents zero, one, or multiple characters. For example, '%apple%' would match any string that contains "apple" as a
substring, regardless of what comes before or after it.
 _: Represents a single character. For example, 'h_t' would match "hat," "hot," or any other three-letter word with "h" as the
first character, "t" as the last character, and any character in between.

Let's see some examples:


1. To find all names starting with "John":
sqlCopy code
SELECT * FROM employees WHERE name LIKE 'John%' ;
2. To find all names containing "Smith":
sqlCopy code
SELECT * FROM employees WHERE last_name LIKE '%Smith%' ;
3. To find all names with exactly five characters where the third character is "a":
sqlCopy code
SELECT * FROM customers WHERE name LIKE '__a__' ;

It's important to note that using the LIKE operator with a leading wildcard (e.g., '%something') can be inefficient in large
databases, as it may require a full table scan to find the matching rows. If possible, it's best to avoid leading wildcards for better
performance.

Additionally, some database systems provide more advanced pattern matching capabilities using regular expressions (e.g.,
REGEXP or RLIKE), which allow for more complex pattern matching scenarios.

You might also like