Professional Documents
Culture Documents
A and B
A B
A B
A\ B
2
Av F
T y,z H
T x,y H W 2x+z = 0 I
Taxtable
Name=Lincoln Town
)) (
X,Y Broadcast
3.2 SQL SELECT a1, ..., an FROM R1, R2, , Rm WHERE Con1, ,Conk This means:
a1, ..., an( Con1(
Conk
( R1 R2 Rm))))
Example:
AS keyword used to rename relations Two SQL expressions can be combined by:
INTERSECT UNION MINUS set difference
Example: Find the names of the streets that intersect. SELECT S.NAME, T.NAME FROM Streets AS S, Streets AS T WHERE S.X = T.X and S.Y = T.Y
Example: Assume we have the relations: Broadcast ( Radio, X , Y ) Town ( Name, X, Y ) Find the parts of Lincoln, NE that can be reached by at least one Radio station. (SELECT X, Y FROM Town WHERE Name = Lincoln) INTERSECT (SELECT X, Y FROM Broadcast)
10
Another way of connecting SQL expressions is using the IN keyword. SELECT .. FROM .. WHERE a IN ( SELECT b FROM .. WHERE .. )
11
SQL with aggregation SELECT aggregate_function FROM . WHERE aggregate_function Max (c1a1 + ..+ cnan) Min (c1a1 + ..+ cnan) Sum(a) Avg(a) Count(a) where ai are attributes and ci are constants
12
Example: Package(Serial_No, From, Destination, Weight) Postage (Weight , Fee) Find the total postage of all packages sent from Omaha. SELECT Sum(Fee) FROM Package, Postage WHERE Package.Weight = Postage.Weight AND Package.From = Omaha
13
GROUP BY SELECT a1, , an, aggregate_function FROM .. WHERE GROUP BY a1, ..., ak Evaluates basic SQL query Groups the tuples according to different values of a1,..,ak Applies the aggregate function to each group separately {a1, , ak} {a1, , an}
14
Example: Find the total postage sent out from each city. SELECT Package.From, Sum(Postage.Fee) FROM Package, Postage WHERE Package.Weight = Postage.Weight GROUP BY Package.From
15