0% found this document useful (0 votes)
9 views52 pages

Chapter 6

Chapter 6 provides an overview of Structured Query Language (SQL), detailing its purpose for managing and manipulating relational databases. It covers SQL commands, data types, query structure, and the use of joins, selections, and aggregations. The chapter also includes examples of SQL queries and exercises for practical application.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views52 pages

Chapter 6

Chapter 6 provides an overview of Structured Query Language (SQL), detailing its purpose for managing and manipulating relational databases. It covers SQL commands, data types, query structure, and the use of joins, selections, and aggregations. The chapter also includes examples of SQL queries and exercises for practical application.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 52

Chapter 6

Structured Query Language (SQL)


What is SQL?
• SQL is Structured Query Language, which is a computer
language for storing, manipulating and retrieving data stored
in relational database.
• Why SQL?
– Allows users to access data in relational database management
systems.
– Allows users to describe the data.
– Allows users to define the data in database and manipulate that
data.
– Allows to embed within other languages using SQL modules,
libraries & pre-compilers.
– Allows users to create and drop databases and tables.
– Allows users to create view, stored procedure, functions in a
database.
– Allows users to set permissions on tables, procedures and views
Schema and Catalog Concepts in SQL
 SQL schema
 Identified by a schema name
 Includes an authorization identifier and
descriptors for each element
 Schema elements include
 Tables, constraints, views, domains, and other
constructs
 Some statement in SQL ends with a semicolon
SQL Commands
• The standard SQL commands to interact with
relational databases are CREATE, SELECT,
INSERT, UPDATE, DELETE and DROP. These
commands can be classified into groups based
on their nature:
– DDL -Data Definition Language:
SQL Commands
• DML -Data Manipulation Language:

• DCL -Data Control Language:

• DQL -Data Query Language:


CREATE TABLE
• Specifies a new base relation by giving it a name, and
specifying each of its attributes and their data types
(INTEGER, FLOAT, DECIMAL(i,j), CHAR(n),
VARCHAR(n))
• A constraint NOT NULL may be specified on an
attribute
CREATE TABLE DEPARTMENT (
DNAME VARCHAR(10)
NOT NULL,
DNUMBER INTEGER NOT
NULL,
MGRSSN CHAR(9),
MGRSTARTDATE CHAR(9) );
Data Types
• SQL data type is an attribute that specifies type of data of any
object. Each column, variable and expression has related data
type in SQL.
– Numeric: INTEGER, INT, FLOAT, DECIMAL
– Character: CHAR(n), VARCHAR(n), VARCHAR2(n), CHAR VARYING(n)
– Bit String: BLOB, CLOB
– Boolean: true, false, and null
– Date and Time: DATE (YYYY-MM-DD) TIME( HH:MM:SS)
– Timestamp: DATE + TIME
– USER Defined types
Some SQL datatypes
Comparison operation and description
SQL Query

Basic form: (plus many many more bells and whistles)

SELECT
SELECT attributes
attributes
FROM
FROM relations
relations(possibly
(possiblymultiple)
multiple)
WHERE
WHERE conditions
conditions(selections)
(selections)

10
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
SELECT **
FROM
FROM Product
Product
WHERE
WHERE category=‘Gadgets’
category=‘Gadgets’
PName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
“selection”
11
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
SELECT PName,
PName,Price,
Price,Manufacturer
Manufacturer
FROM
FROM Product
Product
WHERE
WHERE Price
Price>>100
100
PName Price Manufacturer
“selection” and SingleTouch $149.99 Canon
“projection” MultiTouch $203.99 Hitachi
12
A Notation for SQL Queries
Input Schema

Product(PName, Price, Category, Manfacturer)

SELECT
SELECT PName,
PName,Price,
Price,Manufacturer
Manufacturer
FROM
FROM Product
Product
WHERE
WHERE Price
Price>>100
100
Answer(PName, Price, Manfacturer)

Output Schema 13
Details
• Case insensitive:
– Same: SELECT Select select
– Same: Product product
– Different: ‘Seattle’ ‘seattle’

