Presentation is loading. Please wait.

Presentation is loading. Please wait.

FEN 2014-04-271 More about SELECT, Nested selects GROUP BY, HAVING, ORDER BY Other joins Aggregate functions Views More about SQL.

Similar presentations


Presentation on theme: "FEN 2014-04-271 More about SELECT, Nested selects GROUP BY, HAVING, ORDER BY Other joins Aggregate functions Views More about SQL."— Presentation transcript:

1 FEN 2014-04-271 More about SELECT, Nested selects GROUP BY, HAVING, ORDER BY Other joins Aggregate functions Views More about SQL

2 2 SQL2 - DML (Q16): Sub queries (nested SELECTs) SELECTE.FNAME, E.LNAME FROMEMPLOYEE E WHEREE.SSNIN(SELECTESSN FROMDEPENDENT WHEREESSN = E.SSN ANDE.FNAME = DEPENDENT_NAME ANDSEX = E.SEX) Also ANY (SOME) and ALL in combination with comparison operators (>, >=, = and <>). FEN 2014-04-27

3 3 SQL2 - DML ( Q16): Sub queries (nested SELECTs) SELECTE.FNAME, E.LNAME FROMEMPLOYEE E WHEREE.SSN IN(SELECTESSN FROMDEPENDENT WHEREESSN = E.SSN ANDE.FNAME = DEPENDENT_NAME ANDSEX = E.SEX) For each row in outer table (E), the inner SELECT is executed. If E.SSN is contained in the result of the inner SELECT, then E is included in the result table for the outer SELECT.

4 FEN 2014-04-274 SQL2 - DML (Q7): Existential quantifier - EXISTS: SELECTFNAME, LNAME FROMEMPLOYEE WHEREEXISTS(SELECT* FROMDEPENDENT WHERESSN = ESSN) AND EXISTS(SELECT* FROMDEPARTMENT WHERESSN = MGRSSN)

5 FEN 2014-04-275 SQL2 - DML (Q6): NOT EXISTS: SELECTFNAME, LNAME FROMEMPLOYEE WHERENOT EXISTS(SELECT* FROMDEPENDENT WHERESSN =ESSN)

