DB 2
DB 2
STRUCTURE
Details
• Case insensitive:
– Same: SELECT Select select
– Same: Product product
– Different: ‘Seattle’ ‘seattle’
• Constants:
– ‘abc’ - yes
– “abc” - no
SQL Query
Basic Form:
SELECT <attributes>
FROM <one or more relations>
WHERE <conditions>
Basic Query Structure (DML)
• Queries on Multiple Relations:
• A typical SQL query has the form:
– Ai represents an attribute
– ri represents a relation
– P is a predicate.
Basic Query Structure (DML)
• SQL is based on relational set operations with
certain modifications and enhancements
• Queries on a Single Relation:
• select name
from instructor;
• select dept_name
from instructor;
SELECT <attribute(s)>
FROM <table(s)> Semi-colon at end
WHERE <condition/predicate>;
6
Simple SQL Query
Product Manufacture
PName Price Category
r
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’
PName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
“selection”
Powergizmo $29.99 Gadgets GizmoWorks
Notation
Input Schema
Output Schema
Eliminating Duplicates
Category
SELECT DISTINCT category Gadgets
FROM Product Photography
Household
Compare to:
Category
Gadgets
SELECT category Gadgets
FROM Product Photography
Household
Simple SQL Query
Grades
Name Code Mark Name Code Mark
John DBS 56 Mark PR2 35
John IAI 72 Mark PR1 43
Mary DBS 60 Jane IAI 54
Mark PR1 43 John DBS 56
Mark PR2 35 Mary DBS 60
Jane IAI 54 John IAI 72
ORDER BY Example
SELECT * FROM Grades
ORDER BY Code ASC,
Mark DESC
Grades
Name Code Mark Name Code Mark
John DBS 56 Mary DBS 60
John IAI 72 John DBS 56
Mary DBS 60 John IAI 72
Mark PR1 43 Jane IAI 54
Mark PR2 35 Mark PR1 43
Jane IAI 54 Mark PR2 35
Ordering the Results
Ties are broken by the second attribute on the ORDER BY list, etc.
GizmoWorks 25 USA
Key
Canon 65 Japan
Hitachi 15 Japan
Product
PName Price Category Manufacturer
Foreign
Gizmo $19.99 Gadgets GizmoWorks
Key
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
Constants and Arithmetic
• As well as column
names, you can select SELECT Mark/100
constants, compute FROM Grades
arithmetic expressions
and evaluate functions in SELECT
a SELECT statement Salary + Bonus
FROM Employee
SELECT 1.175*Price
FROM Products
Joins
Product Company
from instructor,department
where instructor.dept_name=department.dept_name;
General form:
Example:
SELECT Best
MAX(Mark) AS Best
FROM Grades 72
Aggregate Functions
We probably want:
What does
it mean ?
SELECT product,
sum(price * quantity) AS SumSales
max(quantity) AS MaxQuantity
FROM Purchase
GROUP BY product
HAVING Clause
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
GROUP BY Store.sid, Store.name
HAVING 100 < min(Product.price)
Two Examples
Store(sid, sname)
Product(pid, pname, price, sid)
Sales
Month Department Value
March Fiction 20
March Travel 30
March Technical 40
April Fiction 10
April Fiction 30
April Travel 25
April Fiction 20
May Fiction 20
May Technical 50
GROUP BY
SELECT Month, Department, SELECT Month, Department,
SUM(Value) AS Total SUM(Value) AS Total
FROM Sales FROM Sales
GROUP BY Month, Department GROUP BY Department, Month
Month Department Total Month Department Total
April Fiction 60 April Fiction 60
April Travel 25 March Fiction 20
March Fiction 20 May Fiction 20
March Technical 40 March Technical 40
March Travel 30 May Technical 50
May Fiction 20 April Travel 25
May Technical 50 March Travel 30
HAVING