• Constants:
– ‘abc’ - yes
– “abc” - no
Selections
What goes in the WHERE clause:
• x = y, x < y, x <= y, etc
– For number, they have the usual meanings
– For CHAR and VARCHAR: lexicographic ordering
• Expected conversion between CHAR and VARCHAR
– For dates and times, what you expect...
• Pattern matching on strings...

15
The LIKE operator
• s LIKE p: pattern matching on strings
• p may contain two special symbols:
– % = any sequence of characters
– _ = any single character

Product(PName, Price, Category, Manufacturer)


Find all products whose name mentions ‘gizmo’:

SELECT
SELECT **
FROM
FROM Products
Products
WHERE
WHERE PName
PNameLIKE
LIKE‘%gizmo%’
‘%gizmo%’

16
Eliminating Duplicates
Category
SELECT
SELECT DISTINCT
DISTINCTcategory
category Gadgets
FROM
FROM Product
Product Photography
Household

Compare to:
Category
Gadgets
SELECT
SELECT category
category Gadgets
FROM
FROM Product
Product Photography
Household

17
Ordering the Results
SELECT
SELECT pname,
pname,price,
price,manufacturer
manufacturer
FROM
FROM Product
Product
WHERE
WHERE category=‘gizmo’
category=‘gizmo’AND
ANDprice
price>>50
50
ORDER
ORDERBYBY price,
price,pname
pname

Ordering is ascending, unless you specify the DESC keyword.

Ties are broken by the second attribute on the ORDER BY list, etc.

18
Ordering the Results
SELECT
SELECT category
category
FROM
FROM Product
Product
ORDER
ORDERBYBY pname
pname

PName Price Category Manufacturer

?
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi

19
Ordering the Results
Category
SELECT
SELECT DISTINCT
DISTINCTcategory
category Gadgets
FROM
FROM Product
Product Household
ORDER
ORDERBYBYcategory
category Photography

Compare to:

SELECT
SELECT category
?
category
FROM
FROM Product
Product
ORDER
ORDERBYBYpname
pname

20
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
Joins in SQL
• Connect two or more tables:
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

Company Cname StockPrice Country

GizmoWorks 25 USA
What is
the connection Canon 65 Japan
between
them ? Hitachi 15 Japan 22
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
SELECT pname,
pname,price
price and Company
FROM
FROM Product,
Product,Company
Company
WHERE
WHERE manufacturer=cname
manufacturer=cnameAND
ANDcountry=‘Japan’
country=‘Japan’
AND
ANDprice
price<=
<=200
200

23
Joins in SQL
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
SELECT pname,
pname,price
price
FROM
FROM Product,
Product,Company
Company
WHERE
WHERE manufacturer=cnameAND
manufacturer=cname ANDcountry=‘Japan’
country=‘Japan’
AND price <= 200
AND price <= 200

PName Price
SingleTouch $149.99

24
Joins
Product (pname, price, category, manufacturer)
Company (cname, stockPrice, country)

Find all countries that manufacture some product in the


‘Gadgets’ category.

SELECT
SELECT country
country
FROM
FROM Product,
Product,Company
Company
WHERE
WHERE manufacturer=cname
manufacturer=cnameAND
ANDcategory=‘Gadgets’
category=‘Gadgets’

25
Joins in SQL
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
SELECT country
country
FROM
FROM Product,
Product,Company
Company
WHERE
WHERE manufacturer=cname
manufacturer=cnameAND
ANDcategory=‘Gadgets’
category=‘Gadgets’

Country
What is ??
the problem ? ??
What’s the
solution ? 26
Joins
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
SELECT DISTINCT
DISTINCTpersname,
persname,store
store
FROM
FROM Person,
Person,Purchase,
Purchase,Product
Product
WHERE
WHERE persname=buyer
persname=buyerAND ANDproduct
product==pname
pnameAND
AND
city=‘Seattle’
city=‘Seattle’ AND
ANDcategory=‘Gadgets’
category=‘Gadgets’ 27
Disambiguating Attributes
• Sometimes two relations have the same attr:
Person(pname, address, worksfor)
Company(cname, address)
Which
SELECT
SELECT DISTINCT
DISTINCTpname,
pname,address
address address ?
FROM
FROM Person,
Person,Company
Company
WHERE
WHERE worksfor
worksfor==cname
cname

