sql

SQL Like – SQL Not Like

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?

The 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:

  1. % – This percentage wildcard symbol represents zero, one or more characters.
  2. _ – This underscore wildcard symbol represents exactly 1 character.

1.2 What is SQL Not Like operator?

The 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.

Similarly, the SQL Not Like operator uses the wildcard symbols as well.

2. Syntax

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

3. Example

Finally, we will see the usage of SQL like and SQL Not Like operators with an example.

  1. Let us start with creating a database named data and subsequently a table named student.
SQL Like - Create Database & Table
Creating database – data and table – student

2. Now, let us insert some rows of data using the SQL Insert Into statement.

SQL Like - Insert data into table
Inserting data into table

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.

SQL Like - LIKE Operator
Examples using Like operator

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’.

SQL Like - NOT LIKE Operator
Examples using Not Like operator

4. Summary

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.

Download
You can download the full source code of this example here: SQL Like – SQL Not Like

Last updated on Feb. 24th, 2022

Simran Koul

Simran has graduated as a Bachelor of Engineering in Computer Science from Chitkara University. She has undergone a 6-months long comprehensive industrial training at the reputed Centre for Development of Advanced Computing (C-DAC), where she worked on a project including the likes of Java, JSP, Servlets while the UI-UX through the pioneering HTML, CSS and JS. Her inquisitive nature and the seed of curiosity keeps her on the toes to find material to write about. Along with her interests in Software Development, she is an ardent reader and always ready-to-write writer.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button