0% found this document useful (0 votes)
8 views62 pages

DB 2

Uploaded by

Abdul Manan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views62 pages

DB 2

Uploaded by

Abdul Manan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 62

BASIC QUERY

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:

select A1, A2, ..., An


from r1, r2, ..., rm
where P

– 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 distinct dept_name The instructor table


from instructor;
SELECT Query Structure
• Basic Form

SELECT <attribute(s)>
FROM <table(s)> Semi-colon at end
WHERE <condition/predicate>;

* denotes all columns


SELECT *
FROM branch
WHERE branch_city=‘Islamabad’;

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

Product(PName, Price, Category, Manfacture

SELECT PName, Price, Manufacturer


FROM Product
WHERE Price > 100

Answer(PName, Price, Manfacturer)

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

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

SELECT PName, Price, Manufacturer


FROM Product
WHERE Price > 100

PName Price Manufacturer


“selection” and SingleTouch $149.99 Canon
“projection” MultiTouch $203.99 Hitachi
CROSS Product -The from Clause!
• The from clause lists the relations
involved in the query
• select * from borrower, loan
ORDER BY
• The ORDER BY clause SELECT <columns>
sorts the results of a FROM <tables>
query WHERE <condition>
– You can sort in ascending
(default) or descending ORDER BY <cols>
order [ASCENDING |
– Multiple columns can be DESCENDING|
given
ASC | DESC ]
ORDER BY Example
SELECT * FROM Grades
ORDER BY Mark

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

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.


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 DISTINCT category


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
CName StockPrice Country

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 (pname, price, category, manufacturer)


Company (cname, stockPrice, country)

Find all products under $200 manufactured in


Japan;
return their names and prices. Join
between Product
SELECT PName, Price and Company
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 PName, Price


FROM Product, Company
WHERE Manufacturer=CName AND Country=‘Japan’
AND Price <= 200 PName Price
SingleTouch $149.99
Tuple Variables
Person(pname, address, worksfor)
Company(cname, address)
Which
SELECT DISTINCT pname, address address ?
FROM Person, Company
WHERE worksfor = cname
SELECT DISTINCT Person.pname, Company.addres
FROM Person, Company
WHERE Person.worksfor = Company.cname
SELECT DISTINCT x.pname, y.address
FROM Person AS x, Company AS y
WHERE x.worksfor = y.cname
Basic Query Structure (DML)
• Queries on Multiple Relations (Example):
– Names of all instructors along with their department names and
department building
select name,instructor.dept_name,building

from instructor,department

where instructor.dept_name=department.dept_name;

The department table


The instructor table
Modifying the Database

Three kinds of modifications


• Insertions
• Deletions
• Updates

Sometimes they are all called “updates”


Insertions

General form:

INSERT INTO R(A1,…., An) VALUES (v1,…., vn)

Example: Insert a new purchase to the database:


INSERT INTO Purchase(buyer, seller, product, store)
VALUES (‘Joe’, ‘Fred’, ‘wakeup-clock-espresso-machine’,
‘The Sharper Image’)

Missing attribute  NULL.


May drop attribute names if give them in order.
Insertions

INSERT INTO PRODUCT(name)

SELECT DISTINCT Purchase.product


FROM Purchase
WHERE Purchase.date > “10/26/01”

The query replaces the VALUES keyword.


Here we insert many tuples into PRODUCT
Deletions

Example:

DELETE FROM PURCHASE

WHERE seller = ‘Joe’ AND


product = ‘Brooklyn Bridge’

Factoid about SQL: there is no way to delete only a single


occurrence of a tuple that appears twice
in a relation.
Aggregate Functions
• Aggregate functions • Aggregate functions
compute summaries of – COUNT: The number of
data in a table rows
– Most aggregate functions – SUM: The sum of the
(all except COUNT) work entries in a column
on a single column of – AVG: The average entry in
numeric data a column
– Use an alias to name the – MIN, MAX: The minimum
result and maximum entries in a
column
Scalar & Vector Aggregates
• The aggregate functions like SUM, AVG,
MIN, MAX & COUNT provide single values
summary for the attribute values on which
they apply. The values returned by them are
called Scalar Aggregates.

• The aggregate functions when used with


group by clause provide multiple results.
These values are called Vector Aggregates.
Aggregate Functions
• These functions operate on the multiset of
values of a column of a relation, and return
a value

• avg: average value


• min: minimum value
• max: maximum value
• sum: sum of values
• count: number of values
Aggregate Functions

Grades SELECT Count


Name Code Mark COUNT(*) AS Count
FROM Grades 6
John DBS 56
John IAI 72
Mary DBS 60 SELECT Total
Mark PR1 43 SUM(Mark) AS Total
Mark PR2 35 FROM Grades 320
Jane IAI 54

SELECT Best
MAX(Mark) AS Best
FROM Grades 72
Aggregate Functions

• You can combine SELECT


aggregate functions MAX(Mark)-MIN(Mark)
using arithmetic AS Range
FROM Grades
Grades
Name Code Mark
John DBS 56
John IAI 72 MAX(Mark) = 72 Range
Mary DBS 60
Mark PR1 43 37
Mark PR2 35 MIN(Mark) = 35
Jane IAI 54
Aggregate Functions (Cont.)
• Find the average salary of instructors in the Computer Science
department.
select avg (salary)
from instructor
where dept_name = ‘Comp.Sci.'

 Find the total number of instructors who teach a course in the


Spring 2010 semester
select count (distinct ID)
from teaches
where semester=‘Spring’and year=2010
 Find the number of tuples in the course relation

select count (*)


from course
Aggregation

SELECT avg(price) SELECT count(*)


FROM Product FROM Product
WHERE maker=“Toyota” WHERE year > 1995

SQL supports several aggregation operations:

sum, count, min, max, avg

Except count, all aggregations apply to a single attribute


Aggregation: Count

COUNT applies to duplicates, unless otherwise stated:

SELECT Count(category) same as Count(*)


FROM Product
WHERE year > 1995

We probably want:

SELECT Count(DISTINCT category)


FROM Product
WHERE year > 1995
More Examples

Purchase(product, date, price, quantity)

SELECT Sum(price * quantity)


FROM Purchase
What do
they mean ?
SELECT Sum(price * quantity)
FROM Purchase
WHERE product = ‘bagel’
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 50 (= 20+30)
WHERE product = ‘bagel’
Grouping and Aggregation
Purchase(product, date, price, quantity)

Find total sales after 10/1/2005 per product.

SELECT product, Sum(price*quantity) AS TotalSales


FROM Purchase
WHERE date > ‘10/1/2005’
GROUP BY product

Let’s see what this means…


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.


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
3. SELECT

Product Date Price Quantity Product TotalSales


Bagel 10/21 1 20
Bagel 10/25 1.50 20 Bagel 50
Banana 10/3 0.5 10
Banana 10/10 1 10
Banana 15

SELECT product, Sum(price*quantity) AS TotalSales


FROM Purchase
WHERE date > ‘10/1’
GROUP BY product
Another Example

What does
it mean ?

SELECT product,
sum(price * quantity) AS SumSales
max(quantity) AS MaxQuantity
FROM Purchase
GROUP BY product
HAVING Clause

Same query, except that we consider only products that had


at least 100 buyers.

SELECT product, Sum(price * quantity)


FROM Purchase
WHERE date > ‘10/1/2005’
GROUP BY product
HAVING Sum(quantity) > 30

HAVING clause contains conditions on aggregates.


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
General form of Grouping and
Aggregation
SELECT S
FROM R1,…,Rn
WHERE C1
GROUP BY a1,…,ak
HAVING C2
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
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
GROUP BY Store.sid, Store.name
HAVING 100 < min(Product.price)
Two Examples

Store(sid, sname)
Product(pid, pname, price, sid)

For each store,


find its most expensive product
Two Examples
This is easy but doesn’t do what we want:
SELECT Store.sname, max(Product.price)
FROM Store, Product
WHERE Store.sid = Product.sid
GROUP BY Store.sid, Store.sname
Aggregate Functions – Group By
Find the total loan amount for each branch

select branch_name, sum(amount)


from loan
group by branch_name
Aggregate Functions – Group By
• Find the average salary of instructors in each department

select dept_name, avg(salary) as avg_salary


from instructor
group by dept_name
Aggregate Functions – Group By
contd.
• Find the number of customers with accounts in each branch

select branch_name, count (distinct customer_name)


from depositor, account
where depositor.account_number = account.account_number
group by branch_name

branch (branch_name, branch_city, assets)

customer (customer_name, customer_street, customer_city)

account (account_number, branch_name, balance)

loan (loan_number, branch_name, amount)

depositor (customer_name, account_number)

borrower (customer_name, loan_number)


Aggregate Functions – Group By
contd.
• Find the number of instructors in each department who teach a course in the Spring 2010
semster

select dept_name, count (distinct ID)


from instructor, teaches
where semester=‘Spring’ and year=2010 and
instructor.ID=teaches.ID
group by dept_name

instructor table teaches table


Aggregate Functions – Having Clause
• Find the names of all branches where the average account
balance is more than $1,200

select branch_name, avg (balance)


from account
group by branch_name
having avg (balance) > 1200

Note: predicates in the having clause are applied after the


formation of groups whereas predicates in the where
clause are applied before forming groups
Null Values
• It is possible for tuples to have a null value, denoted
by null, for some of their attributes
• null signifies an unknown value or that a value does
not exist.
• The predicate is null can be used to check for null
values.
– Example: Find loan numbers of
customers whose amount is null.
select loan_number
from loan
where amount is null
• The result of any arithmetic expression involving null
is null
– Example: 5 + null returns null
• However, aggregate functions simply ignore nulls
Null Values and Aggregates
• Total all loan amounts
select sum (amount )
from loan
– Above statement ignores null
amounts
– Result is null if there is no non-null
amount
• All aggregate operations except
count(*) ignore tuples with null values
on the aggregated attributes
GROUP BY

• Sometimes we want to • The GROUP BY clause


apply aggregate functions does this
to groups of rows
• Example, find the SELECT <cols1>
average mark of each FROM <tables>
student
GROUP BY <cols2>
GROUP BY

• Every entry in <cols1>


must be in <cols2>, be a
SELECT <cols1> constant, or be an
aggregate function
FROM <tables>
• You can have WHERE or
GROUP BY <cols2> ORDER BY clauses as
well as a GROUP BY
clause
GROUP BY

Grades SELECT Name,


AVG(Mark) AS Average
Name Code Mark FROM Grades
John DBS 56 GROUP BY Name
John IAI 72
Mary DBS 60
Mark PR1 43 Name Average
Mark PR2 35
Jane IAI 54
John 64
Mary 60
Mark 39
Jane 54
GROUP BY
• Find the total value of the sales for each department in
each month
– Can group by Month then Department or Department then Month
– Same results, but in a different order

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

• HAVING is like a WHERE


SELECT Name,
clause, except that it AVG(Mark) AS Average
applies to the results of a FROM Grades
GROUP BY Name
GROUP BY query HAVING AVG(Mark) >= 40
• It can be used to select
groups which satisfy a
given condition Name Average
John 64
Mary 60
Jane 54
WHERE and HAVING
• WHERE refers to the • Think of a query being
rows of tables, and so processed as follows:
cannot use aggregate • Tables are combined
functions • WHERE clauses
• HAVING refers to the • GROUP BY and
groups of rows, and so Aggregates
cannot use columns • Column selection
which are not in the • HAVING clauses
GROUP BY • ORDER BY

You might also like