SELECT
SELECT DISTINCT
DISTINCTPerson.pname,
Person.pname,Company.address
Company.address
FROM
FROM Person,
Person,Company
Company
WHERE
WHERE Person.worksfor
Person.worksfor==Company.cname
Company.cname
28
Meaning (Semantics) of SQL
Queries
SELECT
SELECTaa11,,aa22,,…,
…,aakk
FROM
FROM RR11AS ASxx11,,RR22AS
ASxx22,,…,
…,RRnnAS
ASxxnn
WHERE
WHERE Conditions
Conditions
Answer
Answer=={} {}
for
forxx11in
inRR11do
do
for
forxx22in
inRR22dodo
…..
…..
for
forxxnnin
inRRnndo
do
ififConditions
Conditions
then
thenAnswer Answer
Answer==Answer {(a
{(a11,…,a
,…,akk)}
)}
return
returnAnswer
Answer
An Unintuitive Query

SELECT
SELECT DISTINCT
DISTINCTR.A
R.A
FROM
FROM R,R,S,
S,TT
WHERE
WHERE R.A=S.A
R.A=S.A OR
OR R.A=T.A
R.A=T.A

What does it compute ?

Computes R Ç (S È T) But what happens if T is empty?


Exercises
Product (pname, price, category, manufacturer)
Purchase (buyer, seller, store, product)
Company (cname, stock price, country)
Person(per-name, phone number, city)

Ex #1: Find people who bought telephony products.


Ex #2: Find names of people who bought American products
Ex #3: Find names of people who bought American products and they
live in Seattle.
Ex #4: Find people who have both bought and sold something.
Ex #5: Find people who bought stuff from Joe or bought products
from a company whose stock prices is more than $50.
31
Aggregation
SQL supports several aggregation operations:

sum, count, min, max, avg

SELECT
SELECT avg(price)
avg(price) SELECT
SELECT count(*)
count(*)
FROM
FROM Product
Product FROM
FROM Product
Product
WHERE
WHERE maker=“Toyota”
maker=“Toyota” WHERE
WHERE year
year>>1995
1995

Except count, all aggregations apply to a single attribute


Aggregation: Count
COUNT applies to duplicates, unless otherwise stated:

SELECT
SELECT Count(category)
Count(category) same as Count(*)
FROM
FROM Product
Product
WHERE
WHERE year
year>>1995
1995

We probably want:

SELECT
SELECT Count(DISTINCT
Count(DISTINCTcategory)
category)
FROM
FROM Product
Product
WHERE
WHERE year
year>>1995
1995
Purchase
Simple Aggregations
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
SELECT Sum(price
Sum(price**quantity)
quantity)
FROM
FROM Purchase
Purchase 50 (= 20+30)
WHERE
WHERE product
product==‘bagel’
‘bagel’
Grouping and Aggregation
Purchase(product, date, price, quantity)

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

SELECT
SELECT product,
product,Sum(price*quantity)
Sum(price*quantity)AS
ASTotalSales
TotalSales
FROM
FROM Purchase
Purchase
WHERE
WHERE date
date>>‘10/1/2005’
‘10/1/2005’
GROUP
GROUPBYBY product
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 15
Banana 10/10 1 10

