PowerPoint Presentation
SQL
Tables in SQL
PName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
Product
Attribute names
Table name
Tuples or rows
2
Tables Explained
The schema of a table is the table name and its attributes:
Product(PName, Price, Category, Manufacturer)
A primary key is an attribute whose values are unique, and there is a cluster index on it
The primary key is underlined
Product(PName, Price, Category, Manufacturer)
Data types:
Characters: CHAR(20), VARCHAR(50)
Numbers: INT, BIGINT, SMALLINT, FLOAT
Others: MONEY, DATETIME, …
3
Azure SQL Database
Create a SQL Database in Azure
Use the sample database
Use Query Editor to issue SQL statements to the database
To list all tables in the DB:
SELECT * FROM INFORMATION_SCHEMA.TABLES
Note: Microsoft SQL server organizes tables in to “schemas”. Use the schema.tablename to refer to a particular table.
Check the schema of table ‘Customer’:
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = ‘Customer’
Check key constraints:
SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE table_name = ‘Customer’
Tables Explained
A tuple = a record = a row
A table = a set of tuples
Like a list…
…but it is unorderd:
no first(), no next(), no last().
6
SQL Query
Basic form: (plus many many more bells and whistles)
SELECT
FROM
WHERE
7
Simple SQL Query
PName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
SELECT *
FROM Product
WHERE category=‘Gadgets’
Product
PName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
“selection”
8
Simple SQL Query
PName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
SELECT PName, Price, Manufacturer
FROM Product
WHERE Price > 100
Product
PName Price Manufacturer
SingleTouch $149.99 Canon
MultiTouch $203.99 Hitachi
“selection” and
“projection”
9
Notation
Product(PName, Price, Category, Manfacturer)
Answer(PName, Price, Manfacturer)
Input Schema
Output Schema
SELECT PName, Price, Manufacturer
FROM Product
WHERE Price > 100
try a few queries in Azure
10
The LIKE operator
s LIKE p: pattern matching on strings
p may contain two special symbols:
% = any sequence of characters
_ = any single character
SELECT *
FROM Products
WHERE PName LIKE ‘%gizmo%’
11
Eliminating Duplicates
SELECT DISTINCT category
FROM Product
Compare to:
SELECT category
FROM Product
Category
Gadgets
Gadgets
Photography
Household
Category
Gadgets
Photography
Household
12
Ordering the Results
SELECT pname, price, manufacturer
FROM Product
WHERE category=‘gizmo’ AND price > 50
ORDER BY price, pname
Ties are broken by the second attribute on the ORDER BY list, etc.
Ordering is ascending, unless you specify the DESC keyword.
13
Joins
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
14
Joins
PName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
Product
Company
Cname StockPrice Country
GizmoWorks 25 USA
Canon 65 Japan
Hitachi 15 Japan
PName Price
SingleTouch $149.99
SELECT PName, Price
FROM Product, Company
WHERE Manufacturer=CName AND Country=‘Japan’
AND Price <= 200
Azure DB: Find all orders and details on black product, return the product SalesOrderID, SalesOrderDetailID, Name, UnitPrice, and OrderQty
15
A Subtlety about Joins
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’
Unexpected duplicates
16
A Subtlety about Joins
Name Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
Product
Company
Cname StockPrice Country
GizmoWorks 25 USA
Canon 65 Japan
Hitachi 15 Japan
Country
??
??
What is
the problem ?
What’s the
solution ?
SELECT Country
FROM Product, Company
WHERE Manufacturer=CName AND Category=‘Gadgets’
Azure DB: Find all orders that include at least one black product, return SalesOrderID
17
Semantics (Meaning) of Joins
SELECT a1, a2, …, ak
FROM R1, R2, …, Rn
WHERE Conditions
Answer = {}
for a1 in R1 do
for a2 in R2 do
…..
for an in Rn do
if Conditions
then Answer = Answer {(a1,…,ak)}
return Answer
18
Column Name Ambiguity
SELECT pname, address
FROM Person, Company
WHERE worksfor = cname
Which
address ?
Person(pname, address, worksfor)
Company(cname, address)
SELECT Person.pname, Company.address
FROM Person, Company
WHERE Person.worksfor = Company.cname
SELECT x.pname, y.address
FROM Person AS x, Company AS y
WHERE x.worksfor = y.cname
19
Aggregation
SELECT count(*)
FROM Product
WHERE year > 1995
Except count, all aggregations apply to a single attribute
SELECT avg(price)
FROM Product
WHERE maker=“Toyota”
SQL supports several aggregation operations:
sum, count, min, max, avg
20
COUNT applies to duplicates, unless otherwise stated:
SELECT Count(category)
FROM Product
WHERE year > 1995
same as Count(*)
We probably want:
SELECT Count(DISTINCT category)
FROM Product
WHERE year > 1995
Aggregation: Count
Azure DB: How many colors in the products?
21
Purchase(product, date, price, quantity)
More Examples
SELECT Sum(price * quantity)
FROM Purchase
SELECT Sum(price * quantity)
FROM Purchase
WHERE product = ‘bagel’
What do
they mean ?
22
Simple Aggregations
Purchase
Product Date Price Quantity
Bagel 10/21 1 20
Banana 10/3 0.5 10
Banana 10/10 1 10
Bagel 10/25 1.50 20
SELECT Sum(price * quantity)
FROM Purchase
WHERE product = ‘bagel’
50 (= 20+30)
23
Grouping and Aggregation
Purchase(product, date, price, quantity)
SELECT product, Sum(price*quantity) AS TotalSales
FROM Purchase
WHERE date > ‘10/1/2005’
GROUP BY product
Let’s see what this means…
Find total sales after 10/1/2005 per product.
24
Grouping and Aggregation
1. Compute the FROM and WHERE clauses.
2. Group by the attributes in the GROUPBY
3. Compute the SELECT clause: grouped attributes and aggregates.
25
1&2. FROM-WHERE-GROUPBY
Product Date Price Quantity
Bagel 10/21 1 20
Bagel 10/25 1.50 20
Banana 10/3 0.5 10
Banana 10/10 1 10
26
3. SELECT
SELECT product, Sum(price*quantity) AS TotalSales
FROM Purchase
WHERE date > ‘10/1/2005’
GROUP BY product
Product Date Price Quantity
Bagel 10/21 1 20
Bagel 10/25 1.50 20
Banana 10/3 0.5 10
Banana 10/10 1 10
Product TotalSales
Bagel 50
Banana 15
27
Another Example
SELECT product,
sum(price * quantity) AS SumSales
max(quantity) AS MaxQuantity
FROM Purchase
GROUP BY product
What does
it mean ?
28
HAVING Clause
SELECT product, Sum(price * quantity)
FROM Purchase
WHERE date > ‘10/1/2005’
GROUP BY product
HAVING Sum(quantity) > 30
Same query, except that we consider only products that had
at least 100 buyers.
HAVING clause contains conditions on aggregates.
29
General form of Grouping and Aggregation
SELECT S
FROM R1,…,Rn
WHERE C1
GROUP BY a1,…,ak
HAVING C2
S = may contain attributes a1,…,ak and/or any aggregates but NO OTHER ATTRIBUTES
C1 = is any condition on the attributes in R1,…,Rn
C2 = is any condition on aggregate expressions
Why ?
30
Azure Examples
Find the total price of each order, return SalesOrderID and total price (column name should be ‘totalprice’)
Find the total price of each order where the total price > 10000
Find the total price on the black products of each order where the total price > 10000
SELECT SalesOrderID, SUM(UnitPrice*OrderQty*(1-UnitPriceDiscount)) AS TotalPrice
FROM SalesLT.SalesOrderDetail
GROUP BY SalesOrderID
SELECT SalesOrderID, SUM(UnitPrice*OrderQty*(1-UnitPriceDiscount)) AS TotalPrice
FROM SalesLT.SalesOrderDetail
GROUP BY SalesOrderID
HAVING SUM(UnitPrice*OrderQty*(1-UnitPriceDiscount)) > 10000
SELECT SalesOrderID, SUM(UnitPrice*OrderQty*(1-UnitPriceDiscount)) AS TotalPrice
FROM SalesLT.SalesOrderDetail, SalesLT.Product
WHERE SalesLT.SalesOrderDetail.ProductID = SalesLT.Product.ProductID AND Color = ‘Black’
GROUP BY SalesOrderID
HAVING SUM(UnitPrice*OrderQty*(1-UnitPriceDiscount)) > 10000
Outer Joins
Explicit joins in SQL = “inner joins”:
Product(name, category)
Purchase(prodName, store)
SELECT Product.name, Purchase.store
FROM Product JOIN Purchase ON
Product.name = Purchase.prodName
SELECT Product.name, Purchase.store
FROM Product, Purchase
WHERE Product.name = Purchase.prodName
Same as:
But Products that never sold will be lost !
33
Outerjoins
Left outer joins in SQL:
Product(name, category)
Purchase(prodName, store)
SELECT Product.name, Purchase.store
FROM Product LEFT OUTER JOIN Purchase ON
Product.name = Purchase.prodName
34
Name Category
Gizmo gadget
Camera Photo
OneClick Photo
ProdName Store
Gizmo Wiz
Camera Ritz
Camera Wiz
Name Store
Gizmo Wiz
Camera Ritz
Camera Wiz
OneClick NULL
Product
Purchase
35
Application
Compute, for each product, the total number of sales in ‘September’
Product(name, category)
Purchase(prodName, month, store)
SELECT Product.name, count(*)
FROM Product, Purchase
WHERE Product.name = Purchase.prodName
and Purchase.month = ‘September’
GROUP BY Product.name
What’s wrong ?
36
Application
Compute, for each product, the total number of sales in ‘September’
Product(name, category)
Purchase(prodName, month, store)
SELECT Product.name, count(*)
FROM Product LEFT OUTER JOIN Purchase ON
Product.name = Purchase.prodName
and Purchase.month = ‘September’
GROUP BY Product.name
Now we also get the products who sold in 0 quantity
37
Azure Examples
For each customer, find the total quantity of black products bought. Report CustomerID, FirstName, LastName, and total quantity
Multi-table joins:
For each customer, find total quantity of all black products.
Note 1: Inner joins are commutative and associative, outer joins are not!
Note 2: Joins are processed from left to right, unless forced by parentheses
Query Plan
Product (black)
OrderDetail
OrderDetails with black products
OrderHeader
OrderDetails with black products and CustomerID
Customer
OrderDetails with black products and CustomerID and other info
left join
GroupBy
Answer
select saleslt.customer.customerid, FirstName, LastName, sum(orderqty)
from saleslt.customer
left outer join
(
saleslt.salesorderheader
join saleslt.salesorderdetail
on saleslt.salesorderdetail.salesorderid = saleslt.salesorderheader.salesorderid
join saleslt.product
on saleslt.product.productid = saleslt.salesorderdetail.productid and color = ‘black’
)
on saleslt.customer.customerid = saleslt.salesorderheader.customerid
group by saleslt.customer.customerid, FirstName, LastName
order by sum(orderqty) desc
Outer Joins
Left outer join:
Include the left tuple even if there’s no match
Right outer join:
Include the right tuple even if there’s no match
Full outer join:
Include the both left and right tuples even if there’s no match
41
Alternative Answer
Alternative Answer
select saleslt.customer.customerid, sum(orderqty)
from saleslt.salesorderheader
join saleslt.salesorderdetail
on saleslt.salesorderdetail.salesorderid = saleslt.salesorderheader.salesorderid
join saleslt.product
on saleslt.product.productid = saleslt.salesorderdetail.productid and color = ‘black’
right join saleslt.customer
on saleslt.customer.customerid = saleslt.salesorderheader.customerid
group by saleslt.customer.customerid
order by sum(orderqty) desc
/docProps/thumbnail.jpeg