In this article, we will be exploring the SQL Like and the SQL Not Like operators. These operators help in filtering specific patterns from the columns.
1. About SQL Like & SQL Not Like
Let us start with understanding what both these operators are.
1.1 What is SQL Like operator?
SQL Like operator is used to filtering the data according to a specific pattern in a particular column. This operator comes in the WHERE clause of the query.
There are wildcard symbols, which represent the pattern we want to retrieve from the table. Some of the most frequently used wildcard symbols are:
- % – This percentage wildcard symbol represents zero, one or more characters.
- _ – This underscore wildcard symbol represents exactly 1 character.
1.2 What is SQL Not Like operator?
SQL Not Like the operator works in a way opposite to that of the operator. The
SQL Not Like operator excludes the matching pattern from the specific column.
SQL Not Like operator uses the wildcard symbols as well.
Now that we know what each of the operators does, we are going to look at the syntax for both of the operators.
2.1 SQL Like operator syntax
SELECT column1, column2 FROM tableName WHERE columnName LIKE patern
2.2 SQL Not Like operator syntax
SELECT column1, column2 FROM tableName WHERE columnName NOT LIKE patern
Finally, we will see the usage of
SQL like and
SQL Not Like operators with an example.
- Let us start with creating a database named data and subsequently a table named student.
2. Now, let us insert some rows of data using the SQL Insert Into statement.
3. Let us now use the
SQL Like operator. We have used the
SQL Like operator to filter on the basis of – all names starting with A, all names starting with A, and third letter as all names with A as the second letter.
4. Now, let us now use the
SQL Not Like operator. We have used the SQL Not Like operator to filter on the basis of – all country names which don’t start with ‘I’, all country names with don’t end with ‘a’, and country names which neither start with ‘A’ nor end with ‘a’.
In this article, we discuss the two operators
SQL Like and
SQL Not Like. We also discussed the wildcard symbols like the percent (%) symbol, and underscore ( _ ) symbol.
5. Download the source code
To implement the examples implemented above, download the source code.
You can download the full source code of this example here: SQL Like – SQL Not Like
Last updated on Feb. 24th, 2022