SELECT
SELECT product,
product,Sum(price*quantity)
Sum(price*quantity)AS
ASTotalSales
TotalSales
FROM
FROM Purchase
Purchase
WHERE
WHERE date
date>>‘10/1/2005’
‘10/1/2005’
GROUP
GROUPBYBY product
product
General form of Grouping and
Aggregation
SELECT S
FROM R1,…,Rn
WHERE C1
GROUP BY a1,…,ak Why ?
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
SELECT SS
FROM
FROM RR11,…,R
,…,Rnn
WHERE
WHERE C1C1
GROUP
GROUPBYBYaa11,…,a
,…,akk
HAVING
HAVING C2C2
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
NULLS in SQL
• Whenever we don’t have a value, we can put a NULL
• Can mean many things:
– Value does not exists
– Value exists but is unknown
– Value not applicable
– Etc.
• The schema specifies for each attribute if can be null
(nullable attribute) or not
• How does SQL cope with tables that have NULLs ?
Null Values
• If x= NULL then 4*(3-x)/7 is still NULL

• If x= NULL then x=“Joe” is UNKNOWN


• In SQL there are three boolean values:
FALSE = 0
UNKNOWN = 0.5
TRUE = 1
innerjoins
Explicit joins in SQL = “inner joins”:
Product(name, category)
Purchase(prodName, store)

SELECT
SELECTProduct.name,
Product.name,Purchase.store
Purchase.store
FROM
FROM Product
ProductJOIN
JOINPurchase
PurchaseONON
Product.name
Product.name==Purchase.prodName
Purchase.prodName

Same as: SELECT


SELECTProduct.name,
Product.name,Purchase.store
Purchase.store
FROM
FROM Product,
Product,Purchase
Purchase
WHERE
WHERE Product.name
Product.name==Purchase.prodName
Purchase.prodName
But Products that never sold will be lost !
Outerjoins
Left outer joins in SQL:
Product(name, category)
Purchase(prodName, store)

SELECT
SELECTProduct.name,
Product.name,Purchase.store
Purchase.store
FROM
FROM Product
ProductLEFT
LEFTOUTER
OUTERJOIN
JOINPurchase
PurchaseON
ON
Product.name
Product.name==Purchase.prodName
Purchase.prodName
Product Purchase
Name Category ProdName Store

Gizmo gadget Gizmo Wiz

Camera Photo Camera Ritz

OneClick Photo Camera Wiz

Name Store

Gizmo Wiz

Camera Ritz

Camera Wiz

OneClick NULL
Modifying the Database
Three kinds of modifications
• Insertions
• Deletions
• Updates

Sometimes they are all called “updates”


Insertions
General form:

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

Example: Insert a new purchase to the database:


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

Missing attribute  NULL.


Insertions
INSERT
INSERT INTO
INTO PRODUCT(name)
PRODUCT(name)

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

The query replaces the VALUES keyword.


Here we insert many tuples into PRODUCT
Insertion: an Example
Product(name,
Product(name,listPrice,
listPrice,category)
category)
Purchase(prodName,
Purchase(prodName,buyerName,
buyerName,price)
price)
prodName is foreign key in Product.name

Suppose database got corrupted and we need to fix it:


Purchase
Product
prodName buyerName price
name listPrice category
camera John 200

gizmo 100 gadgets gizmo Smith 80

camera Smith 225

Task: insert in Product all prodNames from Purchase


Insertion: an Example
INSERT
INSERT INTO
INTO Product(name)
Product(name)

SELECT
SELECT DISTINCT
DISTINCT prodName
prodName
FROM
FROM Purchase
Purchase
WHERE
WHERE prodName
prodName NOT
NOTININ(SELECT
(SELECT name
nameFROM
FROM Product)
Product)

name listPrice category

gizmo 100 Gadgets

camera - -
Deletions
Example:

DELETE
DELETE FROM
FROM PURCHASE
PURCHASE

WHERE
WHERE seller
seller==‘Joe’
‘Joe’ AND
AND
product
product==‘Brooklyn
‘BrooklynBridge’
Bridge’

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


occurrence of a tuple that appears twice
in a relation.
Updates
Example:

UPDATE
UPDATE PRODUCT
PRODUCT
SET
SET price
price==price/2
price/2
WHERE
WHERE Product.name
Product.name ININ
(SELECT
(SELECTproduct
product
FROM
FROM Purchase
Purchase
WHERE
WHERE DateDate=‘Oct,
=‘Oct,25,
25,1999’);
1999’);

You might also like