Lecture 05 - SQL Commands
Lecture 05 - SQL Commands
Output:
Using the AND operator and OR operator with
the DELETE Statement
Product
Output
• Write a SQL query that deletes records from Product table having category id
25 or/either product id is less than 4 and product name should not be Banana?
• DELETE from Product where category_id = 25 OR (product_id < 4 AND
product_name <> 'Banana');
SQL BETWEEN Operator
• The Between operator selects values within a given range. The values
can be numbers, text, or dates.
• The Between operator is inclusive: begin and end values are
included.
• Between Syntax
• Select column_name(s) from table_name where column_name
between start_value and end_value;
Product Table
• Consider the following Product Table
• Write a SQL query that shows all Products with a price between 10 and
20?
• Select * from Product where Price between 10 and 20;
Supplier Table Output
• Write a SQL query that shows all customers that are located in "Germany", "France"
and "UK"?
• SELECT * FROM Customers WHERE Country IN ('Germany', 'France', 'UK');
• SELECT * FROM Customers WHERE Country='GERMANY' OR Country='FRANCE' OR
Country ='UK';
Using Not operator with IN Operator
• Write a SQL query that shows all customers that are not
located in "Germany", "France" and "UK"?
• SELECT * FROM Customers WHERE Country NOT IN
('Germany', 'France', 'UK');
• The above query is equivalent to
• SELECT * FROM Customers WHERE NOT (Country='Germany'
OR Country='France' OR Country= 'UK');
Use of BETWEEN Operator with IN Operator
and NOT Operator
• Write a SQL query that shows all products with a price
BETWEEN 10 and 20. In addition, it do not show products
with a CategoryID of 1,2, or 3?
• SELECT * FROM Products WHERE (Price BETWEEN 10
AND 20) AND NOT CategoryID IN (1,2,3);
24
SELECT DISTINCT
• The SELECT DISTINCT statement is used to return only
distinct (different) values.
• Inside a table, a column often contains many duplicate
values and sometimes you only want to list the
different (distinct) values.
• SELECT DISTINCT column1, column2… FROM table_name;
Emp Output
ID Name Age Address Salary Age
1 Zeeshan 32 CII Johar Town 2000 32
2 Ali 25 J2 Block Wapda 1500 25
Town 23
3 Rizvi 23 111 Green Town 2000 27
4 Hamza 25 114 Minhas Colony 6500 22
5 Yasir 27 233 Shah Jamal 8500 24
6 Ali 22 J2 Block Wapda 4500
Town
7 Hammad 24 14 A Nespak Society 10000
The percent sign and the underscore can also be used in combinations!
Like operator Syntax
SELECT COLUMN1, COLUMN2… from TABLE where
Column_Name LIKE pattern;
Category
Q: Write a SQL query to retrieve
those categories id’s and their
names whose category id ends
with 5?
Select category_id, category_name from
Category where category_id like '%5';
LIKE operator
Output
category_id category_name
25 Deli
75 Bakery
125 Technology
Write a SQL query to retrieve those category records
whose second character of category name is e or a?
Select * from category where category_name like '_e%'
or category_name like '_a%';
LIKE operator
Write a SQL query to show those category records in
which 'er' comes anywhere in category names?
Select * from category where category_name like
'%er%';
Examples
LIKE Operator Description
WHERE CustomerName LIKE 'a%' Finds any values that start with "a"
WHERE CustomerName LIKE '%a' Finds any values that end with "a"
WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any position
WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second
position
WHERE CustomerName LIKE 'a__%' Finds any values that start with "a" and are at least
3 characters in length
WHERE ContactName LIKE 'a%o' Finds any values that start with "a" and ends with
"o"
Select Statement with Order by clause
• The order by clause is used to sort the rows. The process of arranging data
or records in a sequence is called sorting. A sort can be ascending or
descending.
• In ascending sort, the smallest value is placed at first position and largest
value is placed at the last position.
• In descending sort, the largest value is placed at first position and smallest
value is placed at the last position.
• SQL uses ASC keyword to specify ascending sort and DESC keyword for
descending sort. If nothing is specified, ASC is used as default.
• Syntax:
Select * from TableName Order by ColumnName;
Order by clause Descending
Write a query that displays category table in descending order with
respect to category id?
• Write a query to show maximum price from the above table of Products?
LargestPrice
• Select Max(Price) as LargestPrice From Products;
22
SQL MIN Aggregate Function
• The MIN function returns the smallest value of the selected column.
• MIN() Syntax
• SELECT MIN(column_name) FROM table_name;
• Write a query to show minimum price from the above table of Products?
• Select Min(Price) as 'Smallest Price' From Products; Smallest Price
10
SQL COUNT Aggregate Function
• The COUNT() function returns the number of rows that matches a
specified criteria.
• COUNT() Syntax
• SELECT COUNT(column_name) FROM table_name;
• Example
• SELECT COUNT(ProductID) as 'Total Product ID' FROM Products;
• Output
Total Product ID
5
SQL SUM Aggregate Function
• The SUM() function returns the total sum of a numeric column.
• SUM() Syntax
• SELECT SUM(column_name) FROM table_name
• Example
• SELECT SUM(SupplierID) as 'Total Suppliers' FROM Products;
Total Suppliers
7
Selection of Multiple Aggregate Functions
SELECT Count(ProductID), SUM(SupplierID), MAX(Price), MIN(Price)
FROM Products;
Count(ProductID) SUM(SupplierID) MAX(Price) MIN(Price)
5 7 22 10