EE477 Lecture 3 - SQL Basics
EE477 Lecture 3 - SQL Basics
SQL Basics
2
GCP setup (see HW0 for details)
● We will share one billing account for all students EE477 billing account
a. Tell us your Gmail address through this Google Survey Link
■ Please create a Gmail account if you don’t have one
b.
c.
Our TAs will add you as billing account users
Create a Google Cloud SQL project ...
■ Connect to EE477 billing account
d. We have enough credits for doing all homeworks
● Please do not use resources unnecessarily
a. Make sure to stop instances you are not using
b. More details in HW0 (to be posted tomorrow)
3
Recap
title year length genre
● Data model Oldboy 2003 120 mystery
○ Structure, operations, constraints
○ Relational, semi-structured, key-value Ponyo 2008 103 anime
4
SQL
● Structured Query Language (pronounced “S.Q.L.” or “sequel”)
● Most common language for querying and modifying a database
● Serves as both a
○ data-manipulation language and
○ data-definition language
● Declarative language
5
SQL history and motivation
● Initially developed in the early 1970’s
● By 1986, ANSI and ISO standard groups standardize SQL
○ New versions of standard published in 1989, 1992, and more up to 2016
● Dark times in 2000s
○ NoSQL for Web 2.0 (think social networks like Facebook)
○ Are relational databases dead?
● Now, as before, everyone sells SQL
○ Pig, Hive, Impala, SparkSQL
● SQL → No SQL → Not only SQL → NewSQL
○ Full cycle :-)
○ SQL withstands the test of time and continues to evolve
○ Currently one of the most popular programming languages
6
Simple queries in SQL
● Simplest form: ask for tuples in a relation that satisfy a condition
● Use three keywords: SELECT, FROM, and WHERE
SELECT *
FROM Movies
WHERE studioName = ‘Ghibli’
AND year = 2008;
7
Simple queries in SQL
● Simplest form: ask for tuples in a relation that satisfy a condition
● Use three keywords: SELECT, FROM, and WHERE
SELECT *
FROM Movies Relation(s) which query refers to
WHERE studioName = ‘Ghibli’
AND year = 2008;
8
Simple queries in SQL
● Simplest form: ask for tuples in a relation that satisfy a condition
● Use three keywords: SELECT, FROM, and WHERE
SELECT *
FROM Movies
WHERE studioName = ‘Ghibli’ Condition(s) that tuples must
AND year = 2008; satisfy to match the query
9
Simple queries in SQL
● Simplest form: ask for tuples in a relation that satisfy a condition
● Use three keywords: SELECT, FROM, and WHERE
10
Simple queries in SQL
● Simplest form: ask for tuples in a relation that satisfy a condition
● Use three keywords: SELECT, FROM, and WHERE
11
Projection in SQL
● We can replace the * of the SELECT clause with attributes of the relation
title length
Ponyo 103
12
Projection in SQL
● Use the keyword AS and alias to change an attribute’s name
name length
Ponyo 103
13
Projection in SQL
● Use an expression in place of an attribute
title lengthHrs
Ponyo 1.716
14
Projection in SQL
● Can even allow a constant as an expression
title isMovie
Ponyo yes
15
Selection in SQL
● In the WHERE clause, we may build expressions using:
○ Comparison: =, <>, < , >, <=, and >=
○ Arithmetic: +, -, *, /, %
○ Strings: surrounded by single quotes
○ String concatenation: ||
○ Boolean operators: AND, OR, NOT
SELECT title
FROM Movies
WHERE studioName = ‘Ghi’ || ‘bli’
AND (year > 2000 OR length <= 100);
16
Comparison of strings
● Two strings are equal if they have the same sequence of characters
○ Ignore pad characters in fixed-length CHAR(n) strings
● <, >, <=, >= comparisons are based on lexicographic order
○ ‘fodder’ < ‘foo’
○ ‘bar’ < ‘bargain’
17
Pattern matching in SQL
● Strings can also be compared based on pattern matching
○ s LIKE p, s NOT LIKE p
○ % (matches any sequence of 0 or more characters), _ (matches one character)
18
Pattern matching in SQL
● It is also possible to add apostrophes and % or _ characters in strings
19
Dates and times
● Special data types in SQL
DATE ‘1948-05-14’
TIME ‘15:00:02.5’
TIME ‘12:00:00-8:00’
20
Null values and comparison involving NULL
● Interpretations for NULL value
○ Value unknown
○ Value inapplicable
○ Value withheld
● Asking if x is NULL / x is not NULL: x IS NULL / x IS NOT NULL
● Operations involving NULL result in NULL
○ If x IS NULL, x + 3 is NULL
○ Similarly, x * 0 and x - x are all NULL
○ NULL + 3 is not legal
● Comparisons with NULL result in UNKNOWN
○ If x IS NULL, x = 3 is UNKNOWN
○ x = x is also UNKNOWN
○ NULL = 3 is not legal
21
Truth table for three-value logic
● Rule of thumb: think TRUE = 1, FALSE = 0, and UNKNOWN = 1/2
● AND takes the minimum of values, OR the maximum, and NOT is 1 - value
x y x AND y x OR y NOT x
TRUE TRUE TRUE TRUE FALSE
TRUE UNKNOWN UNKNOWN TRUE FALSE
TRUE FALSE FALSE TRUE FALSE
UNKNOWN TRUE UNKNOWN TRUE UNKNOWN
UNKNOWN UNKNOWN UNKNOWN UNKNOWN UNKNOWN
UNKNOWN FALSE FALSE UNKNOWN UNKNOWN
FALSE TRUE FALSE TRUE TRUE
FALSE UNKNOWN FALSE UNKNOWN TRUE
FALSE FALSE FALSE FALSE TRUE
22
Ordering the output
● Sort the tuples by a list of attributes
○ ASC: ascending order (default)
○ DESC: descending order
SELECT title
FROM Movies
WHERE studioName = ‘Ghibli’
ORDER BY length, title DESC;
23
Pop quiz 1
● Find the movies whose names consist of three or more words
24
Pop quiz 2
● What is the result of this query if length is NULL?
● What is an equivalent query with a single condition in the WHERE clause?
SELECT *
FROM Movies
WHERE length <= 120 OR length > 120;
25
Queries involving multiple relations
● Until now, we studied queries for a single relation
● We can also combine multiple relations
○ joins, products, unions, intersections, and differences
● Why store data in multiple relations?
○ Single table
■ Data exchange is easier
■ Avoids cost of joining
○ Multiple tables
■ Data updates are easier
■ Querying a table is faster
26
Products and joins in SQL
● List multiple relations in FROM clause
SELECT name
FROM Movies, MovieExec
WHERE title = ‘Ponyo’ AND producerCertNum = certNum;
27
Disambiguating attributes
● If two or more attributes have the same name, use the R.A notation
28
Tuple variables
● To refer to two or more tuples from the same relation, use tuple variables
29
Pop quiz
● Find all movies that starred both ‘John’ and ‘Will’
30
Announcements
● Gradiance quizzes
○ Relational model: due 3/19 (only the last try counts)
○ 7 SQL Labs (not graded, only for your practice)
● Homeworks
○ HW0 (GCP setup + basic SQL): due 3/26
31
Recap
● SQL basics
○ SELECT, FROM, WHERE MovieStar(name, address, gender, birthdate)
○ Projection, expressions MovieExec(name, address, certNum, netWorth)
○ String comparison and pattern matching
○ Handling NULL values
○ Ordering SELECT MovieStar.name, MovieExec.name
○ Products and joins FROM MovieStar, MovieExec
WHERE MovieStar.address = MovieExec.address;
32
Interpreting multirelation queries
● Nested loops
SELECT x1.a1, x2.a2, … xm.am
FROM R1 as x1, R2 as x2, … Rm as xm
WHERE Cond
For x1 in R1:
For x2 in R2:
…
For xm in Rm:
If Cond(x1, x2, …):
output(x1.a1, x2.a2, … xm.am)
33
Interpreting multirelation queries
● Parallel assignment
○ Consider all possible assignments of tuples
○ Each assignment that satisfies Cond contributes a tuple to the answer
○ Equivalent definition as nested loops in terms of final answer
34
Unintuitive consequence
● Suppose we want to compute R ⋂ (S ∪ T) and use the query below
● Suppose R, S, and T only have one attribute A
● Suppose T is empty while R and S are not
● What is the result of this query?
SELECT R.A
FROM R, S, T
WHERE R.A = S.A OR R.A = T.A;
35
Union, Intersection, and Difference
● SQL provides the three set operations UNION, INTERSECT, and EXCEPT
○ Duplicates are eliminated by default
○ Use ALL keyword to retain duplicates
R A S A Result: A
(SELECT A FROM R)
UNION 1 1 1
(SELECT A FROM S); 2 3 2
36
Union, Intersection, and Difference
● SQL provides the three set operations UNION, INTERSECT, and EXCEPT
○ Duplicates are eliminated by default
○ Use ALL keyword to retain duplicates
R A S A Result: A
(SELECT A FROM R)
INTERSECT 1 1 1
(SELECT A FROM S); 2 3
37
Union, Intersection, and Difference
● SQL provides the three set operations UNION, INTERSECT, and EXCEPT
○ Duplicates are eliminated by default
○ Use ALL keyword to retain duplicates
R A S A Result: A
(SELECT A FROM R)
EXCEPT 1 1 2
(SELECT A FROM S); 2 3
38
Union, Intersection, and Difference
● SQL provides the three set operations UNION, INTERSECT, and EXCEPT
○ Duplicates are eliminated by default
○ Use ALL keyword to retain duplicates
R A S A Result: A
(SELECT A FROM R)
UNION ALL 1 1 1
(SELECT A FROM S); 2 3 1
39
Pop quiz
● Which movies are longer than Ponyo?
40