Basic Query

Syntax

  1. SELECT clause
    • specify the columns that you want to retrieve from a database table
  • DISTINCT keyword (optional)
    • return only unique values from a specific column (i.e., it would eliminate the outputs)
  1. FROM clause
    • specify the table or tables from which you want to retrieve data
  1. WHEREclause (optional)
    • filter data based on a specific condition
  • 1
    2
    3
    SELECT DISTINCT a1, a2, ..., an    -- target list
    FROM R1, R2, ..., Rm -- relation list
    WHERE P -- qualification
  • Examples:

    1. Find the names of sailors who have reserved boat number 103

      • 1
        2
        3
        SELECT S.sname
        FROM Sailors S, Reserves R
        WHERE R.sid = S.sid AND R.bid = 103
    2. Given following schema:

      1. Find the sids of sailors who have reserved a red boat

        1
        2
        3
        SELECT R.sid
        FROM Boats B, Reserves R
        WHERE B.bid = R.bid AND B.color = 'red'
      2. Find the names of sailors who have reserved a red boat

        1
        2
        3
        SELECT S.sname
        FROM Sailors S, Boats B, Reserves R
        WHERE S.sid = R.sid AND B.bid = R.bid AND B.color = 'red'
      3. Find the colors of boats reserved by Lubber

        1
        2
        3
        SELECT B.color
        FROM Sailors S, Boats B, Reserves R
        WHERE S.sname = 'Lubber' AND S.sid = R.sid AND B.bid = R.bid

Expressions & Strings

  1. AS and = (name field)
    • rename columns or tables in the query result
      1
      2
      SELECT S.age, age1 = S.age + 5, S.age * 2 AS age2
      FROM Sailors S
  1. LIKE
    • is used for string matching
    • _ means any one character (must have one character)
    • % means 0 or more arbitrary characters
      1
      WHERE S.sname LIKE 'B_%B'   -- BAB, BBBBBBB are accepted, BB is not accepted

Set Manipulation

Union

  • combine the results of two SELECT statements into a single result set (eliminates duplicates)

    • E.g., Find the sids of sailors who have reserved a red boat or a green boat

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      -- SELECT R.sid
      -- FROM Boats B, Reserves R
      -- WHERE B.bid = R.bid AND (B.color = 'red' OR B.color = 'green')

      SELECT R.sid
      FROM Boats B, Reserves R
      WHERE B.bid = R.bid AND B.color = 'red'

      UNION

      SELECT R.sid
      FROM Boats B, Reserves R
      WHERE B.bid = R.bid AND B.color = 'green'

Intersection

  • retrieve only the common records returned by two SELECT statements
    • E.g., Find the sids of sailors who have reserved a red boat and a green boat
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      -- SELECT R.sid
      -- FROM Boats B, Reserves R
      -- WHERE B.bid = R.bid AND (B.color = 'red' AND B.color = 'green')

      SELECT R.sid
      FROM Boats B, Reserves R
      WHERE B.bid = R.bid AND B.color = 'red'

      INTERSECT

      SELECT R.sid
      FROM Boats B, Reserves R
      WHERE B.bid = R.bid AND B.color = 'green'

Except

  • retrieve all records from one SELECT statement that are not returned by a second SELECT statement
    • E.g., Find the sids of sailors who have reserved a red boat but not a green boat
      1
      2
      3
      4
      5
      6
      7
      8
      9
      SELECT R.sid
      FROM Boats B, Reserves R
      WHERE B.bid = R.bid AND B.color = 'red'

      EXCEPT

      SELECT R.sid
      FROM Boats B, Reserves R
      WHERE B.bid = R.bid AND B.color = 'green'

Nested Structure

Nested Queries

  • a query that has another query embedded within it, the embedded query is called as a subquery
  • the queries can have a deeply nested structures
    • E.g., Find names of sailors who have reserved boat with bid 103
      1
      2
      3
      4
      5
      SELECT S.sname
      FROM Sailors S
      WHERE S.sid IN (SELECT R.sid
      FROM Reserves R
      WHERE R.bid = 103)
      • The inner subquery is completely independent of the outer query

Correlated Nested Queries

  • a type of nested query that uses data from the outer query in the subquery
    • E.g., Find names of sailors who have reserved boat with bid 103
      1
      2
      3
      4
      5
      SELECT S.sname
      FROM Sailors S
      WHERE EXISTS (SELECT *
      FROM Reserves R
      WHERE R.bid = 103 AND S.sid = R.sid)
      • The inner subquery depends on the row that being examined in the outer query

Comparsion Operators

Arithmetic Comparison Operators

  1. =: equality

  2. != or <>: not equal to

  3. <:less than

  4. >: greater than

  5. <=: less than or equal to

  6. >=: greater than or equal to

