Lec 5
Lec 5
Lecture 5 SQL[DML]
SQL
2
Structured Query Language
Data Definition Language (DDL)
Definerelational schemata
Create/Alter/Drop tables and their attributes
• Basic form
SELECT <attributes>
FROM <one or more relations>
WHERE <conditions>
Call this a SFW query.
SELECT <Column list>
FROM <table names>
[WHERE <Condition>]
[GROUP BY <Column list>]
[HAVING <Condition>]
[ORDER BY <Column list>]
Retrieve Specific Columns and Rows
9
Min
Max
Count
Avg
Sum
Products PID Pname Price Qty SupplierI
D
11Slide 1 Apple 20 200 22
2 Banana 10 100 10
3 Orange 4 400 6
SELECT MIN(Price)
FROM Products; 4
Products PID Pname Price Qty SupplierI
D
1 Apple 20 200 22
12 2 Banana 10 100 10
3 Orange 4 400 6
SELECT COUNT(*)
FROM Customer
WHERE City = ‘Rome’
Categorizing Results
14
Purchase
Product Date Price Quantity Product TotalSales
Apple 10/21 1 20
Apple 10/25 1.50 20 Apple 50
Banana 10/3 0.5 10
Banana 10/10 1 10
Banana 15
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
Notes
23