Lecture 2 SQL
Lecture 2 SQL
Lecture 2 SQL
2:
Introduction to SQL
Lecture 2
Announcements!
2. Enroll to Piazza!!!
Lecture 2:
Introduction to SQL
Lecture 2
Today’s Lecture
3. Multi-table queries
• ACTIVITY: Multi-table queries!
4
Lecture 2 > Section 1
5
Lecture 2 > Section 1
6
Lecture 2 > Section 1 > SQL
SQL Motivation
• But why use SQL?
• The relational model of data is the most widely used model today
• Main Concept: the relation- essentially, a table
SQL Motivation
• “Not-Yet-SQL?”
Lecture 2 > Section 1 > SQL
Basic SQL
9
Lecture 2 > Section 1 > SQL
SQL Introduction
• SQL is a standard language for querying and manipulating data
SQL stands for
• SQL is a very high-level programming language Structured Query Language
• This works because it is optimized well!
SQL is a…
• Data Definition Language (DDL)
• Define relational schemata
• Create/alter/delete tables and their attributes
11
Lecture 2 > Section 1 > Definitions
Tables in SQL
A relation or table is a
Product multiset of tuples
PName Price Manufacturer
having the attributes
specified by the schema
Gizmo $19.99 GizmoWorks
12
Lecture 2 > Section 1 > Definitions
Tables in SQL
A multiset is an
unordered list (or: a set
Product
with multiple duplicate
PName Price Manufacturer instances allowed)
Gizmo $19.99 GizmoWorks
13
Lecture 2 > Section 1 > Definitions
Tables in SQL
14
Lecture 2 > Section 1 > Definitions
Tables in SQL
Product
PName Price Manufacturer
15
Lecture 2 > Section 1 > Definitions
Tables in SQL
Product
PName Price Manufacturer
The number of
attributes is the arity of
the relation
16
Lecture 2 > Section 1 > Definitions
17
Lecture 2 > Section 1 > Definitions
Table Schemas
• The schema of a table is the table name, its attributes, and their
types:
18
Lecture 2 > Section 1 > Keys & constraints
Key constraints
A key is a minimal subset of attributes that acts as a
unique identifier for tuples in a relation
• i.e. if two tuples agree on the values of the key, then they must be
the same tuple!
Students(sid:string, name:string, gpa: float)
In SQL, we may constrain a column to be NOT NULL, e.g., “name” in this table
Lecture 2 > Section 1 > Keys & constraints
General Constraints
• We can actually specify arbitrary assertions
• E.g. “There cannot be 25 people in the DB class”
ACTIVITY: Activity-2-1.ipynb
23
Lecture 2 > Section 2
2. Single-table queries
24
Lecture 2 > Section 2
25
Lecture 2 > Section 2 > SFW
SQL Query
• Basic form (there are many many more bells and whistles)
SELECT <attributes>
FROM <one or more relations>
WHERE <conditions>
26
Lecture 2 > Section 2 > SFW
SELECT *
FROM Product
WHERE Category = ‘Gadgets’
Notation
29
Lecture 2 > Section 2 > SFW
A Few Details
• SQL commands are case insensitive:
• Same: SELECT, Select, select
• Same: Product, product
30
Lecture 2 > Section 2 > Other operators
SELECT *
FROM Products
WHERE PName LIKE ‘%gizmo%’
31
Lecture 2 > Section 2 > Other operators
33
Lecture 2 > Section 2 > ACTIVITY
ACTIVITY: Activity-2-2.ipynb
34
Lecture 2 > Section 3
3. Multi-table queries
35
Lecture 2 > Section 3
2. Joins: basics
36
Lecture 2 > Section 3 > Foreign Keys
Product
PName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
40
Lecture 2 > Section 3 > Joins: Basics
Joins
Product(PName, Price, Category, Manufacturer)
Company(CName, StockPrice, Country) Note: we will often omit
attribute types in schema
Ex: Find all products under $200 manufactured in Japan; definitions for brevity, but
return their names and prices. assume attributes are
always atomic types
41
Lecture 2 > Section 3 > Joins: Basics
Joins
Product(PName, Price, Category, Manufacturer)
Company(CName, StockPrice, Country)
42
Lecture 2 > Section 3 > Joins: Basics
Joins
Product(PName, Price, Category, Manufacturer)
Company(CName, StockPrice, Country)
43
Lecture 2 > Section 3 > Joins: Basics
Joins
Product
Company
PName Price Category Manuf
Gizmo $19 Gadgets GWorks Cname Stock Country
Powergizmo $29 Gadgets GWorks
GWorks 25 USA
Canon 65 Japan
SingleTouch $149 Photography Canon
Hitachi 15 Japan
MultiTouch $203 Household Hitachi
45
Lecture 2 > Section 3 > Joins: Semantics
46
Lecture 2 > Section 3 > Joins: semantics
Answer = {}
for x1 in R1 do
for x2 in R2 do
…..
for xn in Rn do
if Conditions(x1,…, xn)
then Answer = Answer È {(x1.a1, x1.a2, …, xn.ak)}
return Answer