Set Comparsion Operators

  1. IN Operator
    • check whether a specified value matches any value in a list or a subquery
    • allows us to specify multiple values in a WHERE clause
    • negative operator: NOT IN
      • E.g., Find the names of boats which are red, blue or green
        1
        2
        3
        SELECT B.bname
        FROM Boats B
        WHERE B.color IN ('red', 'blue', 'green')
  1. EXISTS
    • check if a subquery returns any rows
    • negative operator: NOT EXISTS
      • E.g., Find names of sailors who have reserved boat with bid 103
        1
        2
        3
        4
        5
        SELECT S.sname
        FROM Sailors S
        WHERE EXISTS (SELECT *
        FROM Reserves R
        WHERE R.bid = 103 AND S.sid = R.sid)
  1. op ANY or op SOME
    • check if the value being compared matches any of the values returned by the subquery
      • E.g., Find sailors whose rating is greater than that of some sailor called Horatio:
        1
        2
        3
        4
        5
        SELECT *
        FROM Sailors S1
        WHERE S1.rating > ANY (SELECT S2.rating
        FROM Sailors S2
        WHERE S2.name = 'Horatio')
  1. op ALL
    • check if the value being compared matches all of the values returned by the subquery
      • E.g., Find sailors whose rating is better than every sailor called Horatio
        1
        2
        3
        4
        5
        SELECT *
        FROM Sailors S1
        WHERE S1.rating > ALL (SELECT S2.rating
        FROM Sailors S2
        WHERE S2.name = 'Horatio')
      • E.g., Find the sailors with the highest rating
        1
        2
        3
        4
        SELECT *
        FROM Sailors S1
        WHERE S1.rating >= ALL (SELECT S2.rating
        FROM Sailors S2)

Division

Aggregate Operations

Five Operations

Operations Syntax Descriptions
COUNT COUNT([DISTINCT]A) The number of [unique] value in the A column
SUM SUM([DISTINCT]A) The sum of all [unique] values in the A column
AVG AVG([DISTINCT]A) The average of all [unique] values in the A column
MAX MAX(A) The maximum values in the A column
MIN MIN(A) The minimum values in the A column
  • Syntax for COUNT, SUM and AVG:

    • E.g., Find the average age of all sailors with rating of 10
      1
      2
      3
      SELECT COUNT(S.age)
      FROM Sailors S
      WHERE S.rating = 10
  • Syntax for MAX and MIN:

    • E.g., Find the name and age of the oldest sailor

      1
      2
      3
      4
      SELECT S.name, S.age
      FROM Sailors S
      WHERE S.age = (SELECT MAX(S2.age)
      FROM Sailors S2)
    • Noted: the following syntax is invalid

      1
      2
      SELECT S.name, MAX(S.age)   -- invalid
      FROM Sailors S
  • Examples:

    1. Count the number of sailors

      1
      2
      SELECT COUNT(*)
      FROM Sailors
    2. Find the names of sailors who are older than the oldest sailor with a rating of 10

      1
      2
      3
      4
      5
      SELECT S.name
      FROM Sailors S
      WHERE S.age > (SELECT MAX(S2.age)
      FROM Sailors S2
      WHERE S2.rating = 10)

GROUP BY and HAVING

  • GROUP BY is used to group rows based on the values in one or more columns with aggregate functions
  • HAVING is used to filter the groups based on some criteria

    • Noted: only the columns that appear in GROUP BY clause can appear in the HAVING clause

    • E.g., Find the average age of sailors for each rating level that has at least two sailors

      1
      2
      3
      4
      SELECT S.sname, AVG(S.age) AS avgage
      FROM Sailors S
      GROUP BY S.rating
      HAVING COUNT(*) >= 2

    • E.g., Find the average age of sailors who are at least 18 years old for each rating level that has at least two sailors

      1
      2
      3
      4
      5
      6
      7
      SELECT S.sname, AVG(S.age) AS avgage
      FROM Sailors S
      WHERE S.age >= 18
      GROUP BY S.rating
      HAVING 2 <= (SELECT COUNT(*) -- !!!
      FROM Sailors S2
      WHERE S.rating = S2.rating)

    • E.g., Find the average age of sailors who are at least 18 years old for each rating level that has at least such two sailors

      1
      2
      3
      4
      5
      6
      7
      SELECT S.sname, AVG(S.age) AS avgage
      FROM Sailors S
      WHERE S.age >= 18 -- ?
      GROUP BY S.rating
      HAVING 2 <= (SELECT COUNT(*)
      FROM Sailors S2
      WHERE S.rating = S2.rating AND S.age >= 18) -- !!!

  • Noted: Aggregate operations cannot be nested!!!
    • E.g., Find those ratings for which the average age is the minimum over all ratings
    • The following SQL is invalid:
      1
      2
      3
      4
      5
      SELECT S.rating
      FROM Sailors S
      WHERE S.age = (SELECT MIN(AVG(S2.age))
      FROM Sailors S2
      GROUP BY S2.rating)
    • Solution:
      1
      2
      3
      4
      5
      6
      SELECT Temp.rating, Temp.avgage
      FROM (SELECT S.rating, AVG(S.age) AS avgage
      FROM Sailors S
      GROUP BY S.rating) AS Temp
      WHERE Temp.avgage = (SELECT MIN(Temp.avgage)
      FROM Temp)