Lec 7
Lec 7
Lecture 7
SQL
2
DDL
Create
Alter
Drop
DML
Insert
Update
Delete
Single Table
Select
Multiple Tables
DCL
Compound Comparison Search Conditions
3
SELECT * Ahmed
Ali
Fahmy
Zidan
111
112
London
Paris
FROM Customer Mark Antony 113 London
Return all Order IDs and count of products in each order line.
orders OrderID ProductID Quantity
100 1 10
SELECT OrderID, Count(ProductID) as X 100 2 17
Order ID X
100 3
102 1
103 4
Qualifying Results by Categories Using the HAVING Clause
9
Return all Order IDs that include more than 3 products in their
OrderLines and their count. orders
OrderID ProductID Quantity
100 1 10
100 2 17
SELECT OrderID, Count(ProductID) 102 2 2
Return all Order IDs that include more than 3 products in their
OrderLines. orders
OrderID ProductID Quantity
100 1 10
SELECT OrderID, Count(ProductID) as X 100 2 17
102 2 2
FROM Orders 100 5 9
GROUP BY OrderID 103 3 3
103 4 4
HAVING X > 3; 103 5 5
103 6 6
Order ID X
103 4
Exercise
11
DML
Multiple Tables
Schema
15
Customer
Customer_ID Customer_Name City State Postal_Code
Product
Product_ID Product_Name Product_Description Product_Finish Standard_Price
Order
Order_ID Order_Date Customer_ID
Order_Line
Order_ID Product_ID Ordered_Quantity
SELECT from Multiple Tables
16
Product Company
PName Price Category Manufacturer Cname StockPrice Country
Gizmo $19.99 Gadgets GizmoWorks GizmoWorks 25 USA
Powergizmo $29.99 Gadgets GizmoWorks Canon 65 Japan
SingleTouch $149.99 Photography Canon Hitachi 15 Japan
MultiTouch $203.99 Household Hitachi
PName Price
SingleTouch $149.99
Join Types
23
Product Purchase
name category prodName store
name store
SELECT Product.name, Purchase.store
Gizmo Wiz
FROM Product
INNER JOIN Purchase Camera Ritz
What’s wrong ?
33
Product Purchase
name category prodName store
GROUP BY Product.name
34
Product Purchase
name category prodName store
name C
Gizmo 1
Camera 2
OneClick 0
Solution
35
Outer joins
return all matching rows, plus nonmatching rows
from one or both tables
can be performed on only two tables at a time.
select *
from one left join two
on one.x = two.x;
X A X B
1 a .
2 b 2 x
4 d .
s105d07
Right Join
39
Table Two Table One
X B X A
2 x 1 a
3 y 4 d
5 v 2 b
select *
from two right join one
on one.x = two.x;
X B X A
. 1 a
2 x 2 b
. 4 d
Full Join
40
select *
from one full join two
on one.x = two.x;
X A X B
1 a .
2 b 2 x
. 3 y
4 d .
. 5 v
LEFT OUTER JOIN
41
Product Purchase
name category prodName store
name store
OID Name
SELECT Orders.OrderID, Employees.Name Ali
FROM Orders RIGHT JOIN Employees
10308 Nancy
ON Orders.EmployeeID = Employees.EmployeeID
ORDER BY Orders.OrderID; 10698 Nancy
10857 Mark
Full Outer Join
43
List all the employees and any orders they might have placed
Employee Name ID Salary Orders OID CID EID Odate
OID Name
SELECT Orders.OrderID, Employees.Name
FROM Orders Full Outer JOIN Employees 10308 Nancy
ON Orders.EmployeeID = Employees.EmployeeID 10857 Mark
ORDER BY Orders.OrderID;
10698
Ali
CROSS JOIN
44
Attributes n+m
Student Cardinality n*m SELECT * FROM
ID Name Student CROSS JOIN
123 John Enrolment
124 Mary
125 Mark ID Name ID Code
126 Jane 123 John 123 DBS
124 Mary 123 DBS
Enrolment 125 Mark 123 DBS
ID Code 126 Jane 123 DBS
123 John 124 PRG
123 DBS 124 Mary 124 PRG
124 PRG 125 Mark 124 PRG
124 DBS 126 Jane 124 PRG
126 PRG 123 John 124 DBS
124 Mary 124 DBS
Solution
45