View
63
Download
9
Category
Preview:
DESCRIPTION
SQL: Structured Query Language. Part II. Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu. More on SQL SELECT. Cartesian Product in SQL. In Relation Algebra: R x S In SQL, add R and S to FROM clause No WHERE condition that links R and S. SELECT * FROM Student, Professor;. - PowerPoint PPT Presentation
Citation preview
Cartesian Product in SQL
SELECT *
FROM Student, Professor;
• In Relation Algebra: R x S
• In SQL, add R and S to FROM clause• No WHERE condition that links R and S
SELECT sName, pNumber
FROM Student, Professor;
3
Cross Product - Example
sNumber sName address professor
1 Dave 320FL 1
2 Greg 320FL 1
3 Matt 320FL 2
Student
pNumber pName addr
1 MM 141FL
2 ER 201FL
Professor
sNumber sName address professor pNumber pName addr
1 Dave 320FL 1 1 MM 141FL
1 Dave 320FL 1 2 ER 201FL
2 Greg 320FL 1 1 MM 141FL
2 Greg 320FL 1 2 ER 201FL
3 Matt 320FL 2 1 MM 141FL
3 Matt 320FL 2 2 ER 201FL
SELECT *
FROM Student, Professor;
4
Theta Join in SQL
SELECT *
FROM Student, Professor
WHERE Student.pNum = Professor.Number;
• In Relation Algebra: R ⋈C S
• In SQL, add R and S to FROM clause• WHERE condition that links R and S with the join condition C
Join condition5
Theta Join Example
sNumber sName pName
1 Dave MM
2 Greg MM
3 Matt ER
sNumber sName address profNum
1 Dave 320FL 1
2 Greg 320FL 1
3 Matt 320FL 2
Student
pNumber pName address
1 MM 141FL
2 ER 201FL
Professor
sNumber,sName,pName(Student ⋈(profNum=pNumber) Professor)
SELECT sNumber, sName, pName
FROM Student, Professor
WHERE profNum = pNumber;
6
Theta Join Example
sNumber sName address profNum
1 Dave 320FL 1
2 Greg 320FL 1
3 Matt 320FL 2
Student
pNumber pName address
1 MM 141FL
2 ER 201FL
Professor
7
If column names are the same use relationName.attrName
SELECT sName, pName, S.address
FROM Student S, Professor P
WHERE S.address = P.address;
sName,pName,S.address(ρS(Student) ⋈(S.address=P.address) ρP(Professor))
Natural Join
SELECT *
FROM Student , Professor
WHERE Student.pnumber = Professor.pnumber ;
Student Professor⋈
Reminder: Join columns must have same names in both relations (R S)⋈
SELECT *FROM Student NATURAL JOIN Professor;
Explicitly add the equality join condition
8
Difference between the two Queries below
SELECT *
FROM Student , Professor
WHERE Student.pnumber = Professor.pnumber ;
Student Professor⋈
SELECT *FROM Student NATURAL JOIN Professor;
Explicitly add the equality join condition
9
Common columns will appear once
Common columns will appear twice
Natural Join - Example
sNumber sName address pNumber
1 Dave 320FL 1
2 Greg 320FL 1
3 Matt 320FL 2
StudentpNumber pName addr
1 MM 141FL
2 ER 201FL
Professor
sNumber sName address pNumber pName addr
1 Dave 320FL 1 MM 141FL
2 Greg 320FL 1 MM 141FL
3 Matt 320FL 2 ER 201FL
Student Professor⋈10
Must be the same name
SELECT *
FROM Student natural join Professor;
Example Queries
SELECT *
FROM loan
WHERE amount > 1200 ;
SELECT L.loan_number
FROM loan L
WHERE L.amount > 1200 ;
11
Example Queries
SELECT customer_name
FROM borrower B, loan L
WHERE B.loan_number = L.loan_number
AND L.branch_name = “Perryridge”;
DBMS is smart enough !!!(Select first, then joins)
13
Sorting: ORDER BY clause
New optional clause that you can add to the SELECT statement called “ORDER BY”
Allows sorting the returned records according to one or more fields
SELECT *
FROM Student
WHERE sNumber >= 1
ORDER BY pNumber, sName;
SELECT *
FROM Student
WHERE sNumber >= 1
ORDER BY pNumber ASC, sName DESC;
Default is ascending order
14
-Order first based on the pNumber (ascending)
-If many records exist with the same pNumber - order them based on sName (descending)
Sorting: ORDER BY clause
sNumber sName address pNumber
1 Dave 320FL 1
2 Greg 320FL 1
3 Matt 320FL 2
Student
SELECT *
FROM Student
WHERE sNumber >= 1
ORDER BY pNumber,
sName DESC;
sNumber sName address pNumber
2 Greg 320FL 1
1 Dave 320FL 1
3 Matt 320FL 2
(pNumber, sName DESC) ( (sNumber >= 1) (Student))
15
Duplicate Elimination in SQL
New optional keyword “DISTINCT”
Added in the SELECT clause
SELECT DISTINCT …
FROM …
…Eliminate any duplicates from the answer
16
Duplicate Elimination: Example
SELECT DISTINCT sName, address
FROM Student;
(sName,address(Student)) ( (address) ( (sNumber > 1) (Student)))
sNumber sName address professor
1 Dave 320FL MM
2 Greg 320FL MM
3 Matt 320FL ER
Student
address
320FL
SELECT DISTINCT address
FROM Student
WHERE sNumber > 1;
sName address
Dave 320FL
Greg 320FL
Matt 320FL
17
Always Remember…. Only SELECT and FROM clauses are mandatory
All the others are optional
You can mix and match the optional ones But if you add a clause, then keep it in its order
SELECT address
FROM Student
ORDER BY sNumber
WHERE sNumber > 1;X
SELECT address
FROM Student
ORDER BY sNumber;
SELECT DISTINCT address
FROM Student
WHERE sNumber > 1;
SELECT address
FROM Student
WHERE sNumber > 1
ORDER BY sNumber;18
Possible Aggregations in SQL
SELECT COUNT (*) FROM Student;
SELECT COUNT (sNumber) FROM Student;
SELECT MIN (sNumber) FROM Student;
SELECT MAX (sNumber) FROM Student;
SELECT SUM (sNumber) FROM Student;
SELECT AVG (sNumber) FROM Student;
20
Grouping & Aggregation in SQL
New optional clause called “GROUP BY”
If the SELECT statement has “WHERE” Then WHERE conditions are evaluated first, then records
are grouped
SELECT pNumber, COUNT (sName), Min(gpa) FROM Student GROUP BY pNumber;
First form groups for each pNumber
21
Then count the records in each groupAnd get the minimum gpa for each group
GROUP BY: Example I
sNumber sName address pNumber
1 Dave 320FL 1
2 Greg 320FL 1
3 Matt 320FL 2
4 Jan 500MA 2
Student
SELECT count(*) AS CNT
FROM Student;
cnt count(*) (Student)
CNT
4
SELECT pNumber, count(*) AS CNT
FROM Student
WHERE sNumber > 1
GROUP BY pNumber;
pNumber CNT
1 1
2 2
pNumber,cnt count(*) ( (sNumber > 1) (Student))
22
GROUP BY: Example II
sNumber sName address pNumber
1 Dave 320FL 1
2 Greg 320FL 1
3 Matt 320FL 2
4 Jan 500MA 2
Student
SELECT pNumber,address, count(sName) AS CNT, sum(sNumber) AS SUM
FROM Student
WHERE sNumber >= 1
GROUP BY pNumber, address;
pNumber address CNT SUM
1 320FL 2 3
2 320FL 1 3
2 500MA 1 4
pNumber,address, CNT count(sName), SUM sum(sNumber) ( (sNumber > 1) (Student))
23
Restrictions of GROUP BY If you group by A1, A2, …An, then any other column projected in
SELECT clause must be inside an aggregation function
SELECT pNumber, address, count(sName) AS CNT, sum(sNumber) AS SUM
FROM Student
WHERE sNumber > 1
GROUP BY pNumber, address;
SELECT pNumber, address, sName, sum(sNumber) AS SUM
FROM Student
WHERE sNumber > 1
GROUP BY pNumber, address;X
SELECT pNumber, count(sName) AS CNT, sum(sNumber) AS SUM
FROM Student
WHERE sNumber > 1
GROUP BY pNumber, address;
24
HAVING Clause: Putting Condition on Groups
How to add conditions on each group? Select only the groups where the COUNT > 5
These conditions are after you build the groups (not before) Remember: WHERE conditions are executed before the
groups are formed
New optional clause called “HAVING”, added after the GROUP BY clause
SELECT pNumber, COUNT (sName) FROM Student GROUP BY pNumberHAVING SUM(sNumber) > 2;
Can reference aggregation inside HAVING
25
HAVING Clause: Example
sNumber sName address pNumber
1 Dave 320FL 1
2 Greg 320FL 1
3 Matt 320FL 2
4 Jan 500MA 2
Student
SELECT pNumber,address, count(sName) AS CNT, sum(sNumber) AS SUM
FROM Student
WHERE sNumber > 1
GROUP BY pNumber, address
HAVING sum(sNumber) > 3;
pNumber address CNT SUM
2 500MA 1 4
(SUM> 3) (pNumber,address, CNT count(sName), SUM sum(sNumber) ( (sNumber > 1) (Student)))
26
SELECT Statement Clauses
Optional clauses if added must be in the order above Order of execution
FROM Check which relations are used WHERE Filter records based on conditions GROUP BY Form groups HAVING Filter groups based on conditions ORDER BY Sort the data SELECT Form the projection list (output columns)
SELECT <projection list>FROM <relation names>WHERE <conditions>GROUP BY <grouping columns>HAVING <grouping conditions>ORDER BY <order columns>;
optional
27
QuestionsSELECT <projection list>FROM <relation names>WHERE <conditions>GROUP BY <grouping columns>HAVING <grouping conditions>ORDER BY <order columns>;
optional
28
Null Values
Null means ‘unknown’ value
Any expression containing Null returns Null 5 + null null ‘ABC’ || null null
Null in predicates returns UNKNOWN Predicates usually return TRUE or FALSE
30
Example
31
sNumber sName address pNumber
1 Dave 320FL 1
2 Greg null 1
3 Matt null 2
4 Jan 500MA 2
Student
SELECT sNumberFROM StudentWHERE address = ‘320FL’;
sNumber
1
2
3 May or may not appear
Use of “IS NULL” or “IS NOT NULL”
Check if a value is null or not
33
SELECT sNumberFROM StudentWHERE address is not nullAND address ‘320FL’;
SELECT sNumberFROM StudentWHERE address is null;
Select student numbers where the address is null
Remember:
SELECT sNumberFROM StudentWHERE address = null;
XThe returned value here is unknown
Use of “NVL” Function
NVL( exp1, exp2) If exp1 is null return exp2, otherwise return expr1
Can be used in projection list or in predicates
34
SELECT sNumberFROM StudentWHERE nvl(address, ‘n/a’) <> ‘n/a’AND address ‘320FL’;
SELECT sNumber, nvl(address, ‘N/A’)FROM Student;
sNumber address
1 320FL
2 N/A
3 N/A
4 500MA
Null with Grouping & Aggregation
Aggregation Null is ignored with all aggregates, e.g., SUM, AVG, MIN,
MAX except COUNT(*)
Grouping Null is considered as a separate group
35
Example
36
sNumber sName address pNumber
1 Dave 320FL 1
2 Greg null 1
3 Matt null null
4 Jan 500MA 2
Student
SELECT address, sum(pNumber) as sum, count(*) as cntFROM StudentGROUP BY address;
address
sum cnt
320FL 1 1
null 1 2
500MA 2 1
Nested Subquery
SQL provides a mechanism for the nesting of subqueries.
A subquery is a SELECT statement expression that is nested within another query
Subquery can appear in FROM or WHERE clauses
38
Nested Subquery in WHERE Clause
Since the predicates has = : The inner statement must return one record with one column In this case, DBMS will automatically convert the relation to a
single scalar value Otherwise an error is generated
39
SELECT *
FROM Student
WHERE pNumber =
(SELECT pNumber
FROM Professor
WHERE pName = ‘Mike’);
1- Execute this statement first to get the pNumber (inner SELECT)
2- Then, execute this statement once pNumber from the first step is known(outer SELECT)
CS3431
Example: Subqueries Retuning Scalar Value
sNumber sName address pNum
1 Dave 320FL 1
2 Greg 320FL 1
3 Matt 320FL 2
Student
pNumber pName address
1 MM 141FL
2 ER 201FL
Professor
sNumber sName
1 Dave
2 Greg
Select students of professor ‘MM’
SELECT sNumber, sNameFROM StudentWHERE pNum =
(SELECT pNumberFROM ProfessorWHERE pName=‘MM’);
SubQuery Returning a Relation (General Case)
Predicates may include any of (OP above) : Exists R True if R is not empty s in R True if tuple s appears in R s not in R True if tuple s does not appear in R
41
SELECT sNumber, sNameFROM StudentWHERE pNum OP
(SELECT pNumber FROM Professor WHERE pName=‘MM’);
Inner Select (R)
Outer Select (S)
CS3431
Example 1: Subqueries Returning Relations
sNumber sName address pNum
1 Dave 320FL 1
2 Greg 320FL 1
3 Matt 320FL 2
4 Sam 30IN 3
Student
pNumber pName address
1 MM 141FL
2 ER 201FL
3 XY 30WA
Professor
sNumber sName
1 Dave
2 Greg
3 Matt
Select students of professors with address like ‘%FL’
SELECT sNumber, sNameFROM StudentWHERE pNum IN
(SELECT pNumberFROM ProfessorWHERE address Like ‘%FL’);
CS3431
Example 2: Subqueries Returning Relations
sNumber sName address pNum
1 Dave 320FL 1
2 Greg 320FL 1
3 Matt 320FL 2
4 Sam 30IN 3
Student
pNumber pName address
1 MM 141FL
2 ER 201FL
3 XY 30WA
Professor
sNumber sName
1 Dave
2 Greg
3 Matt
4 Sam
SELECT sNumber, sNameFROM StudentWHERE Exists
(SELECT pNumberFROM ProfessorWHERE address Like ‘%FL’);
Always true because it is not empty
Comparison Using ALL and ANY
We took: Exists, IN, NOT IN
s > ALL R True if s > all values in R s > ANY R True if s > any value in R ‘>’ can be any of the other comparison operators, e.g., <, <=, >=, =, <> R must be relation with single column
45
SELECT sNumber, sNameFROM StudentWHERE pNum OP
(SELECT pNumber FROM Professor WHERE pName=‘MM’);
Inner Select (R)
Outer Select (S)
Example
sNumber sName address pNum
1 Dave 320FL 1
2 Greg 320FL 1
3 Matt 320FL 2
4 Sam 30IN 3
Student
pNumber pName address
1 MM 141FL
2 ER 201FL
3 XY 30WA
Professor
sNumber sName
3 Matt
4 Sam
SELECT sNumber, sNameFROM StudentWHERE pNum >= ALL
(SELECT pNumberFROM ProfessorWHERE address Like ‘%FL’);
This inner select returns 1 , 2
Correlated Selects If the “inner” select references the “outer” select correlated
In this case, the inner select is executed with each record from the outer select
47
Reference to the outer select
Meaning: For each supplier, execute the inner select, and then evaluate the WHERE clause
Returns: suppliers who do not have orders
Use of Inner Select with DML Commands
Inner select can be used with Insert, Update, Delete commands
48
INSERT INTO suppliers (supplier_id, supplier_name)SELECT account_no, nameFROM externalsWhere code = 1;
Notice that there is no keyword “values” in this case
Nested Subquery in FROM Clause
Use the inner SELECT like any other table It is just built on the fly
Inner SELECT can be a full statement with all clauses ORDER BY clause does not make sense in the inner select
49
SELECT *
FROM Student, (inner SELECT) AS q
WHERE … Table built on the fly
Recommended