LIKE and BETWEEN in SQL

LIKE and BETWEEN operators in SQL are used to compare values in a database. In this tutorial section, we will discuss on:

Watch this Like and Between operators in SQL

LIKE and BETWEEN Operators in SQL

LIKE Operator in SQL

The LIKE in SQL is used to extract records where a particular pattern is present. In a WHERE clause, the LIKE operator is used to look for a certain pattern in a column. The LIKE operator in SQL has two wildcard characters, such as:

  • Percentage symbol: It is a substitution for zero, one, or more characters.
  • Underscore symbol: It is a substitution for a single character.

Interested in learning SQL? Enroll in our SQL Training now!

Here are the examples of how we use the LIKE operator using wildcard

Like Operator Description
WHERE EmployeeName LIKE ‘e%’ Finds values that start with “e” 
WHERE EmployeeName LIKE ‘%e’ Finds values that end with “e”
WHERE EmployeeName LIKE ‘%en%’ Finds values that have “en” at any position
WHERE EmployeeName LIKE ‘_e%’ Finds values that have “e” only in the second position
WHERE EmployeeName LIKE ‘e__%’ Finds values that start with “e” and are at least 3 characters in length
WHERE EmployeeName LIKE ‘e%0’ Finds values that start with “a” and end with “0”

SQL Like Syntax

SELECT column_list
FROM tablename
WHERE column_N LIKE ‘_xxxxx%’;

where SELECT, FROM, WHERE, and LIKE are the keywords, column_list is the list of columns, tablename is the name of the table, column_N is the column name, and there is a pattern followed by a semicolon.

Let me take an example to understand better, imagine that we want to extract a person’s name from the below employee table.

Demo Database

e_id e_name e_salary e_age e_gender e_dept
1 john 30000 25 male operations
2 julia 540000 27 female operations
3 anna 125000 27 female analytics
4 bob 50000 24 male support
5 max 540000 29 male operations

Let’s do some operations with the LIKE query in SQL. Extract all records where the employee name starts with the letter ‘J’

Select * from employee where e_name LIKE 'j%';

LIKE OperatorAfter writing the query, click on the execute button to check for errors. Once the query is executed, a message appears like ‘Commands completed successfully. As we can see that in the result, we got only two rows in which the names start with the letter “j”.

Select * from employee where e_age LIKE '2_';

Want to get certified in SQL! Learn from our SQL expert and do excel in your career with intellipaat’s Microsoft SQL certification.

LIKE Operator step 2

Once again, After writing the query you have to click on the execute button to check for errors. As you can see all the ages present in the table are above 20. So the result showed as it is.

Become a Database Architect

BETWEEN in SQL

The BETWEEN operator in SQL is used to select values within a given range. For example, to extract only those records where the age of the person is between 20 and 25, we have to use the SQL BETWEEN operator.

SQL Between Syntax

SELECT column_list
FROM tablename
Where column_N BETWEEN val1 AND val2;

where SELECT, FROM, WHERE, BETWEEN, and AND are the keywords, column_list is a list of columns, tablename is the name of the table, column_N is the column name, and val1 is the minimum value of the range and val2 is the maximum value of the range, followed by a semicolon. Let’s, now, display employees aging between 25 and 35 from the employee table

Select * From employee where e_age BETWEEN 25 AND 35;

Visit our SQL Community to get answers to all your queries!

After writing the query, click on the execute button to check for errors. Once the query is executed, the table appears.
Let’s display salaries of employees between $60,000 and $120,000 from the employee table

Select * From employee where e_salary BETWEEN 60000 AND 1200000;

BETWEEN Operator 3
After writing the query, click on the execute button to check for errors. Once the query is executed, the table appears.
Well, this brings us to the end of this tutorial section. Now, you’re ready to get started with using LIKE in SQL and BETWEEN in SQL for comparing the values in your database.

Get familiar with the top SQL Interview Questions to get a head start in your career!

Course Schedule

Name Date
SQL Training 2021-07-31 2021-07-25
(Sat-Sun) Weekend batch
View Details
SQL Training 2021-08-07 2021-08-01
(Sat-Sun) Weekend batch
View Details
SQL Training 2021-08-14 2021-08-08
(Sat-Sun) Weekend batch
View Details

Leave a Reply

Your email address will not be published. Required fields are marked *