6 FEN 2014-04-276 SQL2 - DML For All i SQL Although SQL is supposed to be an implementation of first order predicate logic, it does not support the universal qualifier (FORALL), only the existential quantifier (EXISTS) is supported. A well known (?) result from predicate logic can be used in a workaround: Retrieving all elements satisfying some predicate is equivalent to retrieving elements that are not in the set of elements that do not satisfy the predicate: SELECT * FROM --- WHERE NOT EXISTS (SELECT * FROM --- WHERE NOT EXISTS ----

7 FEN 2014-04-277 Let x an arbitrary element in some set and p a predicate stating some condition on x: De Morgan’s Law:  (  x: p(x))   x:  p(x) Apply to  p(x):  (  x:  p(x))   x:  (  p(x)) Reduce the right hand side:  x: p(x)   (  x:  p(x)) “it is not true that there exists x, so p(x) is not true” – that is: “p is true for all x” SELECT * FROM --- WHERE NOT EXISTS (SELECT * FROM --- WHERE NOT EXISTS ---- A Side: Predicate Logic

8 FEN 2014-04-278 SQL2 - DML (Q3B): ”Retrieve the name of each employee who works on all projects controlled by department number 5” SELECTLNAME, FNAME FROMEMPLOYEE WHERENOT EXISTS (SELECT* FROMWORKS_ON B WHERE(B.PNO IN(SELECTPNUMBER FROMPROJECT WHEREDNUM = 5)) AND NOT EXISTS(SELECT* FROMWORKS_ON C WHEREC.ESSN = SSN AND C.PNO=B.PNO))

9 FEN 2014-04-279 SQL2 - DML SELECT DISTINCTESSN FROMWORKS_ON WHEREPNO IN (1,2,3) SELECTFNAME, LNAME FROMEMPLOYEE WHERESUPERSSN IS NULL

10 FEN 2014-04-2710 SQL2 - DML SELECTE.LNAME AS EMP_NAME, S.LNAME AS SUPER_NAME FROMEMPLOYEE AS E, EMPLOYEE AS S WHEREE.SUPERSSN = S.SSN New coulomb names in the resulting table. AS may be omitted.

11 FEN 2014-04-2711 SQL2 - DML Alternative notations for join: SELECTFNAME, LNAME, ADDRESS FROM(EMPLOYEE JOIN DEPARTMENT ON DNO=DNUMBER) WHEREDNAME = ’Research’ Provides a more clear syntax and opens for more specialised joins.

12 FEN 2014-04-2712 SQL2 - DML Natural join (not MS SQL Server): (Q1B): SELECTFNAME, LNAME, ADDRESS FROM(EMPLOYEE NATURAL JOIN (DEPARTMENT AS DEPT (DNAME,DNO,MSSN,MSDATE))) WHEREDNAME = ’Research’ DEPARTMENT.DNUMBER must be rename to DNO in order to match EMPLOYEE.DNO. Natural join is over two attributes with the same name (EMPLOYEE.DNO = DEPT.DNO).

13 FEN 2014-04-2713 SQL2 - DML Outer join: SELECTE.LNAME AS EMP_NAME, S.LNAME AS SUPER_NAME FROMEMPLOYEE AS E, EMPLOYEE AS S WHEREE.SUPERSSN = S.SSN Retrieves only employees who have a supervisor. Left Outer Join retrieves all employees and inserts NULL in the S- attributes for employees with no supervisor. SELECTE.LNAME AS EMP_NAME, S.LNAME AS SUPER_NAME FROM(EMPLOYEE E LEFT OUTER JOIN EMPLOYEE S ON E.SUPERSSN = S.SSN) Also RIGTH OUTER JOIN and FULL OUTER JOIN.

14 FEN 2014-04-2714 SQL2 - DML

15 FEN 2014-04-2715 What about employees with no supervisor?

16 FEN 2014-04-2716 Here they are!

17 FEN 2014-04-2717 SQL2 - DML

18 FEN 2014-04-2718 SQL2 - DML

19 FEN 2014-04-2719 SQL2 - DML Also: CROSS JOIN (Cartesian Product) UNION JOIN SQL2 provides many different ways of expressing the same join: This can be view as an advantage: More simple expressions Or as an disadvantage: More complicated language

20 FEN 2014-04-2720 SQL2 – DML: SELECT Queries: SELECT FROM [WHERE ] [GROUP BY ] [HAVING ] [ORDER BY ] [...]:WHERE, GROUP BY, HAVING and ORDER BY may be omitted.

21 FEN 2014-04-2721 SQL2 - DML Aggregate Functions: COUNT SUM MAX MIN AVG How are NULLs treated? AVG(-) == SUM(-)/COUNT(-) ???

22 FEN 2014-04-2722 SQL2 - DML Ex.: ”Number of Employees in the research department” SELECTCOUNT(*) FROMEMPLOYEE, DEPARTMENT WHEREDNO = DNUMBER AND DNAME = ’Research’

23 FEN 2014-04-2723 SQL2 - DML (Q24) Try this one with at least one employee with a null value in salary. Compare the result with the query: Select DNO, COUNT(*), SUM(SALARY)/COUNT(*)

24 FEN 2014-04-2724 Result of Q24

25 FEN 2014-04-2725 SQL2 - DML (Q26)

26 FEN 2014-04-2726 Result of Q26, 1

27 FEN 2014-04-2727 Result of Q26, 2

28 FEN 2014-04-2728 SQL2 - DML

29 Break for Exercises Elmasri: 5.5a FEN 2014-04-2729

30 FEN 2014-04-2730 SQL - VIEWS A view is virtual table which is created from one or more existing base tables. Views may be used in a layered architecture to provide different view of the database to different users. May also be used to increase efficiency of frequent queries, for instance to avoid JOINs.

31 FEN 2014-04-2731 SQL - VIEWS CREATE VIEWWORKS_ON1 AS SELECTFNAME, LNAME, PNAME, HOURS FROMEMPLOYEE, PROJECT, WORKS_ON WHERESSN=ESSN AND PNO=PNUMBER; Using this view, the query: SELECTFNAME, LNAME, PNAME FROMEMPLOYEE, PROJECT, WORKS_ON WHEREPNAME = 'ProductX' AND SSN = ESSN AND PNO = PNUMBER; May written as SELECTFNAME, LNAME, PNAME FROMWORKS_ON1 WHEREPNAME = 'ProductX'; And hence saving the join

32 FEN 2014-04-2732 SQL - VIEWS Updating through views is problematic: FX: Transfer John Smith from the project 'ProductX' to the project 'ProductY’ UPDATEWORKS_ON1 SETPNAME = ’ProductY’ WHERELNAME = ’Smith’ ANDFNAME = ’John’ ANDPNAME = ’ProductX’

33 FEN 2014-04-2733 SQL - VIEWS Which update of the base tables should be executed? This? Or this?

34 FEN 2014-04-2734 SQL - VIEWS Views and update: Updatable views Gray zone Not updatable vies

35 FEN 2014-04-2735 SQL - VIEWS Generally: Views defined over one base table can be updated, if the primary key (ore some candidate key) is included in the view. Views defined by joining more base tables are generally not updatable. Some joined view are in principle updatable: all primary keys from the base tables must be included in the view. Views defined using aggregate or grouping functions are not updatable. SQL2 standard establishes that joined view are not updatable.

36 FEN 2014-04-2736 Exercises 1.From session03 2.Investigate how MS SQL Server implements some of the SQL constructs treated to day 3.Elmasri 5.7 4.Elmasri 5.8, 5.9 5.Try out some of the queries in solutionssolutions 6.Extra: FlereSQLOpgaver.pdfFlereSQLOpgaver.pdf


Download ppt "FEN 2014-04-271 More about SELECT, Nested selects GROUP BY, HAVING, ORDER BY Other joins Aggregate functions Views More about SQL."

Similar presentations


Ads by Google