SQL Finish Subqueries, Distinct, Grouping, and Aggregation




CS157a

Chris Pollett

Nov 26, 2018

Outline

Introduction

Subqueries in FROM Clauses

R(A,B), S(A,B) use notation R.A or S.A

$R \bowtie_{cond} S$

`sigma_{cond} (R \times S)`

Movies JOIN StarsIn ON title = movieTitle AND year = movieYear

Movies INNER JOIN StarsIn ON title = movieTitle AND year = movieYear

$\mbox{Movies} \bowtie_{\mbox{title = movieTitle AND year = movieYear}} \mbox{StarsIn}$

$(R \bowtie_{cond1} S) \bowtie_{cond2} T$

Joins in SQL

Natural Joins

Outer Joins

Quiz

Which of the following is true?

  1. To pattern match 0 or more characters in a SQL string one uses the symbol '*'.
  2. The only was to disambiguate attributes with the same name from two different tables in SQL is to use tuple variables.
  3. The following SQL query involves a correlated subquery:
    SELECT title
    FROM Movies Old
    WHERE year < ANY 
       (SELECT year 
        FROM Movies 
        WHERE title = Old.title );
    

Full-Relation Operations

Eliminating Duplicates

Aggregation in SQL

GROUP BY Clauses

Grouping, Aggregation, and Nulls

Having Clauses