Lab Manual 06 - Logical Operators
Lab Manual 06 - Logical Operators
Logical Operator
The Logical Operator is nothing but which returns the result in one form, i.e., either it
will display the query is true, or the query is false. The results displayed to combine or
merge more than one true or false data.
The Logical Operators in SQL are as follows:
1. AND OPERATOR
2. OR OPERATOR
3. NOT OPERATOR
4. BETWEEN OPERATOR
5. IN OPERATOR
6. LIKE OPERATOR
Consider we have an employees table with the following data:
Engineer
8 Husnain Raza 45000 Peshawar Software 2020-09-10 26
Engineer
9 Hira Saleem 50000 Multan Software 2021-01-01 25
Tester
10 Jawad Hassan 60000 DI Khan Project 2020-10-02 24
Manager
11 Bilal Hussain 45500 Pattoki HR 2019-01-02 26
12 Riaz Ahmad 50500 Pattoki Software 2016-09-10 25
Developer
13 Dur e Shahwar 50000 Lahore HR 2013-12-12 23
14 Mariyam Ali 40000 Islamabad Project 2017-11-10 25
Manager
15 Mahnoor 38000 Lahore Software 2019-03-05 20
Developer
1. AND Operator
The SQL AND operator is used with the where clause in the SQL Query. AND operator
in SQL returns only those records which satisfy both the conditions in the SQL query.
Example:
Write a query to retrieve only those records of employees from the employees table
where the designation is 'Project Manager' and the City to which the employee belongs
to is DI Khan.
Query:
Output:
E_ID Name Salary City Designation Date_of_Joining Age
10 Jawad 60000 DI Khan Project 2020-10-02 24
Hassan Manager
2. BETWEEN Operator
This operator displays the records which fall between the given ranges in the SQL
query.
Example:
Write a query to retrieve only those records of an employee from the employees table
where employee salary lies between 50000 to 90000.
Query:
SELECT * FROM employees WHERE Salary BETWEEN 50000 AND 90000;
There are nine records in the employees table whose salary falls between 50000 to
90000.
3. SQL OR Operator
The SQL OR operator is used with the where clause in an SQL Query. AND operator in
SQL returns only those records that satisfy any of the conditions in the SQL query.
Example:
Write a query to retrieve only those records of employees from the employees table
where the employee's designation is “Software Developer” or the city to which the
employee belongs is Lahore.
Department of Computer Science, UMT, Lahore. 3 Riaz Ahmad
Lab Manual: Database Systems
Query:
SELECT * FROM employees
WHERE Designation = "Software Developer" OR City = " Lahore ";
4. SQL IN Operator
When we want to check for one or more than one value in a single SQL query, we use
IN operator with the WHERE clause in a SELECT query.
Example:
Write a query to retrieve only those records of employees from the employees table
where the city to which the employee belongs to is either Lahore, Karachi, or DI Khan.
Query:
SELECT * FROM employees WHERE City IN ("Lahore", "Karachi", "DI Khan");
Example:
Write a query to retrieve only those records of employees from the employees table
where the employee's designation is not Project Manager.
Query:
SELECT * FROM employees WHERE NOT Designation = "Project Manager";
Products Table:
productid productname category price
1 iPhone 15 Mobile 1200
2 Samsung Galaxy S Mobile 1000
3 HP Laptop Laptop 1500
4 Dell Inspiron Laptop 1400
5 Apple MacBook Laptop 2500
6 Lenovo ThinkPad Laptop 1300
7 Xiaomi Redmi 12 Mobile 800
8 Oppo Reno 10 Mobile 900
9 Sony Headphones Accessories 200
10 JBL Speaker Accessories 250
11 Canon EOS 90D Camera 2200
12 Nikon D750 Camera 2300
13 Samsung Tablet Tablet 950
14 Apple iPad Tablet 1100
15 ASUS ROG Laptop Laptop 2700
16 Dell XPS 13 Laptop 1800
17 Bose Earbuds Accessories 300
18 Huawei Tablet Tablet 700
19 Google Pixel 7 Mobile 1100
20 Microsoft Surface Laptop 2000
Create Products table and insert data into the table:
CREATE TABLE Products (
productid INT PRIMARY KEY,
productname VARCHAR(100),
category VARCHAR(50),
price INT
);
INSERT INTO Products (productid, productname, category, price) VALUES
(1, 'iPhone 15', 'Mobile', 1200),
(2, 'Samsung Galaxy S', 'Mobile', 1000),
(3, 'HP Laptop', 'Laptop', 1500),
(4, 'Dell Inspiron', 'Laptop', 1400),
(5, 'Apple MacBook', 'Laptop', 2500),
(6, 'Lenovo ThinkPad', 'Laptop', 1300),
(7, 'Xiaomi Redmi 12', 'Mobile', 800),
(8, 'Oppo Reno 10', 'Mobile', 900),