You are on page 1of 40

EE477

SQL Basics

Steven Euijong Whang 1


Announcements
● Gradiance quizzes (relational model): due 3/19 -- only the last try counts
● HW0 (GCP setup + basic SQL): posted tomorrow and due 3/26 -- please read
instructions carefully
● Haedong lounge machine accounts have been created
○ ID: <studentID>
○ Password: 4NSzdQS7cL<studentID>
○ Please change your password as soon as possible

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

● Relational model Frozen 2013 102 anime


○ Tables, schema, SQL (DDL)
● Tables discussion CREATE TABLE MovieStar (
○ Physical data independence name CHAR(30),
○ Row vs column major address VARCHAR(30),
gender CHAR(1),
birthdate DATE,
PRIMARY KEY (name, address)
);

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

Movies(title, year, length, genre, studioName)

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

Movies(title, year, length, genre, studioName)

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

Movies(title, year, length, genre, studioName)

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

Movies(title, year, length, genre, studioName)

Which attributes of the tuples


SELECT * matching the condition are produced
FROM Movies as part of the answer
WHERE studioName = ‘Ghibli’
AND year = 2008;

10
Simple queries in SQL
● Simplest form: ask for tuples in a relation that satisfy a condition
● Use three keywords: SELECT, FROM, and WHERE

Movies(title, year, length, genre, studioName)

SELECT * This tuple matches the query:


FROM Movies (Ponyo, 2008, 103, anime, Ghibli)
WHERE studioName = ‘Ghibli’
AND year = 2008;

11
Projection in SQL
● We can replace the * of the SELECT clause with attributes of the relation

title year length genre studioName


SELECT title, length
Ponyo 2008 103 anime Ghibli
FROM Movies
WHERE studioName = ‘Ghibli’
AND year = 2008;

title length
Ponyo 103

12
Projection in SQL
● Use the keyword AS and alias to change an attribute’s name

title year length genre studioName


SELECT title AS name, length
Ponyo 2008 103 anime Ghibli
FROM Movies
WHERE studioName = ‘Ghibli’
AND year = 2008;

name length
Ponyo 103

13
Projection in SQL
● Use an expression in place of an attribute

title year length genre studioName


SELECT title, length/60 AS lengthHrs
Ponyo 2008 103 anime Ghibli
FROM Movies
WHERE studioName = ‘Ghibli’
AND year = 2008;

title lengthHrs
Ponyo 1.716

14
Projection in SQL
● Can even allow a constant as an expression

title year length genre studioName


SELECT title, ‘yes’ AS isMovie
Ponyo 2008 103 anime Ghibli
FROM Movies
WHERE studioName = ‘Ghibli’
AND year = 2008;

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)

SELECT title SELECT title


FROM Movies FROM Movies
WHERE title LIKE ‘Star ____’; WHERE title LIKE ‘Star%’;

18
Pattern matching in SQL
● It is also possible to add apostrophes and % or _ characters in strings

SELECT title SELECT title


FROM Movies FROM Movies
WHERE title LIKE ‘%’’s%’; WHERE title LIKE ‘x%%x%’ ESCAPE ‘x’;

Ex) Alice’s Restaurant Ex) %Alice’s Restaurant%

19
Dates and times
● Special data types in SQL

DATE ‘1948-05-14’

TIME ‘15:00:02.5’

TIME ‘12:00:00-8:00’

TIMESTAMP ‘1948-05-14 12:00: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

title year length genre studioName


Ponyo 2008 103 anime Ghibli

My Neighbor Totoro 1988 87 anime Ghibli

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

Movies(title, year, length, genre, studioName, producerCertNum)


MovieExec(name, address, certNum, netWorth)

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

MovieStar(name, address, gender, birthdate)


MovieExec(name, address, certNum, netWorth)

SELECT MovieStar.name, MovieExec.name


FROM MovieStar, MovieExec
WHERE MovieStar.address = MovieExec.address;

28
Tuple variables
● To refer to two or more tuples from the same relation, use tuple variables

SELECT Star1.name, Star2.name


FROM MovieStar Star1, MovieStar Star2
WHERE Star1.address = Star2.address
AND Star1.name < Star2.name;

29
Pop quiz
● Find all movies that starred both ‘John’ and ‘Will’

Movies(title, year, length, genre, studioName)


StarsIn(movieTitle, movieYear, starName)

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?

Movies(title, year, length, genre, studioName)

40

You might also like