LIKE and BETWEEN in SQL

To compare values in a database, the LIKE and BETWEEN operators in SQL are used. In this tutorial segment, we will talk about:

  • In SQL, LIKE
  • In SQL BETWEEN

LIKE The SQL Operator

In SQL, LIKE is used to retrieve documents when there is a clear pattern. For starters, imagine that we want to extract the name of an individual which includes ‘john’. So, if there are three individuals called Johnathan, Johnny, and Marcus, then we can extract records where the names are Johnathan and Johnny with the aid of the LIKE in SQL, as they have the appropriate pattern present in them.

In SQL, LIKE is used for wildcard characters, such as:

  • Percentage symbol: This is a replacement for a character of zero, one, or more.
  • Symbol of Underscore: It is a replacement for a single character.

SQL Like Syntax:

LIKE in SQL

Where the keywords are SELECT, FROM, WHERE, and LIKE, column list is the list of columns, tablename is the table name, column N is the column name, and a semicolon is followed by a pattern.

Let’s do some operations in SQL using the LIKE query. Extract all documents where the name of the worker begins with the letter ‘J’.

LIKE operations in SQL

Click the execute button after writing the question to check for errors. A response looks like ‘Commands performed successfully’ until the question is executed.

Now, let’s remove all the documents where the age of the worker is 20.

LIKE operations
LIKE operations

Click the execute button after writing the question to check for errors. A notification shows as ‘Commands successfully accomplished’ until the question is executed.

BETWEEN in SQL

To choose values within a given set, the BETWEEN operator in SQL is used. For instance, we have to use the SQL BETWEEN operator to remove only those records where the person’s age is between 20 and 25.

Syntax:

BETWEEN Syntax in SQL

Where the keywords are Pick, FROM, WHERE, BETWEEN, and AND, column list is a column list, tablename is the name of the table, column N is the name of the column, and val1 is the minimum range value, and val2 is the maximum range value, accompanied by a semicolon. Let’s show workers aged between 25 and 35 from the employee table now,

BETWEEN operations in SQL

Click the execute button after writing the question to check for errors. The table appears until the question is executed.

Let’s see executive compensation from the employee table of between $60,000 and $120,000.

BETWEEN operations in SQL

Click the execute button after writing the question to check for errors. The table appears until the question is executed.

Well, this takes us to the conclusion of this portion of this guide. Now, you’re able to start using SQL with LIKE and SQL with BETWEEN to compare the values in your table.

Leave a Reply