Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

Lecture 05 - SQL Commands

Uploaded by

talha.farooq6625
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lecture 05 - SQL Commands

Uploaded by

talha.farooq6625
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

‫ْ‬ ‫ﱠ‬ ‫ٰ‬ ‫ْ‬ ‫ﱠ‬

‫ﷲ اﻟﺮﺣﻤﻦ اﻟﺮ ِﺣ ِﻢ‬ ‫ْ‬


‫ِ ﺴ ِﻢ ِ‬
‫َﺻ ْﺪري َو َ ﱢ ْ أ ْﻣﺮي َو ْ‬
‫اﺣﻠﻞْ‬ ‫َر ﱢب ا ْ َ حْ‬
‫ِ‬ ‫ِ‬
‫ُﻋ ْﻘ َﺪ ًة ﻣ ْﻦ ﻟ َﺴﺎ َ ْﻔ َﻘ ُﻬﻮا َﻗﻮْ‬
‫ِ‬ ‫ِ ِ ِ‬
SQL Commands -DML
Relational and Logical
Operators
Lecture # 5

Course Instructor: Adnan Rafique


Relational and Logical
Operators
Country Table
CountryID Name Population Year
7 Pakistan 20000000 2012
8 Bangladesh 400000 1990
9 Bengal 500000 1993
10 India 30000000 1996
11 New Zealand 60000000 1997
12 Australia 700000 1999
Multiple AND conditions
Write a SQL query to show those countries name, population having
population greater than or equal to 100000 and population less than
or equal to 500000 and year is greater than 1990?
Select Name, Population from country where (population>=100000 and
population<=500000) and year>1990;
Output:
Name Population
Bengal 500000
Multiple AND conditions
Write a SQL query to show those countries name, population having
population greater than or equal to 100000 and population less than
or equal to 500000 and year is greater than 1990 or year is less than
2000?
Select Name, Population from country where (population>=100000 and
population<=500000) and year>1990 or year<2000;
Output:
Supplier Table
Use of AND Operator and OR operator with
Select statement
Write a SQL query to show those suppliers records having state
California and supplier id is not 900 or either their supplier id is 100?
Select * from suppliers where (state='California' and supplier_id <> 900) or
supplier_id=100;  It is Comparison/Relational
Output: operator
 Also logical operator NOT
can be used here
Customer Table
Using the AND operator and OR operator with
the UPDATE Statement
Write a query to update favorite_website to 'techonthenet.com'
having customer_id 6000 or having customer id greater than
7000 and whose last name is not Johnson?
Update customers set favorite_website='techonthenet.com' WHERE
(customer_id = 6000 OR customer_id > 7000) AND last_name <> 'Johnson');

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 query that displays all suppliers having supplier id between


300 and 600?
• Select * from Supplier where Supplier_id between 300 and 600;
• The above query is equivalent to
• Select * from Supplier where Supplier_id >=300 and Supplier_id<=600;
Using Not operator with Between Operator
• Write a SQL query to display the products outside the range of the
previous example, use NOT BETWEEN?
• SELECT * FROM Product WHERE Price NOT BETWEEN 10 AND 20;
• The above query is equivalent to
• Select * From Product where not (Price>=10 and Price<=20);
• The above query is equivalent to
• Select * From Product where (Price<10 or Price>20);
SQL IN Operator
• The IN operator allows you to specify multiple values in a WHERE
clause.
• The IN operator is a shorthand for multiple OR conditions.
• IN Syntax
• SELECT column_name(s) FROM table_name WHERE column_name IN
(value1, value2, ...);
Customer Table

• 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

Write a sql query to show distinct age of Employees?


Select distinct age from Emp;
Distinct on Multiple columns
Write a SQL query to show distinct name and address of
employees?
Select distinct name,address from Emp;
Name Address
Zeeshan CII Johar Town
Ali J2 Block Wapda Town
Rizvi 111 Green Town
Hamza 114 Minhas Colony
Yasir 233 Shah Jamal
Hammad 14 A Nespak Society
LIKE Operator
• The LIKE operator is used in a WHERE clause to search for a
specified pattern in a column.
• There are two wildcards often used in conjunction with the LIKE
operator:
%
The percent sign represents zero, one, or multiple characters
_
The underscore represents a single character

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?

Select *from category order by category_id desc;


The SQL SELECT LIMIT Clause
• The SELECT limit clause is used to specify the number of records to return.
• The SELECT limit clause is useful on large tables with thousands of records.
Returning a large number of records can impact performance.
• Syntax:
• SELECT column_name(s) FROM table_name WHERE condition LIMIT
number;
The SQL SELECT LIMIT Clause
Department
DeptID DeptName DeptChair
1 CS Farooq Ahmad
2 SE James Berlin
3 IT Yaseen Virk
4 EE Saud Gohar
5 Mech Zohaib Tariq
6 Islamic Daud Warrich
Write a sql query to show top 3 Department ID, Department Name and
Department chair?
Select * from Department LIMIT 3;
The SQL SELECT LIMIT Clause
Result of Previous Query:
DeptID DeptName DeptChair
1 CS Farooq Ahmad
2 SE James Berlin
3 IT Yaseen Virk
Aggregate Functions
• Aggregate functions generate summary value.
Aggregate functions generate a single value from a set
of values. The following are the aggregate functions in
SQL.
• Average
• Maximum
• Minimum
• Count
• Sum
Product table
SQL AVG Aggregate Function
• The AVG() function returns the average value of a numeric column.
• Syntax
• SELECT AVG(column_name) FROM table_name;
• Example
• SELECT AVG(Price) FROM Products;
• Output
AVG(Price)
18.07
SQL MAX Aggregate Function
• The MAX function returns the largest value of the selected column.
• MAX() Syntax
• SELECT MAX(column_name) FROM table_name;

• 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

SELECT Count(ProductID) as TotalProducts,SUM(SupplierID) as


TotalSuppliers, MAX(Price) as LargestPrice, MIN(Price) as SmallestPrice
FROM Products;
TotalProducts TotalSuppliers LargestPrice SmallestPrice
5 7 22 10

You might also like