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 Operator in SQL
The LIKE Operator 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. In SQL, it 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.
Get 100% Hike!
Master Most in Demand Skills 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 “e” 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 us take an example to understand this better, imagine that we want to extract a person’s name from the employee table below.
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%';
After writing the query, click on the execute button to check for errors. Once the SQL query is executed, a message appears like “Commands completed successfully”. As we can see in the result, we got only two rows in which the names start with the letter “j”.
Now, let’s take another example in which we extract all records from the database where the employee age is in the twenties.
Select * from employee where e_age LIKE '2_';
Once again, after writing the query you have to click on the execute button to check for errors. If no errors are found, the output containing information about all the employees whose age ranges from 20 to 29, will be displayed as it is.
BETWEEN Operator 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 BETWEEN query in SQL.
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, 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 aged between 25 and 35 from the above employee table.
Select * From employee where e_age BETWEEN 25 AND 35;
After writing the query, click on the execute button to check for errors. Once the query is executed, the table appears.
Let’s display the salaries of employees between $60,000 and $120,000 from the employee table.
Select * From employee where e_salary BETWEEN 60000 AND 1200000;
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.