You are on page 1of 17

Basic SQL

Lecture 2

1

Outline
• Data in SQL
• Simple Queries in SQL
• Queries with more than one relation

Reading:
• Chapter 3, “Simple Queries” from SQL for Web
Nerds, by Philip Greenspun
http://philip.greenspun.com/sql/

2

1

.a.a. SQL3) • Vendors support various subsets of these • What we discuss is common to all of them 3 SQL • Data Definition Language (DDL) – Create/alter/delete tables and their attributes – Following lectures.. SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: • ANSI SQL • SQL92 (a. SQL2) • SQL99 (a.k. • Data Manipulation Language (DML) – Query one or more tables – discussed next ! – Insert/delete/modify tuples in tables • Transact-SQL – Idea: package a sequence of SQL statements  server – Won’t discuss in class 4 2 .k.

a.k. All are simple 6 3 .. FLOAT -.variable length • Numbers: – BIGINT. Tables built from atomic types 5 Data Types in SQL • Characters: – CHAR(20) -. Data in SQL 1.differ in precision – MONEY • Times and dates: – DATE – DATETIME -. SMALLINT. Atomic types.SQL Server • Others.a. data types 2.fixed length – VARCHAR(40) -. INT. TINYINT – REAL..

Table name Attribute names Tables in SQL Product PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29. no last(). • No nested tables.99 Photography Canon MultiTouch $203. no next().99 Household Hitachi Tuples or rows 7 Table Details • A tuple = a record – Restriction: all attributes are of atomic type • A table = a set of tuples – Like a list… – …but it is unordered: no first().99 Gadgets GizmoWorks SingleTouch $149. only flat tables are allowed! – We will see later how to decompose complex structures into multiple flat tables 8 4 .

Price. joined) WHERE conditions (selections) 10 5 . Price. Category. Category. Manfacturer) 9 SQL Query Basic form: SELECT attributes FROM relations (possibly multiple. we underline a key Product(PName. Table Details • The schema of a table is the table name and its attributes: Product(PName. Manfacturer) • A key is an attribute whose values are unique.

99 Gadgets GizmoWorks Powergizmo $29.99 Photography Canon MultiTouch $203.99 Canon “projection” MultiTouch $203. Simple SQL Query Product PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks SingleTouch $149.99 Gadgets GizmoWorks SingleTouch $149.99 Gadgets GizmoWorks “selection” 11 Simple SQL Query Product PName Price Category Manufacturer Gizmo $19.99 Household Hitachi SELECT * FROM Product WHERE category=‘Gadgets’ PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29.99 Gadgets GizmoWorks Powergizmo $29. Price.99 Household Hitachi SELECT PName.99 Photography Canon MultiTouch $203.99 Hitachi 12 6 . Manufacturer FROM Product WHERE Price > 100 PName Price Manufacturer “selection” and SingleTouch $149.

• Pattern matching on strings: s LIKE p (next) 14 7 . etc – For number. Manufacturer FROM Product WHERE Price > 100 Answer(PName. Price. Category. Manfacturer) Output Schema 13 Selections What goes in the WHERE clause: • x = y. they have the usual meanings – For CHAR and VARCHAR: lexicographic ordering • Expected conversion between CHAR and VARCHAR – For dates and times. A Notation for SQL Queries Input Schema Product(PName. Price. x <= y. what you expect. Manfacturer) SELECT PName.. x < y. Price..

Category. Manufacturer) Find all products whose name mentions ‘gizmo’: SELECT * FROM Products WHERE PName LIKE ‘%gizmo%’ 15 Eliminating Duplicates Category Gadgets SELECT category Gadgets FROM Product Photography Household Compare to: Category SELECT DISTINCT category Gadgets FROM Product Photography Household 16 8 . Price. The LIKE operator • s LIKE p: pattern matching on strings • p may contain two special symbols: – % = any sequence of characters – _ = any single character Product(Name.

99 Gadgets GizmoWorks SingleTouch $149.99 Gadgets GizmoWorks Powergizmo $29.99 Photography Canon MultiTouch $203. etc. manufacturer FROM Product WHERE category=‘gizmo’ AND price > 50 ORDER BY price. Ordering the Results SELECT pname. pname Ordering is ascending. unless you specify the DESC keyword. 17 Ordering the Results SELECT Category FROM Product ORDER BY PName PName Price Category Manufacturer ? Gizmo $19. Ties are broken by the second attribute on the ORDER BY list.99 Household Hitachi 18 9 . price.

Ordering the Results Category SELECT DISTINCT category Gadgets FROM Product Household ORDER BY category Photography Compare to: ? SELECT DISTINCT category FROM Product ORDER BY PName 19 Joins in SQL • Connect two or more tables: Product PName Price Category Manufacturer Gizmo $19.99 Photography Canon MultiTouch $203.99 Gadgets GizmoWorks SingleTouch $149.99 Household Hitachi Company CName StockPrice Country GizmoWorks 25 USA What is the Connection Canon 65 Japan between them ? Hitachi 15 Japan 20 10 .99 Gadgets GizmoWorks Powergizmo $29.

stockPrice. return their names and prices. Joins Product (pname. Price FROM Product. category. country) Find all products under $200 manufactured in Japan. manufacturer) Company (cname. price. Join between Product SELECT PName.99 Gadgets GizmoW orks GizmoW orks 25 USA Powergizmo $29.99 22 11 . Company WHERE Manufacturer=CName AND Country=‘Japan’ AND Price <= 200 PName Price SingleTouch $149. Company WHERE Manufacturer=CName AND Country=‘Japan’ AND Price <= 200 21 Joins in SQL Product Company PName Price C ategory Manufacturer C name StockPrice C ountry Gizmo $19. Price and Company FROM Product.99 Gadgets GizmoW orks C anon 65 Jap an SingleTouch $149.99 Household Hitachi SELECT PName.99 Photograp hy C anon Hitachi 15 Jap an MultiTouch $203.

99 Photograp hy C anon Hitachi 15 Jap an MultiTouch $203.99 Gadgets GizmoW orks C anon 65 Jap an SingleTouch $149. SELECT Country FROM Product. Company WHERE Manufacturer=CName AND Category=‘Gadgets’ 23 Joins in SQL Product Company PName Price C ategory Manufacturer C name StockPrice C ountry Gizmo $19.99 Household Hitachi SELECT Country FROM Product. price. category. Company WHERE Manufacturer=CName AND Category=‘Gadgets’ C ountry What is ?? the problem ? ?? What’s the solution ? 24 12 . country) Find all countries that manufacture some product in the ‘Gadgets’ category. stockPrice. manufacturer) Company (cname. Joins Product (pname.99 Gadgets GizmoW orks GizmoW orks 25 USA Powergizmo $29.

category. and the names of the stores they bought such product from SELECT DISTINCT persname. Company.worksfor = Company. product) Person(persname. Purchase. Company WHERE Person. price.address FROM Person.cname 26 13 . Joins Product (pname. Product WHERE persname=buyer AND product = pname AND city=‘Seattle’ AND category=‘Gadgets’ 25 Disambiguating Attributes • Sometimes two relations have the same attr: Person(pname. phoneNumber.pname. store. seller. Company WHERE worksfor = cname SELECT DISTINCT Person. address) Which SELECT DISTINCT pname. manufacturer) Purchase (buyer. city) Find names of people living in Seattle that bought some product in the ‘Gadgets’ category. address address ? FROM Person. store FROM Person. address. worksfor) Company(cname.

phoneNumber. Tuple Variables Product (pname. price.price > 100 Doesn’t work when Product occurs more than once: In that case the user needs to define variables explicitly.name FROM Product AS Product WHERE Product. 28 14 .product = y. product) Person(persname. seller. store. city) Find all stores that sold at least one product that the store ‘BestBuy’ also sold: SELECT DISTINCT x. Purchase AS y WHERE x. price. category.store FROM Purchase AS x. manufacturer) Purchase (buyer. category.store = ‘BestBuy’ Answer (store) 27 General rule: Tuple Variables tuple variables introduced automatically by the system: Product ( name. manufacturer) SELECT name FROM Product WHERE price > 100 Becomes: SELECT Product.product AND y.

…. for xn in Rn do if Conditions then Answer = Answer ∪ {(x1. a2.99 29 Meaning (Semantics) of SQL Queries SELECT a1.99 renaming MultiTouch $203. …. Rn AS xn WHERE Conditions 1.99 Gadgets GizmoWorks Powergizmo $29.….99 Gadgets GizmoWorks SingleTouch $149. Nested loops: Answer = {} for x1 in R1 do for x2 in R2 do ….99 Household Hitachi SELECT Pname AS prodName.. ak FROM R1 AS x1. Renaming Columns Product PName Price Category Manufacturer Gizmo $19. Price AS askPrice FROM Product WHERE Price > 100 prodName askPrice Query with SingleTouch $149.xk)} return Answer 30 15 .99 Photography Canon MultiTouch $203. R2 AS x2.

A=T.xk)} return Answer Doesn’t impose any order ! 31 First Unintuitive SQLism SELECT R. S.A=S. …. R2 AS x2.…. Meaning (Semantics) of SQL Queries SELECT a1. T WHERE R.A OR R. a2. Parallel assignment Answer = {} for all assignments x1 in R1. …. ak FROM R1 AS x1. Rn AS xn WHERE Conditions 2.A FROM R. xn in Rn do if Conditions then Answer = Answer ∪ {(x1. ….A Looking for R ! (S ! T) But what happens if T is empty? 32 16 .

SELECT Question FROM Student WHERE Course=“IIS” 33 17 .