Lab 3 Muhammad Abdullah (1823-2021)
Lab 3 Muhammad Abdullah (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
a) Equal to
SELECT EmployeeID, FirstName, LastName, HireDate, City FROM Employees WHERE City =
'London';
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';
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,...);
➢ 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;
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';
Lab Tasks:
1) Create a table and insert 20 different records in it having at least 5 fields.
• Equal to
• Greater than
Logical Operators
• OR
• NOT
• BETWEEN
• NULL
• NOT NULL
LIST OPERATOR:
SQL WILDCARDS: