Chapter 4- Lecture- SQL
Chapter 4- Lecture- SQL
o SQL schema
₋ Identified by a schema name
₋ Includes an authorization identifier and descriptors for each element
o Schema elements include
₋ Tables, constraints, views, domains, and other constructs
o Some statement in SQL ends with a semicolon
SQL Commands
o Specifies a new base relation by giving it a name, and specifying each of its
attributes and their data types (INTEGER, FLOAT, DECIMAL(i,j), CHAR(n),
VARCHAR(n))
SELECT attributes
FROM relations (possibly multiple)
WHERE conditions (selections)
10
Simple SQL Query
SELECT *
FROM Product
WHERE category=‘Gadgets’
PName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
“selection” Powergizmo $29.99 Gadgets GizmoWorks
11
Simple SQL Query
PName Price Category Manufacturer
Product
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
Input Schema
Output Schema 13
Details
o Case insensitive:
₋ Same: SELECT Select select
₋ Same: Product product
₋ Different: ‘Seattle’ ‘seattle’
o Constants:
₋ ‘abc’ - yes
₋ “abc” - no
Selections
15
The LIKE operator
16
Eliminating Duplicates
Category
SELECT DISTINCT category Gadgets
FROM Product Photography
Household
Compare to:
Category
Gadgets
SELECT category Gadgets
FROM Product Photography
Household
17
Ordering the Results
• Ties are broken by the second attribute on the ORDER BY list, etc.
18
Ordering the Results
SELECT category
FROM Product
ORDER BY pname
?
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
19
Ordering the Results
Category
SELECT DISTINCT category
Gadgets
FROM Product
ORDER BY category Household
Photography
Compare to:
SELECT category
FROM Product
ORDER BY pname
?
20
Keys and Foreign Keys
Company
CName StockPrice Country
GizmoWorks 25 USA
Key
Canon 65 Japan
Hitachi 15 Japan
Product
PName Price Category Manufacturer
Foreign
Gizmo $19.99 Gadgets GizmoWorks key
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
Joins in SQL
PName Price
SingleTouch $149.99
24
Joins
SELECT country
FROM Product, Company
WHERE manufacturer=cname AND category=‘Gadgets’
25
Joins in SQL
Product
Company
SELECT country
FROM Product, Company
WHERE manufacturer=cname AND category=‘Gadgets’
Country
What is ??
the problem ? ??
What’s the
solution ?
26
Joins
Answer = {}
for x1 in R1 do
for x2 in R2 do
…..
for xn in Rn do
if Conditions
then Answer = Answer {(a1,…,ak)}
return Answer
An Unintuitive Query
31
Aggregation
SELECT Count(category)
FROM Product same as Count(*)
WHERE year > 1995
We probably want:
Bagel 10/21 1 20
Banana 10/10 1 10
3. SELECT
SELECT S
FROM R1,…,Rn
WHERE C1
GROUP BY a1,…,ak Why ?
HAVING C2
S = may contain attributes a1,…,ak and/or any aggregates but NO OTHER ATTRIBUTES
C1 = is any condition on the attributes in R 1,…,Rn
C2 = is any condition on aggregate expressions
General form of Grouping and Aggregation
Evaluation steps:
1. Evaluate FROM-WHERE, apply condition C1
2. Group by the attributes a1,…,ak
3. Apply condition C2 to each group (may have aggregates)
4. Compute aggregates in S and return the result
SELECT S
FROM R1,…,Rn
WHERE C1
GROUP BY a1,…,ak
HAVING C2
NULLS in SQL
Same as:
SELECT Product.name, Purchase.store
FROM Product, Purchase
WHERE Product.name = Purchase.prodName
But Products that never sold will be lost !
Outerjoins
Name Store
Gizmo Wiz
Camera Ritz
Camera Wiz
OneClick NULL
Modifying the Database
General form:
camera - -
Deletions
Example:
UPDATE PRODUCT
SET price = price/2
WHERE Product.name IN
(SELECT product
FROM Purchase
WHERE Date =‘Oct, 25, 1999’);