Download presentation
Presentation is loading. Please wait.
Published byJalynn Barbara Modified over 10 years ago
1
REVIEW: Examples (8): continued (for all, ) EMP( EMPNO, …,DNO,JOB,...) 100D3electrician 200D3plumber 300D3electrician 400D1electrician 500D1plumber 600D1carpenter 700D2electrician 800D2carpenter 900D2electrician Q12.Find the numbers of those departments that have employees who can do all the jobs that are done by an employee in department D3. Answer: D1, but not D2
2
SQL: for all ( ) using count Function Second Attempt: selectDNO fromDEPT D where(select count(distinct JOB) fromEMP ED3 whereED3.DNO = ‘D3’) = (select count(distinct EY.JOB) from EMP EY, EMP ED3 whereEY.DNO = D.DNO andEY.JOB = ED3.JOB and ED3.DNO = ‘D3’) D3JOBS: Jobs done by employees in department D3. DJOBS: Jobs done by employees in department D that are also done by employees in department D3. The first nested select is pretty trivial, we will focus on the second Nested select that contains EY & ED3.
3
Start Since there is an external reference to DEPT within the second Nested select we will execute the nested select for each DEPT Tuple. Assume the DEPT table contains only three tuples corr esponding to D1, D2 and D3 in that order. The first tuple we will evaluate is the DEPT.DNO = D1 Since the first nested select is trivial (it will always compute The two distinct jobs in D3) We will focus on the functioning of the second nested select.
4
ED3.EMPNO, ED3.DNO,ED3.JOB,..) 100D3electrician 200D3plumber 300D3electrician 400D1electrician 500D1plumber 600D1carpenter 700D2electrician 800D2carpenter 900D2electrician EY.EMPNO, EY.DNO,EY.JOB,..) 100D3electrician 200D3plumber 300D3electrician 400D1electrician 500D1plumber 600D1carpenter 700D2electrician 800D2carpenter 900D2electrician Both EY and ED3 are aliases for the Employee table and are shown here. From first principles remember that the first step in a select operation Is to simply compute the cross-product of ED3 and EY. Since there Are 9 tuples in both these tables there will be 81 tuples in the result. However, from the select conditions (EY.DNO = DEPT.DNO (=D1) and ED3.DNO = D3 we know that we need to focus Only on the crossproduct of the underlined tuples
5
ED3.EMPNO, ED3.DNO,ED3.JOB,..) 100D3electrician 200D3plumber 300D3electrician 100D3electrician 200D3plumber 300D3electrician 100D3electrician 200D3plumber 300D3electrician EY.EMPNO, EY.DNO,EY.JOB,..) 400D1electrician 500D1plumber 600D1carpenter 500D1plumber 600D1carpenter 400D1electrician 600D1carpenter 400D1electrician 500D1plumber Above are the 9 tuples in the cross product of ED3 and EY That satisfy the criteria ED3.DNO=D3 & EY.DNO = DEPT.DNO (D1) Among these we need to select those tuples that satisfy ED3.JOB=EY.JOB Which are underlined above. And we want to count the number of distinct Jobs = 2 (electrician & plumber). Since this count is equal to the count of the first nested select, D1 is displayed
6
ED3.EMPNO, ED3.DNO,ED3.JOB,..) 100D3electrician 200D3plumber 300D3electrician 400D1electrician 500D1plumber 600D1carpenter 700D2electrician 800D2carpenter 900D2electrician EY.EMPNO, EY.DNO,EY.JOB,..) 100D3electrician 200D3plumber 300D3electrician 400D1electrician 500D1plumber 600D1carpenter 700D2electrician 800D2carpenter 900D2electrician Now the process repeats for the next tuple of Dept (not shown). The DNO value of this tuple is D2. For the first nested select again The result will be the same 2 distinct jobs in D3. For the second Nested select we will again form the cross product of EY and ED3. Again we need only limit ourselves to the cross product of those Tuples in ED3 (satisfying ED3.DNO=D3) and those tuples in EY Satisfying (EY.DNO = DEPT.DNO (=D2)) shown underlined above.
7
ED3.EMPNO, ED3.DNO,ED3.JOB,..) 100D3electrician 200D3plumber 300D3electrician 100D3electrician 200D3plumber 300D3electrician 100D3electrician 200D3plumber 300D3electrician EY.EMPNO, EY.DNO,EY.JOB,..) 700D2electrician 800D2carpenter 900D2electrician 800D2carpenter 900D2electrician 700D2electrician 900D2electrician 700 D2electrician 800 D2carpenter Above are the 9 tuples in the cross product of ED3 and EY that satisfy the criteria ED3.DNO=D3 & EY.DNO = DEPT.DNO (D2). Among these we need to select those tuples that satisfy ED3.JOB=EY.JOB (underlined Above). And we want to count the number of distinct jobs = 1 (electrician) from this set of tuples. Since this count is not equal to the count of the first nested select, D2 is not displayed.
8
END Now we need to repeat the process for the next tuple in the DEPT Table, that is, the tuple with DNO = D3. It should be clear to you that if you go through the process we Just described for D1 and D2 that D3 will be selected and displayed. If the question had asked for those Departments other than D3 to be displayed we would have had To add the following to the end of the where clause AND DEPT.DNO <> D3.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.