SQL Tips Tricks 1734323478
SQL Tips Tricks 1734323478
Tuples or rows
Tables Explained
Atomic types:
Characters: CHAR(20), VARCHAR(50)
Numbers: INT, BIGINT, SMALLINT, FLOAT
Others: MONEY, DATETIME, …
A tuple = a record
Restriction: all attributes are of atomic type
SELECT <attributes>
FROM <one or more relations>
WHERE <conditions>
Simple SQL Query
SELECT *
FROM Product
WHERE category=‘Gadgets’
Input Schema
Output Schema
Details
Case insensitive:
Same: SELECT Select select
Same: Product product
Different: ‘Seattle’ ‘seattle’
Constants:
‘abc’ - yes
“abc” - no
The LIKE operator
SELECT *
FROM Products
WHERE PName LIKE ‘%gizmo%’
Category
SELECT DISTINCT category Gadgets
FROM Product
Photography
Household
Compare to:
Category
Gadgets
SELECT category
Gadgets
FROM Product
Photography
Household
Ordering the Results
Ties are broken by the second attribute on the ORDER BY list, etc.
?
FROM Product
ORDER BY category
?
SELECT Category
FROM Product
ORDER BY PName
?
SELECT DISTINCT category
FROM Product
ORDER BY PName
Keys and Foreign Keys
Company
GizmoWorks 25 USA
Key
Canon 65 Japan
Hitachi 15 Japan
Product
Join
between Product
and Company
SELECT PName, Price
FROM Product, Company
WHERE Manufacturer=CName AND Country=‘Japan’
AND Price <= 200
Joins
Product Company
PName Price Category Manufacturer Cname StockPrice Country
Gizmo $19.99 Gadgets GizmoWorks
GizmoWorks 25 USA
Powergizmo $29.99 Gadgets GizmoWorks Canon 65 Japan
SingleTouch $149.99 Photography Canon Hitachi 15 Japan
MultiTouch $203.99 Household Hitachi
SELECT cname
FROM
WHERE
A Subtlety about Joins
SELECT Country
FROM Product, Company
WHERE Manufacturer=CName AND Category=‘Gadgets’
Unexpected duplicates
A Subtlety about Joins
Product Company
Name Price Category Manufacturer Cname StockPrice Country
Gizmo $19.99 Gadgets GizmoWorks
GizmoWorks 25 USA
Powergizmo $29.99 Gadgets GizmoWorks Canon 65 Japan
SingleTouch $149.99 Photography Canon Hitachi 15 Japan
MultiTouch $203.99 Household Hitachi
SELECT Country
FROM Product, Company
WHERE Manufacturer=CName AND Category=‘Gadgets’
Country
What is ??
the problem ? ??
What’s the
solution ?
Tuple Variables
Answer = {}
for x1 in R1 do
for x2 in R2 do
…..
for xn in Rn do
if Conditions
then Answer = Answer {(a1,…,ak)}
return Answer
An Unintuitive Query
SELECT Company.city
FROM Company
WHERE Company.name IN
(SELECT Product.maker
FROM Purchase , Product
WHERE Product.pname=Purchase.product
AND Purchase .buyer = ‘Joe Blow‘);
Subqueries Returning
Relations
Is it equivalent to this ?
SELECT Company.city
FROM Company, Product, Purchase
WHERE Company.name= Product.maker
AND Product.pname = Purchase.product
AND Purchase.buyer = ‘Joe Blow’
Beware of duplicates !
Removing Duplicates
SELECT DISTINCT Company.city
FROM Company
WHERE Company.name IN
(SELECT Product.maker
FROM Purchase , Product
WHERE Product.pname=Purchase.product
AND Purchase .buyer = ‘Joe Blow‘);
SELECT name
FROM Product
WHERE price > ALL (SELECT price
FROM Purchase
WHERE maker=‘Gizmo-Works’)
Question for Database Fans
and their Friends
Can we express this query as a single SELECT-FROM-WHERE query,
without subqueries ?
Question for Database Fans
and their Friends
correlation
Note (1) scope of variables (2) this can still be expressed as single SFW
Complex Correlated Query
We probably want:
What do
they mean ?
What does
it mean ?
SELECT product,
sum(price * quantity) AS SumSales
max(quantity) AS MaxQuantity
FROM Purchase
GROUP BY product
HAVING Clause
Evaluation steps:
1. Evaluate FROM-WHERE, apply condition C1
2. Group by the attributes a1,…,ak
3. Apply condition C2 to each group (may have aggregates)
4. Compute aggregates in S and return the result
Advanced SQLizing
2. Quantifiers
Find all companies that make some products with price < 100
Existential: easy ! ☺
2. Quantifiers
Find all companies that make only products with price < 100
same as:
Find all companies s.t. all of their products have price < 100
Universal: hard !
2. Quantifiers
2. Find all companies s.t. all their products have price < 100
Author(login,name)
Wrote(login,url)
Mentions(url,word)
Find authors with vocabulary 10000 words:
SELECT Author.name
FROM Author, Wrote, Mentions
WHERE Author.login=Wrote.login AND Wrote.url=Mentions.url
GROUP BY Author.name
HAVING count(distinct Mentions.word) > 10000
Two Examples
Store(sid, sname)
Product(pid, pname, price, sid)
Find all stores that sell only products with price > 100
same as:
Find all stores s.t. all their products have price > 100)
SELECT Store.name
FROM Store, Product
WHERE Store.sid = Product.sid
Why both ?
GROUP BY Store.sid, Store.name
HAVING 100 < min(Product.price)
SELECT Store.name
FROM Store
WHERE
Almost equivalent… 100 < ALL (SELECT Product.price
FROM product
WHERE Store.sid = Product.sid)
SELECT Store.name
FROM Store
WHERE Store.sid NOT IN
(SELECT Product.sid
FROM Product
WHERE Product.price <= 100)
Two Examples
Store(sid, sname)
Product(pid, pname, price, sid)
Better:
SELECT Store.sname, x.pname
FROM Store, Product x
WHERE Store.sid = x.sid and
But may x.price >=
return ALL (SELECT y.price
multiple FROM Product y
product names WHERE Store.sid = y.sid)
per store
Two Examples
Finally, choose some pid arbitrarily, if there are many
with highest price:
SELECT * E.g.
FROM Person age=20
WHERE (age < 25) AND heigth=NULL
weight=200
(height > 6 OR weight > 190)
Rule in SQL: include only tuples that yield TRUE
Null Values
Unexpected behavior:
SELECT *
FROM Person
WHERE
Some Persons areage < 25! OR age >= 25
not included
Null Values
SELECT *
FROM Person
WHERE age < 25 OR age >= 25 OR age IS NULL
Name Store
Gizmo Wiz
Camera Ritz
Camera Wiz
OneClick NULL
Application
What’s wrong ?
Application
General form:
Purchase
Product
prodName buyerName price
name listPrice category
camera John 200
camera - -
Insertion: an Example
camera 200 -
UPDATE PRODUCT
SET price = price/2
WHERE Product.name IN
(SELECT product
FROM Purchase
WHERE Date =‘Oct, 25, 1999’);