1
1
Basic SQL
Lecture 2
2
Outline
- Data in SQL
- Simple Queries in SQL
- Queries with more than one relation
Reading:
- Chapter 3, “Simple Queries” from SQL for Web
Nerds, by Philip Greenspun http://philip.greenspun.com/sql/
Basic SQL Lecture 2 1 Outline Data in SQL Simple Queries in SQL - - PDF document
Basic SQL Lecture 2 1 Outline Data in SQL Simple Queries in SQL Queries with more than one relation Reading: Chapter 3, Simple Queries from SQL for Web Nerds, by Philip Greenspun http://philip.greenspun.com/sql/ 2 1
1
1
2
Nerds, by Philip Greenspun http://philip.greenspun.com/sql/
2
3
Standard language for querying and manipulating data Structured Query Language Many standards out there:
4
– Create/alter/delete tables and their attributes – Following lectures...
– Query one or more tables – discussed next ! – Insert/delete/modify tuples in tables
– Idea: package a sequence of SQL statements server – Won’t discuss in class
3
5
6
– CHAR(20)
– VARCHAR(40)
– BIGINT, INT, SMALLINT, TINYINT – REAL, FLOAT
– MONEY
– DATE – DATETIME
4
7
Hitachi Household $203.99 MultiTouch Canon Photography $149.99 SingleTouch GizmoWorks Gadgets $29.99 Powergizmo GizmoWorks Gadgets $19.99 Gizmo Manufacturer Category Price PName Product Attribute names Table name Tuples or rows
8
– Restriction: all attributes are of atomic type
– Like a list… – …but it is unordered: no first(), no next(), no last().
– We will see later how to decompose complex structures into multiple flat tables
5
9
its attributes: Product(PName, Price, Category, Manfacturer)
we underline a key Product(PName, Price, Category, Manfacturer)
10
Basic form: SELECT attributes FROM relations (possibly multiple, joined) WHERE conditions (selections)
6
11
Hitachi Household $203.99 MultiTouch Canon Photography $149.99 SingleTouch GizmoWorks Gadgets $29.99 Powergizmo GizmoWorks Gadgets $19.99 Gizmo Manufacturer Category Price PName
SELECT * FROM Product WHERE category=‘Gadgets’
Product GizmoWorks Gadgets $29.99 Powergizmo GizmoWorks Gadgets $19.99 Gizmo Manufacturer Category Price PName
“selection”
12
Hitachi Household $203.99 MultiTouch Canon Photography $149.99 SingleTouch GizmoWorks Gadgets $29.99 Powergizmo GizmoWorks Gadgets $19.99 Gizmo Manufacturer Category Price PName
SELECT PName, Price, Manufacturer FROM Product WHERE Price > 100
Product Hitachi $203.99 MultiTouch Canon $149.99 SingleTouch Manufacturer Price PName
“selection” and “projection”
7
13
SELECT PName, Price, Manufacturer FROM Product WHERE Price > 100
Product(PName, Price, Category, Manfacturer) Answer(PName, Price, Manfacturer)
Input Schema Output Schema
14
What goes in the WHERE clause:
– For number, they have the usual meanings – For CHAR and VARCHAR: lexicographic ordering
– For dates and times, what you expect...
8
15
– % = any sequence of characters – _ = any single character
Product(Name, Price, Category, Manufacturer) Find all products whose name mentions ‘gizmo’: SELECT * FROM Products WHERE PName LIKE ‘%gizmo%’
16
Compare to: SELECT DISTINCT category FROM Product
Household Photography Gadgets Category
SELECT category FROM Product
Household Photography Gadgets Gadgets Category
9
17
SELECT pname, price, manufacturer FROM Product WHERE category=‘gizmo’ AND price > 50 ORDER BY price, pname Ordering is ascending, unless you specify the DESC keyword. Ties are broken by the second attribute on the ORDER BY list, etc.
18
SELECT Category FROM Product ORDER BY PName
Hitachi Household $203.99 MultiTouch Canon Photography $149.99 SingleTouch GizmoWorks Gadgets $29.99 Powergizmo GizmoWorks Gadgets $19.99 Gizmo Manufacturer Category Price PName
10
19
SELECT DISTINCT category FROM Product ORDER BY category Compare to:
Photography Household Gadgets Category
SELECT DISTINCT category FROM Product ORDER BY PName
20
Hitachi Household $203.99 MultiTouch Canon Photography $149.99 SingleTouch GizmoWorks Gadgets $29.99 Powergizmo GizmoWorks Gadgets $19.99 Gizmo Manufacturer Category Price PName Product Company Japan 15 Hitachi Japan 65 Canon USA 25 GizmoWorks Country StockPrice CName
What is the Connection between them ?
11
21
Product (pname, price, category, manufacturer) Company (cname, stockPrice, country) Find all products under $200 manufactured in Japan; return their names and prices. SELECT PName, Price FROM Product, Company WHERE Manufacturer=CName AND Country=‘Japan’ AND Price <= 200 Join between Product and Company
22
Hitachi Household $203.99 MultiTouch Canon Photography $149.99 SingleTouch GizmoWorks Gadgets $29.99 Powergizmo GizmoWorks Gadgets $19.99 Gizmo Manufacturer Category Price PName Product Company Japan 15 Hitachi Japan 65 Canon USA 25 GizmoWorks Country StockPrice Cname $149.99 SingleTouch Price PName
SELECT PName, Price FROM Product, Company WHERE Manufacturer=CName AND Country=‘Japan’ AND Price <= 200
12
23
Product (pname, price, category, manufacturer) Company (cname, stockPrice, country) Find all countries that manufacture some product in the ‘Gadgets’ category. SELECT Country FROM Product, Company WHERE Manufacturer=CName AND Category=‘Gadgets’
24
Hitachi Household $203.99 MultiTouch Canon Photography $149.99 SingleTouch GizmoWorks Gadgets $29.99 Powergizmo GizmoWorks Gadgets $19.99 Gizmo Manufacturer Category Price PName Product Company Japan 15 Hitachi Japan 65 Canon USA 25 GizmoWorks Country StockPrice Cname ?? ?? Country
What is the problem ? What’s the solution ?
SELECT Country FROM Product, Company WHERE Manufacturer=CName AND Category=‘Gadgets’
13
25
Product (pname, price, category, manufacturer) Purchase (buyer, seller, store, product) Person(persname, phoneNumber, city) Find names of people living in Seattle that bought some product in the ‘Gadgets’ category, and the names of the stores they bought such product from SELECT DISTINCT persname, store FROM Person, Purchase, Product WHERE persname=buyer AND product = pname AND city=‘Seattle’ AND category=‘Gadgets’
26
Person(pname, address, worksfor) Company(cname, address)
SELECT DISTINCT pname, address FROM Person, Company WHERE worksfor = cname SELECT DISTINCT Person.pname, Company.address FROM Person, Company WHERE Person.worksfor = Company.cname Which address ?
14
27
SELECT DISTINCT x.store FROM Purchase AS x, Purchase AS y WHERE x.product = y.product AND y.store = ‘BestBuy’ Find all stores that sold at least one product that the store ‘BestBuy’ also sold: Answer (store) Product (pname, price, category, manufacturer) Purchase (buyer, seller, store, product) Person(persname, phoneNumber, city)
28
General rule: tuple variables introduced automatically by the system: Product ( name, price, category, manufacturer) Becomes: Doesn’t work when Product occurs more than once: In that case the user needs to define variables explicitly. SELECT name FROM Product WHERE price > 100 SELECT Product.name FROM Product AS Product WHERE Product.price > 100
15
29
Hitachi Household $203.99 MultiTouch Canon Photography $149.99 SingleTouch GizmoWorks Gadgets $29.99 Powergizmo GizmoWorks Gadgets $19.99 Gizmo Manufacturer Category Price PName
SELECT Pname AS prodName, Price AS askPrice FROM Product WHERE Price > 100
Product $203.99 MultiTouch $149.99 SingleTouch askPrice prodName
Query with renaming
30
SELECT a1, a2, …, ak FROM R1 AS x1, R2 AS x2, …, Rn AS xn WHERE Conditions
Answer = {} for x1 in R1 do for x2 in R2 do ….. for xn in Rn do if Conditions then Answer = Answer ∪ {(x1,…,xk)} return Answer
16
31
SELECT a1, a2, …, ak FROM R1 AS x1, R2 AS x2, …, Rn AS xn WHERE Conditions
Doesn’t impose any order ! Answer = {} for all assignments x1 in R1, …, xn in Rn do if Conditions then Answer = Answer ∪ {(x1,…,xk)} return Answer
32
SELECT R.A FROM R, S, T WHERE R.A=S.A OR R.A=T.A Looking for R (S T) But what happens if T is empty?
17
33