Module 3
Module 3
Table name
Product
PName Price Category Manufacturer
Tuples or rows
Tables Explained
SELECT <attributes>
FROM <one or more relations>
WHERE <conditions>
Simple SQL Query
Output
Schema
Details
● Case insensitive:
○ Same: SELECT Select select
○ Same: Product product
○ Different: ‘Seattle’ ‘seattle’
● Constants:
○ 'abc’ - yes
○ "abc” - no
The LIKE operator
SELECT *
FROM Product
WHERE PName LIKE ‘%gizmo%’
Gadgets
SELECT category Gadgets
FROM Product
Photography
Household
Ordering the Results
?
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
Canon 65 Japan
Hitachi 15 Japan
Product
PName Price Category Manufacturer
Foreign
Gizmo $19.99 Gadgets GizmoWorks key
Canon 65 Japan
Find all products under $200 manufactured in
Japan; return their names and prices. Hitachi 15 Japan
Product
SELECT PName, Price
PName Price Category Manufacturer
FROM Product, Company
WHERE Manufacturer=CName Gizmo $19.99 Gadgets GizmoWorks
AND Country=‘Japan’ Powergizmo $29.99 Gadgets GizmoWorks
AND Price <= 200
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
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
Product
Unexpected duplicates
A Subtlety about Joins
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 ?
Join
Aggregation
SELECT avg(price) SELECT count(*)
FROM Product FROM Product
WHERE maker=“Toyota” WHERE year > 1995
We probably want:
Bagel 10/21 1 20
Banana 10/10 1 10
Bagel 10/21 1 20
Banana 10/10 1 10
SELECT *
FROM Person
WHERE age < 25 OR age >= 25 OR age IS NULL
Example:
UPDATE PRODUCT
SET price = price/2
WHERE Product.name IN
(SELECT product
FROM Purchase
WHERE Date =‘Oct, 25, 1999’);
Background: Semi Structured Data
● XML and JSON are two standard, textual data formats for
representing arbitrary data
○ XML stands for “eXtensible Markup Language”
○ JSON stands for “JavaScript Object Notation”
● Both are commonly used in practice. XML came first
● JSON, which uses JavaScript syntax, became popular for
representing data in web applications and services
○ If you’re using JavaScript, JSON is an obvious choice
Background: Semi Structured Data
<account>
<account_number>A-101</account_number> …
</account>
○Suggestion: use attributes for identifiers of elements, and use
subelements for contents
More on XML Syntax
Elements without subelements or text content can be abbreviated
by ending the start tag with a /> and deleting the end tag
○ <account number=“A-101” branch=“Perryridge” balance=“200 />
To store string data that may contain tags, without the tags being
interpreted as subelements, use CDATA as below
○ <![CDATA[<account> … </account>]]>
Here, <account> and </account> are treated as just strings
CDATA stands for “character data”, text that will NOT be parsed by a parser
JSON Data – A name and a value
• A name/value pair consists of a field name (in double quotes), followed by a
colon, followed by a value
• Unordered sets of name/value pairs
• Begins with { (left brace)
• Ends with } (right brace)
• Each name is followed by : (colon)
• Name/value pairs are separated by , (comma)
{
"employee_id": 1234567,
"name": "Jeff Fox",
"hire_date": "1/1/2013",
"location": "Norwalk, CT",
"consultant": false
}
JSON Data – A name and a value
• In JSON, values must be one of the following data types:
• a string
• a number
• an object (JSON object)
• an array
• a boolean
• null
{
"employee_id": 1234567,
"name": "Jeff Fox",
"hire_date": "1/1/2013",
"location": "Norwalk, CT",
"consultant": false
}
JSON Data – A name and a value
• Strings in JSON must be written in double quotes.
{ "name":"John" }
{"employees":[
{ "firstName":"John", "lastName":"Doe" },
{ "firstName":"Anna", "lastName":"Smith" },
{ "firstName":"Peter", "lastName":"Jones" }
]}
XML vs. JSON
•JSON is Like XML Because The biggest difference is: XML
• Both JSON and XML are "self describing" has to be parsed with XML parser. JSON
(human readable) can be parsed by standard JavaScript
• Both JSON and XML are hierarchical function.
(values within values)
• Both JSON and XML can be parsed and
used by lots of programming languages