Structured Query Language Continued Rose-Hulman Institute of - - PowerPoint PPT Presentation

structured query language continued
SMART_READER_LITE
LIVE PREVIEW

Structured Query Language Continued Rose-Hulman Institute of - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

Structured Query Language – Continued

Rose-Hulman Institute of Technology Curt Clifton

slide-2
SLIDE 2

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 …

slide-3
SLIDE 3

Next Up: Subqueries

 As values  As relations

slide-4
SLIDE 4

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

slide-5
SLIDE 5

Subqueries as Relations – in FROM

 SELECT Likes.customer,

mix.soda1, mix.soda2 FROM Likes, (SELECT s1.name AS soda1, s2.name AS soda2 FROM Soda s1, Soda s2 WHERE s1.manf = s2.manf AND s1.name < s2.name) AS mix WHERE Likes.soda = mix.soda1

slide-6
SLIDE 6

Subqueries as Relations – in WHERE

 value IN relation  Evaluates to true if relation contains value  SELECT *

FROM Soda WHERE name IN (SELECT soda FROM Likes WHERE customer = 'Fred')

slide-7
SLIDE 7

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 s1 WHERE NOT EXISTS ( SELECT * FROM Soda s2 WHERE s2.manf = s1.manf AND s2.name <> s1.name)

slide-8
SLIDE 8

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

slide-9
SLIDE 9

Example

 SELECT soda

FROM Sells WHERE price >= ALL(SELECT price FROM Sells)

slide-10
SLIDE 10

Subqueries Summary

 As values  As relations in FROM clause  As relations in WHERE clause

 IN  EXISTS  ANY  ALL

slide-11
SLIDE 11

Combining Relations

 Union, Intersection, Difference  Joins

slide-12
SLIDE 12

Union, Intersection, and Difference

 Union

 (subquery) UNION (subquery )

 Intersection

 (subquery) INTERSECT (subquery)

 Difference

 (subquery) EXCEPT (subquery)

slide-13
SLIDE 13

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

slide-14
SLIDE 14

Example

 Find all the different prices charged for sodas

slide-15
SLIDE 15

Example

 Find all the different prices charged for sodas

 SELECT DISTINCT price

FROM Sells

slide-16
SLIDE 16

Theta Join

 Syntax:  SELECT …

FROM table1 JOIN table2 ON condition …

slide-17
SLIDE 17

Example

 Give name and phone number of all

customers that frequent Joe's Sushi

slide-18
SLIDE 18

Example

 SELECT name, phone

FROM Customer JOIN Frequents ON name = customer WHERE rest = 'Joe''s Sushi'

 Compare:

SELECT name, phone FROM Customer, Frequents WHERE name = customer AND rest = 'Joe''s Sushi'

slide-19
SLIDE 19

Natural Join

 Not in SQL Server  But some DBMS allow:

 SELECT …

FROM table1 NATURAL JOIN table2

slide-20
SLIDE 20

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

slide-21
SLIDE 21

Cross Product

 Possible, though less common  SELECT …

FROM table1 CROSS JOIN table2

 Or just write:

 SELECT …

FROM table1, table2

slide-22
SLIDE 22

Reporting

 Aggregation  Grouping

slide-23
SLIDE 23

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.”

slide-24
SLIDE 24

Aggregation and Duplicates

 Can use DISTINCT inside an aggregation  Example – Find the number of different prices

charged for Pepsi

slide-25
SLIDE 25

Aggregation and Duplicates

 Can use DISTINCT inside an aggregation  Example – Find the number of different prices

charged for Pepsi

 SELECT COUNT(DISTINCT price)

FROM Sells WHERE soda = 'Pepsi'

slide-26
SLIDE 26

Grouping

 For aggregating

subsections of result

 SELECT …

FROM … WHERE … GROUP BY attr,…

slide-27
SLIDE 27

Example: Grouping

 Find the average price for each soda

slide-28
SLIDE 28

Example: Grouping

 Find the average price for each soda  SELECT soda, AVG(price)

FROM Sells GROUP BY soda

slide-29
SLIDE 29

Having

 Like a WHERE clause for groups  SELECT …

FROM … WHERE …

  • - filter rows

GROUP BY …

  • - group rows

HAVING …

  • - filter groups
slide-30
SLIDE 30

Example: Having

 Find the average price of those sodas that are

served by at least three restaurants

slide-31
SLIDE 31

Example: Having

 Find the average price of those sodas that are

served by at least three restaurants

 SELECT soda, AVG(price)

FROM Sells GROUP BY soda HAVING COUNT(rest) >= 3

slide-32
SLIDE 32

Modifying the Database

 Insert  Delete  Update

slide-33
SLIDE 33

Insertion

 Single tuple, quick and dirty:

 INSERT INTO table

VALUES (value1, …)

 Single tuple, more robust:

 INSERT INTO table(attr1, …)

VALUES (value1, …)

 Many tuples:

 INSERT INTO table (subquery)

slide-34
SLIDE 34

Deletion

 Single tuple:

 DELETE FROM table WHERE condition

 All tuples (zoinks!):

 DELETE FROM table

slide-35
SLIDE 35

Updates

 Syntax:

 UPDATE table

SET attr1 = expr1, …

  • - attributes, new values

WHERE condition

  • - rows to change
slide-36
SLIDE 36

Example

 Change Fred's phone number to 555-1212

slide-37
SLIDE 37

Example

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

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

slide-38
SLIDE 38

Example

 Raise all prices by 10%

slide-39
SLIDE 39

Example

 Raise all prices by 10%  UPDATE Sells

SET price = (price * 1.10)