+ All Categories
Home > Documents > Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Date post: 19-Dec-2015
Category:
View: 216 times
Download: 1 times
Share this document with a friend
39
Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton
Transcript
Page 1: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Structured Query Language – Continued

Rose-Hulman Institute of Technology

Curt Clifton

Page 2: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

The Story Thus Far SELECT … FROM … WHERE SELECT * … SELECT Foo AS Bar … SELECT expression … SELECT … FROM … WHERE … LIKE … SELECT … FROM Foo, Bar … SELECT … FROM Foo f1, Foo f2 …

Page 3: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Next Up: Subqueries As values As relations

Page 4: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Subqueries as Values Only allowed when subquery evaluates to

single value Run-time error otherwise

Example: Find the restaurants that sell Slice for the price the Joe's charges for Pepsi

Page 5: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Subqueries as Relations – in FROM SELECT Likes.customer,

mix.soda1, mix.soda2FROM Likes,

(SELECT s1.name AS soda1, s2.name AS soda2

FROM Soda s1, Soda s2WHERE s1.manf = s2.manf

AND s1.name < s2.name)AS mix

WHERE Likes.soda = mix.soda1

Page 6: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Subqueries as Relations – in WHERE value IN relation Evaluates to true if relation contains value SELECT *

FROM SodaWHERE name IN (SELECT soda

FROM LikesWHERE customer = 'Fred')

Page 7: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Subqueries as Relations – in WHERE EXISTS relation Evaluates to true if relation is non-empty Find every soda where its manufacturer does not

make anything else SELECT name

FROM Soda s1WHERE NOT EXISTS (

SELECT *FROM Soda s2WHERE s2.manf = s1.manf

AND s2.name <> s1.name)

Page 8: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Subqueries as Relations – in WHERE ANY

x comp ANY(relation) comp can be <, >, =, <>, >=, <= Evaluates to true if comparison holds for any tuple in

relation ALL

x comp ALL(relation) comp can be <, >, =, <>, >=, <= Evaluates to true if comparison holds for every tuple in

relation

Page 9: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Example SELECT soda

FROM SellsWHERE price >= ALL(SELECT price

FROM Sells)

Page 10: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Subqueries Summary As values As relations in FROM clause As relations in WHERE clause

IN EXISTS ANY ALL

Page 11: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Combining Relations Union, Intersection, Difference Joins

Page 12: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Union, Intersection, and Difference Union

(subquery) UNION (subquery ) Intersection

(subquery) INTERSECT (subquery) Difference

(subquery) EXCEPT (subquery)

Page 13: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

SQL Goofiness – Sets vs. Bags Bags by default

SELECT Sets by default

UNION INTERSECT EXCEPT

Overriding defaults SELECT DISTINCT

UNION ALL Cannot override Cannot override

Page 14: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Example Find all the different prices charged for sodas

Page 15: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Example Find all the different prices charged for sodas

SELECT DISTINCT price FROM Sells

Page 16: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Theta Join Syntax: SELECT …

FROM table1 JOIN table2 ON condition

Page 17: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Example Give name and phone number of all

customers that frequent Joe's Sushi

Page 18: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Example SELECT name, phone

FROM Customer JOIN FrequentsON name = customer

WHERE rest = 'Joe''s Sushi' Compare:

SELECT name, phoneFROM Customer, FrequentsWHERE name = customer

AND rest = 'Joe''s Sushi'

Page 19: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Natural Join Not in SQL Server But some DBMS allow:

SELECT …FROM table1 NATURAL JOIN

table2

Page 20: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Outer Joins Recall: solution to dangling tuple problem Make sure every tuple shows up, even if no

“mate”, by inserting nulls if needed Three basic forms:

SELECT … FROM t1 LEFT OUTER JOIN t2 SELECT … FROM t1 RIGHT OUTER JOIN t2 SELECT … FROM t1 OUTER JOIN t2

Page 21: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Cross Product Possible, though less common SELECT …

FROM table1 CROSS JOIN table2 Or just write:

SELECT …FROM table1, table2

Page 22: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Reporting Aggregation Grouping

Page 23: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Aggregation Calculations over rows Example:

SELECT AVG(price)FROM Sells

WHERE soda = 'Pepsi'

Other aggregations: SUM AVG COUNT, COUNT(*) MIN, MAX

“Let me explain. No, would take too long. Let me sum up.”

Page 24: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Aggregation and Duplicates Can use DISTINCT inside an aggregation Example – Find the number of different prices

charged for Pepsi

Page 25: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Aggregation and Duplicates Can use DISTINCT inside an aggregation Example – Find the number of different prices

charged for Pepsi SELECT COUNT(DISTINCT price)

FROM SellsWHERE soda = 'Pepsi'

Page 26: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Grouping For aggregating

subsections of result SELECT …

FROM …WHERE …GROUP BY

attr,…

Page 27: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Example: Grouping Find the average price for each soda

Page 28: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Example: Grouping Find the average price for each soda SELECT soda, AVG(price)

FROM SellsGROUP BY soda

Page 29: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Having Like a WHERE clause for groups SELECT …

FROM …WHERE … -- filter rowsGROUP BY … -- group rowsHAVING … -- filter groups

Page 30: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Example: Having Find the average price of those sodas that are

served by at least three restaurants

Page 31: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Example: Having Find the average price of those sodas that are

served by at least three restaurants SELECT soda, AVG(price)

FROM SellsGROUP BY sodaHAVING COUNT(rest) >= 3

Page 32: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Modifying the Database Insert Delete Update

Page 33: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Insertion Single tuple, quick and dirty:

INSERT INTO tableVALUES (value1, …)

Single tuple, more robust: INSERT INTO table(attr1, …)

VALUES (value1, …) Many tuples:

INSERT INTO table (subquery)

Page 34: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Deletion Single tuple:

DELETE FROM table WHERE condition All tuples (zoinks!):

DELETE FROM table

Page 35: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Updates Syntax:

UPDATE tableSET attr1 = expr1, … -- attributes, new valuesWHERE condition -- rows to change

Page 36: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Example Change Fred's phone number to 555-1212

Page 37: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Example Change Fred's phone number to 555-1212 UPDATE Customer

SET phone = '555-1212'WHERE name = 'Fred'

Page 38: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Example Raise all prices by 10%

Page 39: Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.

Example Raise all prices by 10% UPDATE Sells

SET price = (price * 1.10)


Recommended