0% found this document useful (0 votes)
106 views

DBMS Lab ASS#2

The document discusses the IN, BETWEEN, and LIKE operators in SQL Server. The BETWEEN operator returns values within a specified range. The IN operator searches for values that match any in a set of multiple values. The LIKE operator searches for character strings matching a pattern using wildcard characters like '%' to represent any number of characters. Examples demonstrate using each operator to return records from an Employee_5 table that meet criteria on the ID, Department, and City fields.

Uploaded by

Syed Aqib Raza
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views

DBMS Lab ASS#2

The document discusses the IN, BETWEEN, and LIKE operators in SQL Server. The BETWEEN operator returns values within a specified range. The IN operator searches for values that match any in a set of multiple values. The LIKE operator searches for character strings matching a pattern using wildcard characters like '%' to represent any number of characters. Examples demonstrate using each operator to return records from an Employee_5 table that meet criteria on the ID, Department, and City fields.

Uploaded by

Syed Aqib Raza
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignment # 02

By

AQIB RAZA
CIIT/FA19-BCS-126/ATK
Teacher: Mr. WASEEM KHAN
Submission Date: Monday, March 8, 2021

COMSATS University Islamabad


Attock Campus
Spring, 2021
IN BETWEEN AND LIKE
OPERATORS IN SQL
SERVER
Understanding IN BETWEEN and LIKE Operators in SQL Server:

Let us understand the IN, Between and Like operators with examples. We are going to use the
following Employee_5 tables to understand these special operators.

SQL Script to create database EmployeeData_1, Employee_5 table and populate Employee_5
table with test data.
BETWEEN Operator in SQL Server:

The BETWEEN operator in SQL Server is used to get the values within a range. Generally, we use
the BETWEEN operator in WHERE clause to get values within a specified range. For example, if
you want to fetch all the employees between id 3 and 7 from the Employee_5 table, then you
need to use the BETWEEN operator.

SELECT* FROM Employee_5 WHERE id BETWEEN 3 AND 7

The above SQL statement will return records from the Employee_5 table where the
employee_5 id is between 3 and 7. Following is the output of the above SQL query.
IN Operator in SQL Server:

The IN Operator in SQL Server is used to search for specified values matches any value in the set
of multiple values it accepts. Generally, we will use this IN operator in WHERE clause to
compare column or variable values with a set of multiple values. For example, if you want to
fetch all the employees whose department is either IT or HR, then you could write the SQL
Query using the IN Operator as shown below.

SELECT* FROM Employee_5 WHERE Department IN ('IT','HR')

Following is the output of the above SQL query.

LIKE Operator in SQL Server:

This is one of the most frequently used operators in SQL Server. The LIKE operator in SQL Server
is used to search for character string with the specified pattern using wildcards in the column.
In SQL Server, pattern means its specific string of characters with wildcards to search for
matched expressions. Generally, we will use this LIKE operator in WHERE clause. Here, we will
try to understand the LIKE operator using different types of wildcard characters.

Using ‘%’ wildcard


Example1:
The following SQL query will return all employees whose CITY starts with the character ‘A’
followed by any string of characters. This is because here we mentioned a pattern like ‘A%’.
Here ‘%’ is a wildcard character that we will use before or after characters to search for the
required matched string of characters.

SELECT * FROM Employee_5 WHERE CITY LIKE 'A%'

Following is the output of the above SQL query.

Example2:

The following SQL query will return all employees whose CITY ends with the character ‘k’. This is
because here we mentioned the pattern as ‘%a’. This means return all records whose name
ends with the character ‘a’.

SELECT * FROM Employee_5 WHERE CITY LIKE '%K'

Following is the output of the above SQL query.

Example3:

The following SQL query will return all employees with CITY containing the word ‘am’ anywhere
in the name column because we mentioned patterns like ‘%am%’. This means it will check for
the respective word anywhere in column irrespective of characters in front or back.

SELECT * FROM Employee_5 WHERE CITY LIKE '%am%'


Following is the output of the above SQL query.

You might also like