SQL Select and Relational Algebra

27
SQL Select and Relational Algebra

description

SQL Select and Relational Algebra. SELECT. SELECT fields FROM tables WHERE conditions GROUP BY fields HAVING conditions ORDER BY fields. SELECT * FROM expeople. SELECT name (Projection) FROM expeople. SELECT name, age FROM expeople. - PowerPoint PPT Presentation

Transcript of SQL Select and Relational Algebra

Page 1: SQL Select and Relational Algebra

SQL Select and Relational Algebra

Page 2: SQL Select and Relational Algebra

SELECT

SELECT fields

FROM tables

WHERE conditions

GROUP BY fields

HAVING conditions

ORDER BY fields

Page 3: SQL Select and Relational Algebra

SELECT *

FROM expeople

Page 4: SQL Select and Relational Algebra

SELECT name (Projection)

FROM expeople

Page 5: SQL Select and Relational Algebra

SELECT name, age

FROM expeople

Page 6: SQL Select and Relational Algebra

SELECT DISTINCT name

FROM expeople

Page 7: SQL Select and Relational Algebra

SELECT sex AS gender (renaming)

FROM expeople

Page 8: SQL Select and Relational Algebra

SELECT *

FROM expeople

WHERE age = 17 (selection)

Page 9: SQL Select and Relational Algebra

= Equal

<> Not Equal

> Greater Than

< Less Than

>= Greater Than or Equal

<= Less Than or Equal

IS NULL

IS NOT NULL

Page 10: SQL Select and Relational Algebra

LIKE

NOT LIKE

%

_

New% will match Newark, New York, etc.

_ow will match Cow, Bow, Now, etc.

AND

OR

Page 11: SQL Select and Relational Algebra

SELECT *

FROM expeople

WHERE age = 17 OR name = ‘JoJo’

Page 12: SQL Select and Relational Algebra

SELECT *

FROM expeople

ORDER BY expeople.id desc

(or can use asc)

Page 13: SQL Select and Relational Algebra

SELECT sum(nums)

FROM exnum

Page 14: SQL Select and Relational Algebra

count() Total number of items

sum() Sum of the items

avg() Average of the items

min() Smallest of the items

max() Biggest of the items

Page 15: SQL Select and Relational Algebra

SELECT name, sum(number)

FROM exnum

GROUP BY name

Page 16: SQL Select and Relational Algebra

SELECT sum(number)

FROM exnum

GROUP BY name

HAVING sum(nums) > 30

Page 17: SQL Select and Relational Algebra

SELECT expeople.*, exjobs.*

FROM expeople, exjobs

(cartesian product)

Page 18: SQL Select and Relational Algebra
Page 19: SQL Select and Relational Algebra

SELECT expeople.*, exjobs.*

FROM expeople, exjobs

WHERE expeople.id = exjobs.jid(equi-joins)

Page 20: SQL Select and Relational Algebra
Page 21: SQL Select and Relational Algebra

SELECT *

FROM expeople

WHERE name = ‘JoJo’

UNION (union)

SELECT *

FROM expeople

WHERE age = ‘17’

Page 22: SQL Select and Relational Algebra
Page 23: SQL Select and Relational Algebra

SELECT *

FROM expeople

WHERE name = ‘Mike’

INTERSECT(intersection)

SELECT *

FROM expeople

WHERE age = ‘17’

Page 24: SQL Select and Relational Algebra
Page 25: SQL Select and Relational Algebra

SELECT *

FROM expeople

WHERE name = ‘Mike’

MINUS (set-difference)

SELECT *

FROM expeople

WHERE age = ‘17’

Page 26: SQL Select and Relational Algebra
Page 27: SQL Select and Relational Algebra

DONE