Presentation is loading. Please wait.

Presentation is loading. Please wait.

SubQueries Part III.

Similar presentations


Presentation on theme: "SubQueries Part III."— Presentation transcript:

1 SubQueries Part III

2 Subquery A subquery is a select statement (inner query) nested within another select statement (outer query) A subquery is always enclosed within parentheses Non-Correlated Subquery Allows each row of an outer query to be compared to the results of the inner query Correlated Subquery A subquery that references 1 or more columns from the outer query Executes every time a row is selected by the outer query

3 Non-Correlated Subquery
SELECT ti.csu_id ,ti.name ,ti.term_gpa FROM csus_term_info_eot_cur ti WHERE ti.term_gpa > (SELECT AVG(tx.term_gpa) FROM csus_term_info_eot_cur tx) ORDER BY ti.term_gpa DESC

4 Correlated Subquery SELECT c.id CSU_ID ,c.name Name
,c.course_reference_number CRN FROM csus_section_info_cur c JOIN csus_student_attribute a ON a.person_uid = c.person_uid AND a.academic_period = c.academic_period WHERE a.student_attribute = 'ASHS' AND NOT EXISTS (SELECT ‘X' FROM schedule_attribute s WHERE s.course_reference_number = c.course_reference_number AND s.academic_period = c.academic_period AND s.schedule_attribute IN ('DHAN', 'DHHS', 'DHHY', 'DHRI')) ORDER BY Name

5 Inline View A subquery defined in the FROM clause
The subquery must be named SELECT a_column a_col FROM z_table JOIN (SELECT b_column b_col FROM y_table) inline_view ON inline_view.b_col = a_col After it has been defined, an inline view is treated as if it were another joined table

6 Example Inline View SELECT x1.ssbxlst_term_code AS term, MAX(xs1.ssrxlst_crn) AS crn, s.ssbsect_subj_code AS subj, s.ssbsect_crse_numb AS crse, s.ssbsect_seq_numb AS sect, x1.ssbxlst_xlst_group AS xlist, summary.max_enrl AS seat_sum, x1.ssbxlst_max_enrl AS xlist_maxseat, s.ssbsect_camp_code AS campus FROM ssbxlst x1 JOIN ssrxlst xs1 ON xs1.ssrxlst_term_code = x1.ssbxlst_term_code AND xs1.ssrxlst_xlst_group = x1.ssbxlst_xlst_group JOIN ssbsect s ON s.ssbsect_term_code = xs1.ssrxlst_term_code AND s.ssbsect_crn = xs1.ssrxlst_crn LEFT OUTER JOIN (SELECT x.ssbxlst_term_code AS term, x.ssbxlst_xlst_group AS grp, SUM(s.ssbsect_max_enrl) AS max_enrl FROM ssbxlst x ON s.ssbsect_term_code = x.ssbxlst_term_code AND s.ssbsect_crn IN (SELECT xs.ssrxlst_crn FROM ssrxlst xs WHERE xs.ssrxlst_term_code = x.ssbxlst_term_code AND xs.ssrxlst_xlst_group = x.ssbxlst_xlst_group) GROUP BY x.ssbxlst_term_code ,x.ssbxlst_xlst_group) summary ON summary.term = x1.ssbxlst_term_code AND summary.grp = x1.ssbxlst_xlst_group List of cross listed courses where the cross list max enrollment does not equal the sum of the max enrollment for each section.

7 Example Inline View (cont)
WHERE x1.ssbxlst_term_code >= csus_f_cur_term() AND x1.ssbxlst_max_enrl != summary.max_enrl GROUP BY x1.ssbxlst_term_code ,s.ssbsect_subj_code ,s.ssbsect_crse_numb ,s.ssbsect_seq_numb ,x1.ssbxlst_xlst_group ,summary.max_enrl ,x1.ssbxlst_max_enrl ,s.ssbsect_camp_code ORDER BY term ,xlist ,subj ,crse ,sect

8 Questions?


Download ppt "SubQueries Part III."

Similar presentations


Ads by Google