Chapter 4
Structured Query Language (SQL)
What is SQL?
o SQL is Structured Query Language, which is a computer language for storing,
manipulating and retrieving data stored in relational database.
o 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
o SQL schema
₋ Identified by a schema name
₋ Includes an authorization identifier and descriptors for each element
o Schema elements include
₋ Tables, constraints, views, domains, and other constructs
o Some statement in SQL ends with a semicolon
SQL Commands
o 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
o DML -Data Manipulation Language:
o DCL -Data Control Language:
o DQL -Data Query Language:
CREATE TABLE
o 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))
o 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
o 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 attributes
FROM relations (possibly multiple)
WHERE conditions (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 *
FROM Product
WHERE category=‘Gadgets’
PName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
“selection” Powergizmo $29.99 Gadgets GizmoWorks
11
Simple SQL Query
PName Price Category Manufacturer
Product
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”
SingleTouch $149.99 Canon
and
MultiTouch $203.99 Hitachi
“projection”
12
A Notation for SQL Queries
Input Schema
Product(PName, Price, Category, Manfacturer)
SELECT PName, Price, Manufacturer
FROM Product
WHERE Price > 100
Answer(PName, Price, Manfacturer)
Output Schema 13
Details
o Case insensitive:
₋ Same: SELECT Select select
₋ Same: Product product
₋ Different: ‘Seattle’ ‘seattle’
o Constants:
₋ ‘abc’ - yes
₋ “abc” - no
Selections
What goes in the WHERE clause:
o 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...
o Pattern matching on strings...
15
The LIKE operator
o s LIKE p: pattern matching on strings
o 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 *
FROM Products
WHERE PName LIKE ‘%gizmo%’
16
Eliminating Duplicates
Category
SELECT DISTINCT category Gadgets
FROM Product Photography
Household
Compare to:
Category
Gadgets
SELECT category Gadgets
FROM Product Photography
Household
17
Ordering the Results
SELECT pname, price, manufacturer
FROM Product
WHERE category=‘gizmo’ AND price > 50
ORDER BY price, 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 category
FROM Product
ORDER BY 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 DISTINCT category
Gadgets
FROM Product
ORDER BY category Household
Photography
Compare to:
SELECT category
FROM Product
ORDER BY 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
o 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
What is GizmoWorks 25 USA
the connection Canon 65 Japan
between
them ? Hitachi 15 Japan 22
Joins
o Product (pname, price, category, manufacturer)
o Company (cname, stockPrice, country)
o Find all products under $200 manufactured in Japan;
Join
return their names and prices.
between Product
and Company
SELECT pname, price
FROM Product, Company
WHERE manufacturer=cname AND country=‘Japan’
AND price <= 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 pname, price
FROM Product, Company
WHERE manufacturer=cname AND country=‘Japan’
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 country
FROM Product, Company
WHERE manufacturer=cname AND 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 country
FROM Product, Company
WHERE manufacturer=cname AND category=‘Gadgets’
Country
What is ??
the problem ? ??
What’s the
solution ?
26
Joins
o Product (pname, price, category, manufacturer)
o Purchase (buyer, seller, store, product)
o Person(persname, phoneNumber, city)
o 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 DISTINCT persname, store
FROM Person, Purchase, Product
WHERE persname=buyer AND product = pname AND
city=‘Seattle’ AND category=‘Gadgets’
27
Disambiguating Attributes
o Sometimes two relations have the same attr:
Person(pname, address, worksfor)
Company(cname, address)
Which
SELECT DISTINCT pname, address address ?
FROM Person, Company
WHERE worksfor = cname
SELECT DISTINCT Person.pname, Company.address
FROM Person, Company
WHERE Person.worksfor = Company.cname 28
Meaning (Semantics) of SQL Queries
SELECT a1, a2, …, ak
FROM R1 AS x1, R2 AS x2, …, Rn AS xn
WHERE Conditions
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 DISTINCT R.A
FROM R, S, T
WHERE R.A=S.A OR R.A=T.A
What does it compute ?
Computes R (S T) But what happens if T is empty?
Exercises
o Product (pname, price, category, manufacturer)
o Purchase (buyer, seller, store, product)
o Company (cname, stock price, country)
o 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
o SQL supports several aggregation operations:
o sum, count, min, max, avg
SELECT avg(price) SELECT count(*)
FROM Product FROM Product
WHERE maker=“Toyota” WHERE year > 1995
Except count, all aggregations apply to a single attribute
Aggregation: Count
COUNT applies to duplicates, unless otherwise stated:
SELECT Count(category)
FROM Product same as Count(*)
WHERE year > 1995
We probably want:
SELECT Count(DISTINCT category)
FROM Product
WHERE year > 1995
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
o Purchase(product, date, price, quantity)
o 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/2005’
GROUP BY 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 R 1,…,Rn
C2 = is any condition on aggregate expressions
General form of Grouping and Aggregation
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
SELECT S
FROM R1,…,Rn
WHERE C1
GROUP BY a1,…,ak
HAVING C2
NULLS in SQL
o Whenever we don’t have a value, we can put a NULL
o Can mean many things:
₋ Value does not exists
₋ Value exists but is unknown
₋ Value not applicable
₋ Etc.
o The schema specifies for each attribute if can be null (nullable attribute) or not
o How does SQL cope with tables that have NULLs ?
Null Values
o If x= NULL then 4*(3-x)/7 is still NULL
o If x= NULL then x=“Joe” is UNKNOWN
o In SQL there are three boolean values:
FALSE = 0
UNKNOWN = 0.5
TRUE = 1
Inner joins
o 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
Same as:
SELECT Product.name, Purchase.store
FROM Product, Purchase
WHERE Product.name = Purchase.prodName
But Products that never sold will be lost !
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
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
o Three kinds of modifications
₋ Insertions
₋ Deletions
₋ Updates
o 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.
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
Insertion: an Example
Product(name, listPrice, category)
Purchase(prodName, buyerName, 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 INTO Product(name)
SELECT DISTINCT prodName
FROM Purchase
WHERE prodName NOT IN (SELECT name FROM Product)
name listPrice category
gizmo 100 Gadgets
camera - -
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.
Updates
Example:
UPDATE PRODUCT
SET price = price/2
WHERE Product.name IN
(SELECT product
FROM Purchase
WHERE Date =‘Oct, 25, 1999’);