Database SystemsCSE 303
Lecture 03: SQL
1
2016
Single Table Query
CSE 303: Ashikur Rahman
SQL
• Data Definition Language (DDL)
– Create/alter/delete tables and their attributes
– Following lectures...
2
– Following lectures...
• Data Manipulation Language (DML)
– Query one or more tables – discussed next !
– Insert/delete/modify tuples in tables
CSE 303: Ashikur Rahman
Tables in SQL
PName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
Product
Attribute namesTable name
3
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
Tuples or rows CSE 303: Ashikur Rahman
SQL Query
Basic form: (plus many many more bells and whistles)
4
Basic form: (plus many many more bells and whistles)
SELECT <attributes>FROM <one or more relations>
CSE 303: Ashikur Rahman
Simplest SQL Query
PName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
Product
5
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
Show all entries from product table
CSE 303: Ashikur Rahman
Simplest 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
Product
6
MultiTouch $203.99 Household Hitachi
SELECT *FROM Product
“selection”
PName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household HitachiCSE 303: Ashikur Rahman
Single Column Selection
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
Show all product categories
7
MultiTouch $203.99 Household Hitachi
SELECT categoryFROM Product
Category
Gadgets
Gadgets
Photography
Household
“selection” and“projection”
CSE 303: Ashikur Rahman
Single Column Selection (Eliminating Duplicates)
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
8
MultiTouch $203.99 Household Hitachi
SELECT DISTINCT categoryFROM Product
Category
Gadgets
Photography
Household
CSE 303: Ashikur Rahman
Show all product categories
In summary..
SELECT DISTINCT categoryFROM Product
Category
Gadgets
Photography
Household
99
Compare to:
SELECT categoryFROM Product
Category
Gadgets
Gadgets
Photography
Household
CSE 303: Ashikur Rahman
Multi-column Selection
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
List all products along with their price and manufacturers
10
MultiTouch $203.99 Household Hitachi
SELECT pname, price, manufacturerFROM Product
PName Price Manufacturer
Gizmo $19.99 GizmoWorks
Powergizmo $29.99 GizmoWorks
SingleTouch $149.99 Canon
MultiTouch $203.99 HitachiCSE 303: Ashikur Rahman
Notation
Product(PName, Price, Category, Manfacturer)
Input Schema
11
Product(PName, Price, Category, Manfacturer)
Answer(PName, Price, Manfacturer)
Output Schema
SELECT pname, price, manufacturerFROM Product
CSE 303: Ashikur Rahman
Duplicates from multiple columnMaker Model Type
A 1001 pc
A 1002 pc
A 1003 pc
A 2004 laptop
A 2005 laptop
SELECT maker, typeFROM Product
1212
A 2005 laptop
A 2006 laptop
B 1004 pc
B 1005 pc
B 1006 pc
B 2007 laptop
C 1007 pc
… … …
…. … …
?CSE 303: Ashikur Rahman
Duplicates from multiple column
SELECT maker, typeFROM Product
Maker Type
A pc
A pc
A pc
A laptop
A laptop
1313CSE 303: Ashikur Rahman
A laptop
A laptop
B pc
B pc
B pc
B laptop
C pc
… …
…. …
Duplicates from multiple columnMaker Type
A pc
A pc
A pc
A laptop
A laptop
SELECT DISTINCT maker, typeFROM Product
1414
A laptop
A laptop
B pc
B pc
B pc
B laptop
C pc
… …
…. …
CSE 303: Ashikur Rahman
Duplicates from multiple columnMaker Type
A pc
A laptop
B pc
B laptop
C pc
1515
C pc
… …
…. …
… …
…. …
… …
…. …
… …
…. …
CSE 303: Ashikur Rahman
Simple Predicates
1616
SELECT <attributes>FROM <one or more relations>WHERE <conditions>
1
2
3
CSE 303: Ashikur Rahman
Semantics
SELECT a1,…,ak
FROM RWHERE conditions
1
2
3
1717CSE 303: Ashikur Rahman
Answer = {}for each x in R do
if conditions satisfied thenAnswer = Answer {(a1,…,ak)}
return Answer
Number Range Selection
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
Show all product’s info that will cost more than 100 dollars
1818
MultiTouch $203.99 Household Hitachi
SELECT *FROM ProductWHERE price > 100
PName Price Category Manufacturer
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
CSE 303: Ashikur Rahman
Answer = {}for each x in R do
if conditions satisfied thenAnswer = Answer {(a1,…,ak)}
return Answer
Number Range Operators
• Greater than “>”
• Less than “<”
19
• Less than “<”
• Greater than or equal to “>=”
• Less than or equal to “<=”
• Two forms of not-equal-to
“!=” and “< >”
CSE 303: Ashikur Rahman
BETWEEN-AND operator
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
Show all product categoriesShow all product’s info in the price range $100 to $200 (inclusive)
2020
MultiTouch $203.99 Household Hitachi
SELECT *FROM ProductWHERE price BETWEEN 100 AND 200
PName Price Category Manufacturer
SingleTouch $149.99 Photography CanonBETWEEN-ANDis inclusive
CSE 303: Ashikur Rahman
String Matching
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
Show all household product’s info
2121
MultiTouch $203.99 Household Hitachi
SELECT pname, price, manufacturerFROM ProductWHERE category =‘Household’
PName Price Manufacturer
MultiTouch $203.99 Hitachi
CSE 303: Ashikur Rahman
Matches even category is fixed length CHAR data type right padded with blank
The LIKE operator
• s LIKE p: pattern matching on strings
• p may contain two special symbols:
(1) % any sequence of characters (zero or more)
22
(1) % any sequence of characters (zero or more)
(2) _ any single character (exactly one)
CSE 303: Ashikur Rahman
The LIKE operator
PName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
Show all product’s info that has ‘mo’ as a substring of the product name
23
SELECT *FROM ProductsWHERE pname LIKE ‘%mo%’
PName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
CSE 303: Ashikur Rahman
The LIKE operator
PName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
Show all product’s info where the product name ends with ‘Touch’
24
SELECT *FROM ProductsWHERE pname LIKE ‘%Touch’
PName Price Category Manufacturer
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
CSE 303: Ashikur Rahman
The LIKE operatorPName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
25
SELECT *FROM ProductsWHERE manufacturer LIKE ‘G_z_oWorks’
PName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
CSE 303: Ashikur Rahman
Searching for three special symbols
• ’ ‘Baby’s World’
• % ‘10%’
26
• % ‘10%’
• _ ‘USA_Canada’
CSE 303: Ashikur Rahman
Searching for three special symbols
• ’ ‘Baby’s World’
Search for all company names having apostrophe s in the
27
Search for all company names having apostrophe s in the name?
Use double apostrophe
SELECT *
FROM Company
WHERE cname LIKE ‘%’’s%’
CSE 303: Ashikur Rahman
Searching for three special symbols
• % ‘10%’
Search for all interest rates having percent at the end?
28
Search for all interest rates having percent at the end?
Use ESCAPE characters
SELECT *
FROM Bank
WHERE interestRates LIKE ‘%\%’ ESCAPE ‘\’
CSE 303: Ashikur Rahman
Searching for three special symbols
• ‘_’ ‘USA_Canada’
Search for all country names having underscore inside?
29
Search for all country names having underscore inside?
Use ESCAPE characters
SELECT *
FROM Company
WHERE country LIKE ‘%\_%’ ESCAPE ‘\’
CSE 303: Ashikur Rahman
LIKE does not pad with blank before matching
SELECT pname, price, manufacturerFROM ProductWHERE category =‘Household’
CSE 303: Ashikur Rahman 30
WHERE category =‘Household’
SELECT pname, price, manufacturerFROM ProductWHERE category LIKE ‘Household’
Not equivalent
The NOT LIKE operatorPName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
3131
SELECT *FROM ProductsWHERE manufacturer NOT LIKE ‘%mo%’
PName Price Category Manufacturer
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
CSE 303: Ashikur Rahman
PName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
Product
Combining Predicates
3232
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
Show all household products having prices more than $100
CSE 303: Ashikur Rahman
Combining Predicates
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
Show all manufacturer that make photography or household items
333333
MultiTouch $203.99 Household Hitachi
SELECT DISTINCT makerFROM ProductWHERE category =‘Photography’OR category =‘Household’
Manufacturer
Canon
HitachiCSE 303: Ashikur Rahman
Combining Predicates
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
Show all manufacturer that make both photography and household items
WRONG!
343434
MultiTouch $203.99 Household Hitachi
SELECT DISTINCT makerFROM ProductWHERE category =‘Photography’AND category =‘Household’
PName Price Category Manufacturer
CSE 303: Ashikur Rahman
Combining PredicatesPName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
SELECT *
Product
353535
SELECT *FROM ProductWHERE pname LIKE ‘%Giz%’OR category LIKE ‘%hold’AND price > 100
Pname Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
MultiTouch $203.99 Household Hitachi
AND has higher precedence over OR
CSE 303: Ashikur Rahman0
Combining PredicatesPName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
SELECT *
Product
363636
SELECT *FROM ProductWHERE (pname LIKE ‘%Giz%’OR category LIKE ‘%hold’)AND price > 100
Pname Price Category Manufacturer
MultiTouch $203.99 Household Hitachi
CSE 303: Ashikur Rahman
Ordering the Results
SELECT pname, price, manufacturer
PName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
Default order is AscendingUse DESC for descending
37
SELECT pname, price, manufacturerFROM ProductWHERE price > 50ORDER BY pname
CSE 303: Ashikur Rahman
Pname Price Category Manufacturer
MultiTouch $203.99 Household Hitachi
SingleTouch $149.99 Photography Canon
ORDER BY: Sorting the Results
SELECT PName, Price, ManufacturerFROM ProductWHERE Category=‘gizmo’ AND Price > 50ORDER BY Price, PName
CSE 303: Ashikur Rahman 38
ORDER BY Price, PName
Ties are broken by the second attribute on the ORDER BY list, etc.
Ties are broken by the second attribute on the ORDER BY list, etc.
Ordering is ascending, unless you specify the DESC keyword.
Ordering is ascending, unless you specify the DESC keyword.
IN operator
artist album tracks company year
U2 The Unforgettable 10 Island 1984
U2 Rattle and Hum 17 Island 1988
U2 Achtung Boys 12 Island 1991
Underworld Second Toughest 8 Junior 1996
CDCollection
Find all albums launched in 1984 or 1996.
3939
SELECT albumFROM CDCollectionWHERE year IN (1984, 1996)
artist album tracks company year
U2 The Unforgettable 10 Island 1984
Underworld Second Toughest 8 Junior 1996
SELECT albumFROM CDCollectionWHERE year =1984OR year =1996 CSE 303: Ashikur Rahman
NOT IN operatorartist album tracks company year
U2 The Unforgettable 10 Island 1984
U2 Rattle and Hum 17 Island 1988
U2 Achtung Boys 12 Island 1991
Underworld Second Toughest 8 Junior 1996
CDCollection
4040
SELECT *FROM CDCollectionWHERE year NOT IN (1984, 1996)
artist album tracks company year
U2 Rattle and Hum 17 Island 1988
U2 Achtung Boys 12 Island 1991
SELECT *FROM CDCollectionWHERE year < > 1984AND year < > 1996
CSE 303: Ashikur Rahman
Top Related