0% found this document useful (0 votes)
44 views11 pages

Lab 3 Muhammad Abdullah (1823-2021)

This document discusses SQL operators and wildcards. It defines comparison, logical, list, and range operators and provides examples of how each can be used in a SELECT statement WHERE clause. It also covers NULL values, DISTINCT, and wildcards used with the LIKE operator. Finally, it lists tasks to create a table, insert records, apply operators, and use wildcards on the data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views11 pages

Lab 3 Muhammad Abdullah (1823-2021)

This document discusses SQL operators and wildcards. It defines comparison, logical, list, and range operators and provides examples of how each can be used in a SELECT statement WHERE clause. It also covers NULL values, DISTINCT, and wildcards used with the LIKE operator. Finally, it lists tasks to create a table, insert records, apply operators, and use wildcards on the data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Faculty of Computing and Information Technology (FCIT)

Department of Computing Indus University, Karachi

NAME OF STUDENT: Muhammad Abdullah ID No: 1823 - 2021

Lab 03
Implementation of SQL Operators and Wildcard

➢ Select Statement:
The SELECT ... FROM Clause
The most basic SELECT statement has only 2 parts:
(1) the columns you want to display and
(2) from the table(s) these columns belong to.
EXAMPLE 1
SELECT EmployeeID, FirstName, LastName, HireDate, City FROM Employees
Select * from Employees

Where Clause
By adding a WHERE clause to the SELECT statement, we add one (or more) conditions that must
be met by the selected data. This will limit the number of rows that answer the query and are
fetched. In many cases, this is where most of the "action" of a query takes place.

➢ Operators
Definition: An operator is a character or a reserved word that is used to specify a condition, or
combine two or more conditions. The operators are used with the WHERE clause in the SELECT
statement to set the filter criteria for data.
Types: There are four types of operators:
1. Comparison Operators
2. Logical Operators
3. List Operators
4. Range Operators

1. Comparison Operator = Comparison operators are used to compare the column data with
specific values in a condition.
Comparison Operator Description
= Equal to
> Greater than
< Less than

>= Greater than or equal to


<= Less than or equal to
<> Not equal to

SQL WHERE and OPERATOR Syntax


SELECT column_name,column_name FROM table_name WHERE column_name operator value

a) Equal to
SELECT EmployeeID, FirstName, LastName, HireDate, City FROM Employees WHERE City =
'London';

b) Greater than or equal to


SELECT ProductName,UnitsInStock,UnitsOnOrder FROM Products WHERE
UnitsOnOrder >=70

c) Not equal to
If you wanted to get the opposite, the employees who do not live in London, you would write
SELECT EmployeeID, FirstName, LastName, HireDate, City FROM Employees WHERE
City <> 'London'
1. Logical Operators = Logical operators are used to test if a specified condition is true or not.
They are also used to combine multiple conditions.
Some of the commonly used logical operators are:
OR
• Retrieves rows that meet any one of the specified conditions.
AND
• Retrieves rows that meet both the conditions.

NOT
• Retrieves rows that do not meet the specified condition.
BETWEEN
• Retrieves rows where the tested value falls within the specified range.

a. OR OPERATOR
SELECT CompanyName,City FROM Customers WHERE City = 'London' OR City = 'Madrid'

b. AND OPERATOR
SELECT * FROM Customers WHERE Country='Germany'
AND City='Berlin';

USING AND and OR OPERATOR


SELECT * FROM Customers WHERE Country='Germany' AND (City='Berlin' OR
City='Mannheim');
c. Between Operator
SELECT EmployeeID, FirstName, LastName, HireDate, City FROM Employees
WHERE HireDate BETWEEN '1-june-1992' AND '15december-1993‘

1. List Operator
The IN list operator checks if the result of the expression meets one of the specified values.
The syntax for using a list operator in a query is:
Syntax: SELECT column_name(s) FROM table_name WHERE column_name IN
(value1,value2,...);

Query: SELECT CompanyName,City FROM Customers WHERE City IN


('London','Madrid','Paris')

SELECT CompanyName,City FROM Customers WHERE City NOT IN


('London','Madrid','Paris')

➢ Null Clause
A NULL value in a column means that there is no data in the column. You can retrieve rows which
contain NULL values using the IS NULL keyword with the WHERE clause.
SELECT <column_names>
FROM <table_name>
WHERE <column_name> IS NULL
SELECT DISTINCT Statement
The SELECT DISTINCT statement is used to return only distinct (different) values.
SELECT DISTINCT column_name,column_name
FROM table_name;
SELECT DISTINCT City FROM Customers;

Between Operator (REGION OPERATOR)


• Note that SQL also has a special BETWEEN operator that checks to see if a value is between
two values (including equality on both ends).
• Retrieves rows where the tested value falls within the specified range.

NULL KEYWORD
SELECT CompanyName,City,Region,Country FROM Customers WHERE REGION IS
NULL

➢ SQL Wildcards
• A wildcard character can be used to substitute for any other character(s) in a string.
• In SQL, wildcard characters are used with the SQL LIKE operator.
• SQL wildcards are used to search for data within a table.
SELECT * FROM Customers WHERE City LIKE '_erlin';

SELECT ProductId, ProductName, UnitPrice FROM Products WHERE ProductName


LIKE 'Cha_' OR ProductName LIKE 'Chan_'

Lab Tasks:
1) Create a table and insert 20 different records in it having at least 5 fields.

2) Apply all operators on these record.

3) Apply SQL wildcards on these records.


Lab Tasks:
Create a table and insert 20 different records in it having at least 5 fields.

4) Apply all operators on these records.

Comparison Operator Description

• Equal to
• Greater than

• < Less than

• >= Greater than or equal to

• <= Less than or equal to


• <> Not equal to

Logical Operators
• OR

• NOT
• BETWEEN

• NULL

• NOT NULL
LIST OPERATOR:

5) Apply SQL wildcards on these records.

SQL WILDCARDS:

WITH WHERE CLAUSE:

You might also like