Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL : Query Language CS3431.

Similar presentations


Presentation on theme: "SQL : Query Language CS3431."— Presentation transcript:

1 SQL : Query Language CS3431

2 SELECT-FROM-WHERE  (sName=“Greg” AND address=“320 FL”) (Student)
FROM Student WHERE sName=“Greg” AND address=“320 FL” Student sNumber sName address professor 1 Dave 311FL MM 2 Greg 320FL 3 Matt ER sNumber sName address professor 2 Greg 320FL MM  (sName=“Greg” AND address=“320 FL”) (Student) CS3431

3 Select-From Query SELECT sNumber, sName FROM Student
address professor 1 Dave 320FL MM 2 Greg 3 Matt ER sNumber sName 1 Dave 2 Greg 3 Matt  (sNumber, sName) (Student) CS3431

4 Extended Projection SELECT sNumber || sName AS info FROM Student
WHERE address=“320 FL” Student info 1Dave 2Greg 3Matt sNumber sName address professor 1 Dave 320FL MM 2 Greg 3 Matt ER  (sNumber||sNameinfo) ( (address=“320 FL”) (Student)) CS3431

5 SQL and Relational Algebra
 L ( C (R)) becomes SELECT L FROM R WHERE C CS3431

6 Tuple Variables + Renaming
SELECT S1.sNumber AS num FROM Student S1 WHERE S1.sNumber >= 1; Student num 1 2 3 sNumber sName address professor 1 Dave 320FL MM 2 Greg 3 Matt ER  (S1.sNumbernum) ( (S1.sNumber >= 1) ( S1 (Student))) CS3431

7 String Operators Comparison Operators based on lexicographic ordering: =, <, >, <>, >=, <= Concatenation operator: || Pattern match: s LIKE p p denotes a pattern % : any sequence of 0 or more characters - : matches 1 character Use two consecutive quotes (‘) to represent ‘ CS3431

8 String Matching Example
SELECT s1.sNumber AS num FROM Student S1 WHERE s1.sName LIKE ‘Star_ _ _ _’ or S1.professor LIKE ‘Mad Cow’’s%’ ; CS3431

9 Set Operations in SQL (SELECT sName FROM Student) EXCEPT
Operators : UNION, INTERSECT, and EXCEPT (SELECT sName FROM Student) EXCEPT (SELECT sName FROM Student WHERE address=‘320 FL’) For set semantics, use UNION, INTERSECT, EXCEPT. For bag semantics, use UNION ALL, INTERSECT ALL, EXCEPT ALL CS3431

10 EXCEPT - Example (SELECT sName FROM Student) EXCEPT
WHERE address=‘320 FL’) Reminder : Set semantics ! Student sNumber sName address professor 1 Dave 320FL MM 2 Greg 3 Matt ER 4 300FL sName CS3431

11 Joins + SQL Multiple relations in FROM clause : SELECT *
FROM Student, Professor CS3431

12 Cross (Cartesian) Product
SELECT * FROM Student, Professor Student X Professor SELECT * FROM Student CROSS JOIN Professor; CS3431

13 Cross Product - Example
Student Professor sNumber sName address professor 1 Dave 320FL 2 Greg 3 Matt pNumber pName address 1 MM 141FL 2 ER 201FL sNumber sName address professor pNumber pName 1 Dave 320FL MM 141FL 2 ER 201FL Greg 3 Matt CS3431

14 Theta Join SELECT * FROM Student, Professor WHERE professor=pNumber;
Student ⋈(professor=pNumber) Professor SELECT * FROM Student JOIN Professor ON professor=pNumber; CS3431

15 Theta Join Example Student Professor
sNumber sName address professor 1 Dave 320FL 2 Greg 3 Matt pNumber pName address 1 MM 141FL 2 ER 201FL sNumber sName address professor pNumber pName 1 Dave 320FL MM 141FL 2 Greg 3 Matt ER 201FL Student ⋈(professor=pNumber) Professor CS3431

16 Natural Join SELECT * FROM Student , Professor
Reminder: Join columns must have same names in both relations SELECT * FROM Student , Professor WHERE Student.pnumber = Professor.pnumber ; Student ⋈ Professor SELECT * FROM Student NATURAL JOIN Professor CS3431

17 Natural Join - Example Student Professor Student ⋈ Professor sNumber
sName address pNumber 1 Dave 320FL 2 Greg 3 Matt pNumber pName address 1 MM 141FL 2 ER 201FL sNumber sName address pNumber pName 1 Dave 320FL MM 141FL 2 Greg 3 Matt ER 201FL Student ⋈ Professor CS3431

18 Outer Joins SELECT * FROM Student NATURAL FULL OUTER JOIN Professor
Student ⋈o Professor SELECT * FROM Student NATURAL LEFT OUTER JOIN Professor Student ⋈oL Professor SELECT * FROM Student NATURAL RIGHT OUTER JOIN Professor Student ⋈oR Professor CS3431

19 Outer Joins - Example Student Professor Student ⋈o Professor sNumber
sName address pNumber 1 Dave 320FL 2 Greg 3 Matt 4 Ben pNumber pName address 1 MM 141FL 2 ER 201FL 3 MW 168FL sNumber sName address pNumber pName 1 Dave 320FL MM 141FL 2 Greg 3 Matt ER 201FL 4 Ben Null MW 168FL CS3431 Student ⋈o Professor

20 Putting it together: Joins+more
SELECT sName FROM Student, Professor WHERE pName=‘MM’ AND pNumber=professor; (sName)(Student ⋈(pName=‘MM’ and pNumber=professor) Professor) CS3431

21 Joins - example SELECT sName FROM Student, Professor WHERE pName=‘MM’
sNumber sName address professor 1 Dave 320FL 2 Greg 3 Matt pNumber pName address 1 MM 141FL 2 ER 201FL SELECT sName FROM Student, Professor WHERE pName=‘MM’ AND pNumber=professor; sName Dave Greg CS3431


Download ppt "SQL : Query Language CS3431."

Similar presentations


Ads by Google