Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL: Structured Query Language

Similar presentations


Presentation on theme: "SQL: Structured Query Language"— Presentation transcript:

1 SQL: Structured Query Language
Instructor: Mohamed Eltabakh

2 Aggregation + GroupBy

3 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;

4 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 Then count the records in each group And get the minimum gpa for each group SELECT pNumber, COUNT (sName), Min(gpa) FROM Student GROUP BY pNumber; First form groups for each pNumber

5 GROUP BY: Example I Student cnt  count(*) (Student)
sNumber sName address pNumber 1 Dave 320FL 2 Greg 3 Matt 4 Jan 500MA cnt  count(*) (Student) pNumber,cnt count(*) ( (sNumber > 1) (Student)) SELECT count(*) AS CNT FROM Student; SELECT pNumber, count(*) AS CNT FROM Student WHERE sNumber > 1 GROUP BY pNumber; CNT 4 pNumber CNT 1 2

6 GROUP BY: Example II Student
sNumber sName address pNumber 1 Dave 320FL 2 Greg 3 Matt 4 Jan 500MA pNumber,address, CNT  count(sName), SUM  sum(sNumber) ( (sNumber > 1) (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 500MA 4

7 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;

8 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 pNumber HAVING SUM(sNumber) > 2; Can reference aggregation inside HAVING

9 HAVING Clause: Example
Student sNumber sName address pNumber 1 Dave 320FL 2 Greg 3 Matt 4 Jan 500MA  (SUM> 3) (pNumber,address, CNT  count(sName), SUM  sum(sNumber) ( (sNumber > 1) (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; Applied before the grouping Applied after the grouping on each group pNumber address CNT SUM 2 500MA 1 4

10 Example Queries Find customers having loans with sum > 20,000. Report the customer names and the total loans amount SELECT customer_name, sum(amount) AS SUM FROM Loan L, Borrower B WHERE L.loan_number = B. loan_number GROUP BY customer_name HAVING sum(amount) > 20,000;

11 Example Queries Find the cities with more than 3 branches
SELECT branch_city FROM Branch GROUP BY branch_city HAVING count(branch_name) > 3;

12 Example Queries Report the largest and smallest loans given by each branch in NY city along with the branch name. SELECT branch_name, Max(amount) As MaxLoan, Min(amount) As MinLoan FROM Loan L, Branch B Where L.branch_name = B.branch_name AND branch_city = ‘NY’ GROUP BY branch_name;

13 SELECT Statement Clauses
SELECT <projection list> FROM <relation names> WHERE <conditions> GROUP BY <grouping columns> HAVING <grouping conditions> ORDER BY <order columns>; optional 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)

14 Example: Order of Execution
Student sNumber sName address pNumber 1 Dave 320FL 2 Greg 3 Matt 4 Jan 500MA 5 SELECT pNumber,address, count(sName) AS CNT, sum(sNumber) AS SUM FROM Student WHERE sNumber > 1 GROUP BY pNumber, address HAVING sum(sNumber) > 3; 1 2 3 4 pNumber address CNT SUM 2 500MA 1 4

15 Questions optional SELECT <projection list>
FROM <relation names> WHERE <conditions> GROUP BY <grouping columns> HAVING <grouping conditions> ORDER BY <order columns>; optional

16 More in SELECT Statement
Special handling for NULL values Queries inside Insert/Update/Delete Nested subqueries

17 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

18 Having Null in the data is problematic and needs special care…
Example Student sNumber sName address pNumber 1 Dave 320FL 2 Greg null 3 Matt 4 Jan 500MA sNumber 1 2 3 SELECT sNumber FROM Student WHERE address = ‘320FL’; May or may not appear Having Null in the data is problematic and needs special care…

19 Use of “IS NULL” or “IS NOT NULL”
Check if a value is null or not SELECT sNumber FROM Student WHERE address is null; Select student numbers where the address is null SELECT sNumber FROM Student WHERE address is not null AND address = ‘320FL’; Remember: SELECT sNumber FROM Student WHERE address = null; X The returned value here is unknown

20 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 SELECT sNumber FROM Student WHERE 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 4 500MA

21 Null with Grouping & Aggregation
Null is ignored with all aggregates, e.g., SUM, AVG, MIN, MAX except COUNT() Grouping Null is considered as a separate group

22 Example Student SELECT address, sum(pNumber) as sum, count(*) as cnt
sNumber sName address pNumber 1 Dave 320FL 2 Greg null 3 Matt 4 Jan 500MA SELECT address, sum(pNumber) as sum, count(*) as cnt FROM Student GROUP BY address; address sum cnt 320FL 1 null 2 500MA

23 More in SELECT Statement
Special handling for NULL values Queries inside Insert/Update/Delete Nested subqueries

24 Use of Select Inside Insert
All records returned from the Select will be inserted Notice that there is no keyword “values” in this case INSERT INTO suppliers (supplier_id, supplier_name) SELECT account_no, name FROM externals Where code = 1; Number of columns and data types should match

25 Use of Select Inside Delete
Student Registration sNumber sName address 1 Dave 320FL 2 Greg null 3 Matt 4 Jan 500MA sNumber courseID grade 4 DB1 A 2 F 3 DB2 Delete from Student all those who have grade ‘F’ Delete From Student Where sNumber in (Select sNumber from Registration Where grade = ‘F’); Students number 2 & 4 will be deleted…

26 Use of Select Inside Delete
Student Registration sNumber sName address 1 Dave 320FL 2 Greg null 3 Matt 4 Jan 500MA sNumber courseID grade 4 DB1 A 2 F 3 DB2 Delete from Student all those who do not have registration Delete From Student Where sNumber not in (Select sNumber from Registration); Student number 1 will be deleted…

27 Use of Select Inside Update
Student Registration sNumber sName address 1 Dave 320FL 2 Greg null 3 Matt 4 Jan 500MA sNumber courseID grade 4 DB1 A 2 F 3 DB2 Update the grades of student ‘Matt’ to B Update Registration Set grade = ‘B’ Where sNumber in (Select sNumber from Student Where sName =‘Matt’);

28 Remember… In Delete and Update
From Student Where sNumber not in (Select sNumber from Registration); Update Registration Set grade = ‘B’ Where sNumber in (Select sNumber from Student Where sName =‘Matt’); In Delete and Update - The ‘From’ clause always has one table - The subquery can be only added in the ‘Where’ clause

29 More in SELECT Statement
Special handling for NULL values Queries inside Insert/Update/Delete Nested subqueries

30 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

31 Nested Subquery in FROM Clause
SELECT * FROM Student, (inner SELECT) AS q WHERE … Table built on the fly 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

32 Example Subquery 1 is computed on the fly
It is treated as a normal table after that

33 Nested Subquery in WHERE Clause
2- Then, execute this statement once pNumber from the first step is known (outer SELECT) SELECT * FROM Student WHERE pNumber = (SELECT pNumber FROM Professor WHERE pName = ‘Mike’); 1- Execute this statement first to get the pNumber (inner SELECT) 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

34 Example: Subqueries Retuning Scalar Value
Student Professor sNumber sName address pNum 1 Dave 320FL 2 Greg 3 Matt pNumber pName address 1 MM 141FL 2 ER 201FL Select students of professor ‘MM’ SELECT sNumber, sName FROM Student WHERE pNum = (SELECT pNumber FROM Professor WHERE pName=‘MM’); sNumber sName 1 Dave 2 Greg CS3431

35 SubQuery Returning a Relation (General Case)
Outer Select (S) SELECT sNumber, sName FROM Student WHERE pNum OP (SELECT pNumber FROM Professor WHERE pName=‘MM’); Inner Select (R) Predicates may include any of (OP above) : s in R  True if tuple s appears in R s not in R  True if tuple s does not appear in R s = R  R must return a single value (otherwise invalid op) Exists R  True if R is not empty Not Exists R  True if R is empty

36 Example 1: Subqueries Returning Relations
Student Professor sNumber sName address pNum 1 Dave 320FL 2 Greg 3 Matt 4 Sam 30IN pNumber pName address 1 MM 141FL 2 ER 201FL 3 XY 30WA Select students of professors with address like ‘%FL’ SELECT sNumber, sName FROM Student WHERE pNum IN (SELECT pNumber FROM Professor WHERE address Like ‘%FL’); sNumber sName 1 Dave 2 Greg 3 Matt CS3431

37 Example 2: Subqueries Returning Relations
Student Professor sNumber sName address pNum 1 Dave 320FL 2 Greg 3 Matt 4 Sam 30IN pNumber pName address 1 MM 141FL 2 ER 201FL 3 XY 30WA SELECT sNumber, sName FROM Student WHERE Exists (SELECT pNumber FROM Professor WHERE address Like ‘%FL’); sNumber sName 1 Dave 2 Greg 3 Matt 4 Sam Always true because it is not empty CS3431

38 Comparison Using ALL and ANY
Outer Select (S) SELECT sNumber, sName FROM Student WHERE pNum OP (SELECT pNumber FROM Professor WHERE pName=‘MM’); Inner Select (R) 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

39 Example Student Professor SELECT sNumber, sName FROM Student
address pNum 1 Dave 320FL 2 Greg 3 Matt 4 Sam 30IN pNumber pName address 1 MM 141FL 2 ER 201FL 3 XY 30WA SELECT sNumber, sName FROM Student WHERE pNum >= ALL (SELECT pNumber FROM Professor WHERE address Like ‘%FL’); sNumber sName 3 Matt 4 Sam This inner select returns 1 , 2


Download ppt "SQL: Structured Query Language"

Similar presentations


Ads by Google