cdb03

Embed Size (px)

Citation preview

  • 8/4/2019 cdb03

    1/15

    11

    3. Relational Algebra and SQL3. Relational Algebra and SQLExample:

    Let the following relations describe point sets:

    A(x, y), B(x, y), J(x, y) 2D points in the plane

    H(x, y, z), I(x, y, z) 3D points in space

    F(z) line on the z-axis

    K(y,z) 2D points in (y, z)

    plane

  • 8/4/2019 cdb03

    2/15

    22

    3.1 Relation algebra operators3.1 Relation algebra operators

    A B

    A B A \ B

    A and B

  • 8/4/2019 cdb03

    3/15

    33

    A v F J K

  • 8/4/2019 cdb03

    4/15

    44

    T x,y H

    T y,z H

    W 2x+z = 0 I

  • 8/4/2019 cdb03

    5/15

    55

    Example:

    Find the SSN and tax for each person.

    SSN,Tax wages+interest+capital_gain = income Taxrecord Taxtable

    Example:

    Find the area of Lincoln reached by a

    radio station.( X,Y ( Name=Lincoln Town ) ) ( X,Y Broadcast )

  • 8/4/2019 cdb03

    6/15

    66

    3.2 SQL

    SELECT a1, ..., an

    FROM R1, R2, , Rm

    WHERE Con1, ,Conk

    This means:

    a1, ..., an( Con1( ( Conk ( R1

    R2

    Rm))))

  • 8/4/2019 cdb03

    7/15

    77

    Example:

    Find the SSN and tax for each person.

    SELECT SSN, Tax

    FROM Taxrecord, Taxtable

    WHERE wages + interest + capital_gain = income

  • 8/4/2019 cdb03

    8/15

    88

    AS keyword used to rename relations

    Two SQL expressions can be combined by:

    INTERSECTUNION

    MINUS set difference

  • 8/4/2019 cdb03

    9/15

    99

    Example:

    Find the names of the streets that intersect.

    SELECT S.NAME, T.NAME

    FROM Streets AS S, Streets AS TWHERE S.X = T.X and

    S.Y = T.Y

  • 8/4/2019 cdb03

    10/15

    1010

    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)

  • 8/4/2019 cdb03

    11/15

    1111

    Another way of connecting SQL expressions

    is using the IN keyword.

    SELECT ..

    FROM ..WHERE a IN ( SELECT b

    FROM ..

    WHERE .. )

  • 8/4/2019 cdb03

    12/15

    1212

    SQL with aggregation

    SELECT aggregate_function

    FROM .WHERE

    aggregate_function

    Max (c1a1 + ..+ cnan) where ai are attributes

    Min (c1a1 + ..+ cnan) and ci are constants

    Sum(a) where a is an attribute that is

    Avg(a) constant in each constraint tuple

    Count(a)

  • 8/4/2019 cdb03

    13/15

    1313

    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

  • 8/4/2019 cdb03

    14/15

    1414

    GROUP BY

    SELECT a1, , an, aggregate_functionFROM ..

    WHERE

    GROUP BY a1, ..., ak

    Evaluates basic SQL query

    Groups the tuples according to different values ofa1,..,ak

    Applies the aggregate function to each groupseparately

    {a1, , ak} {a1, , an}

  • 8/4/2019 cdb03

    15/15

    1515

    Example:

    Find the total postage sent out from each city.

    SELECT Package.From, Sum(Postage.Fee)

    FROM Package, PostageWHERE Package.Weight = Postage.Weight

    GROUP BY Package.From