Week7 Chapter 8 - moreSQLexamples
Week7 Chapter 8 - moreSQLexamples
College of Engineering
School of Electrical and Electronic Engineering
Chapter 8: Adv. SQL & More SQL Examples 0
SQL multi-Table Queries
(Chapter 7/8 )
Persons table
LastName FirstName Address City
Hansen Ola Timoteivn 10 Sandnes
Svendson Tove Borgvn 23 Sandnes
Pettersen Kari Storgt 20 Stavanger
SELECT LastName
FROM Persons;
Result:
LastName
Hansen
Svendson
Pettersen
Query Result
LastName FirstName
Hansen Ola
Svendson Tove
Pettersen Kari
Query Results
Orders
Company
Sega
Company OrderNumber
W3Schools
Sega 3412
Trio
W3Schools 2312
W3Schools
Trio 4678
W3Schools 6798
Query Results
Address
LastName FirstName City Year
Hansen Ola Timoteivn 10 Sandnes 1951
Svendson Tove Borgvn 23 Sandnes 1978
Svendson Stale Kaivn 18 Sandnes 1980
Chapter 8: Adv. SQL & More SQL Examples 9
Insert a New Row
Persons table
LastName FirstName Address City
Pettersen Kari Storgt 20 Stavanger
Persons table
LastName FirstName Address City
We want to change the address and add the name of the city:
UPDATE Person
SET Address = 'Stien 12', City = 'Stavanger'
WHERE LastName = 'Rasmussen' ;
UPDATE PRODUCT
SET price = price/2
WHERE Product.name IN
(SELECT Distinct product
FROM Purchase
WHERE Date =‘Oct. 25, 1999’);
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
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 city
FROM Company
WHERE name IN
(SELECT maker
FROM Purchase, Product
WHERE pname=product
AND buyer = ‘Joe Blow‘);
Chapter 8: Adv. SQL & More SQL Examples 19
Subqueries
Is it equivalent to this ?
SELECT city
FROM Company, Product, Purchase
WHERE name= maker
AND pname = product
AND buyer = ‘Joe Blow’;
Beware of duplicates !
Find products that have a higher price than any product in the
“Électronics” category: (at least one value in the set)
SELECT *
FROM Product
WHERE price > ANY (SELECT price FROM Product
WHERE category = 'Electronics');
Chapter 8: Adv. SQL & More SQL Examples 22
Operators : ALL, ANY, EXISTS
The subquery inside EXISTS checks if there is at least one row in the Product table where the maker is 'ABC Inc.'. If
such a row exists, the condition is true, and the product details are included in the result set.
SELECT 1 = at least one row exists – but no need to retrieve the row
But if you want to get the total number of categories, you should do:
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
2. Find cities of all companies with all their products having prices < 100
SELECT DISTINCT city
FROM Company
WHERE cname NOT IN (SELECT DISTINCT company
FROM Product
WHERE price >= 100);
Wrote(login, url)
Mentions(url, word)
expensive product
SELECT sname, x.pname
FROM Store, Product x
WHERE Store.sid = x.sid
AND x.price >=
ALL (SELECT y.price
FROM Product y
WHERE Store.sid = y.sid )
GROUP BY sname;
Unexpected behavior:
SELECT *
FROM Person
WHERE age < 25 OR age >= 25;
SELECT *
FROM Person
WHERE age < 25 OR age >= 25 OR age IS NULL ;
(a) List all courses (and course details) for which Professor John Tan is
qualified.
(b) Find the IDs of any lecturers who are qualified to teach ISM3113
but not qualified to teach ISM3114.
(c) Find out how many students are enrolled in course ISM3113 during
semester I-98.
(d) Find out which students were not enrolled in any courses during
semester I-98. Show their IDs and names.