advanced sql.pdf

download advanced sql.pdf

of 20

Transcript of advanced sql.pdf

  • 8/11/2019 advanced sql.pdf

    1/20

    SQL - 3

    Week 7 - 2

  • 8/11/2019 advanced sql.pdf

    2/20

    2

    Aggregate operators

    What is aggregation?

    Computing arithmetic expressions, such as

    Minimumor Maximum

    The aggregate operators supported by SQL are:

    COUNT, SUM, AVG, MIN, MAX

  • 8/11/2019 advanced sql.pdf

    3/20

    3

    Aggregate Operators

    COUNT(A): The number of values in the column A

    SUM(A): The sum of all values in column A AVG(A): The average of all values in column A

    MAX(A): The maximum value in column A

    MIN(A): The minimum value in column A

    (We can use DISTINCT with COUNT, SUM and AVG to compute

    only over non-duplicated columns)

  • 8/11/2019 advanced sql.pdf

    4/20

    4

    Using the COUNT operator

    Count the number of sailors

    SELECT COUNT (*)

    FROM Sailors S;

  • 8/11/2019 advanced sql.pdf

    5/20

    5

    Another Aggregate Query

    Count the number of different sailor names

    SELECT COUNT (DISTINCT S.sname)

    FROM Sailors S;

  • 8/11/2019 advanced sql.pdf

    6/20

    6

    Example of SUM operator

    Find the sum of ages of all sailors with a rating of 10

    SELECT SUM (S.age)

    FROM Sailors S

    WHERE S.rating=10;

  • 8/11/2019 advanced sql.pdf

    7/20

    7

    Example of AVG operator

    Find the average age of all sailors with rating 10

    SELECT AVG (S.age)

    FROM Sailors S

    WHERE S.rating=10;

  • 8/11/2019 advanced sql.pdf

    8/20

    8

    Example of MAX operator

    Find the name and age of the oldest sailor

    SELECT S.sname, MAX(S.age)

    FROM Sailors S;

    But this is illegal in SQL!!

  • 8/11/2019 advanced sql.pdf

    9/20

    9

    Correct SQL Query for MAX

    SELECT S.sname, S.ageFROM Sailors S

    WHERE S.age = ( SELECT MAX(S2.age)

    FROM Sailors S2 );

  • 8/11/2019 advanced sql.pdf

    10/20

    10

    Alternatively

    SELECT S.sname, S.ageFROM Sailors S

    WHERE ROWNUM

  • 8/11/2019 advanced sql.pdf

    11/20

    11

    Banking Examples

    branch (branch-id, branch-city, assets)

    customer (customer-id, customer-name, customer-city)

    account (account-number, branch-id, balance)

    loan (loan-number, branch-id, amount)

    depositor (customer-id, account-number)

    borrower (customer-id, loan-number)

  • 8/11/2019 advanced sql.pdf

    12/20

    12

    INExample 1

    Find the account numbers opened at branches of the bank in

    Fairfax

    SELECT A.account-number

    FROM account A

    WHERE A.branch-id IN(SELECT B.branch-id

    FROM branch B

    WHERE B.branch-city=

    Fairfax

    )

  • 8/11/2019 advanced sql.pdf

    13/20

    13

    INExample 2

    Find the account numbers opened at branches 101 and

    102 of the bank

    SELECT A.account-number

    FROM A.account

    WHERE A.branch-id IN(

    101

    ,

    102

    )

  • 8/11/2019 advanced sql.pdf

    14/20

    14

    EXISTS

    TheEXISTSpredicate is TRUE if and only if the Subquery

    returns a non-empty set.

    TheNOT EXISTSpredicate is TRUE if and only if the

    Subquery returns an empty set.

    TheNOT EXISTScan be used to implement the SETDIFFERENCE operator from relational algebra.

  • 8/11/2019 advanced sql.pdf

    15/20

    15

    EXISTSExample 1Select all the account balances where the account has been

    opened in a branch in Fairfax

    SELECT A.account-balance

    FROM account A

    WHERE EXISTS(SELECT *

    FROM branch B

    WHERE B.branch-city=

    Fairfax

    AND B.branch-id=A.branch-id)

  • 8/11/2019 advanced sql.pdf

    16/20

    16

    EXISTSExample 2

    Select all the account balances where the account has not been

    opened in a Fairfax branch

    SELECT A.account-balance

    FROM account A

    WHERE NOT EXISTS(SELECT *

    FROM branch B

    WHERE B.branch-city=

    Fairfax

    AND B.branch-id=A.branch-id)

  • 8/11/2019 advanced sql.pdf

    17/20

    17

    Quantified Comparison PredicateExample 1

    Select account numbers of the accounts with the minimum

    balance

    SELECT A.account-number

    FROM account A

    WHERE A.balance

  • 8/11/2019 advanced sql.pdf

    18/20

    18

    Aggregate Functions in SQL

    revisitedSQL provides five built-in aggregate functions that operate

    on sets of column values in tables:

    COUNT( ), MAX( ), MIN( ), SUM( ), AVG( ).

    With the exception of COUNT( ), these set functions must

    operate on sets that consist of simple values-that is, sets of

    numbers or sets of character strings, rather than sets of rows

    with multiple values.

  • 8/11/2019 advanced sql.pdf

    19/20

    19

    Aggregate Functions in SQL

    Example 1

    Select the total amount of balance of the account in

    branches located in Fairfax

    SELECT SUM(A.balance) AS total_amount

    FROM account A, branch B

    WHERE B.branch-city=

    Fairfax

    AND

    B.branch-id= A.branch-id

  • 8/11/2019 advanced sql.pdf

    20/20

    20

    Aggregate Functions in SQL

    Example 2Select the total number of opened accounts

    SELECT COUNT(A.account-number)

    FROM account AOR

    SELECT COUNT(*)

    FROM account