Download presentation
Presentation is loading. Please wait.
1
Table Joins Part II
2
Joining Tables Used to query data from 2 or more tables using 1 SELECT statement. The 2 tables must have at least one common column Columns do not have to have the same name
3
Oracle vs ANSI Standard
SELECT spriden_id CSU_ID ,spriden_last_name Stdt_Name FROM spriden ,spraddr WHERE spriden_pidm = spraddr_pidm AND spraddr_atyp_code = 'MA' AND spraddr_zip like '80523%' AND spriden_change_ind IS NULL ORDER BY Stdt_Name ANSI Standard SELECT spriden_id CSU_ID ,spriden_last_name Stdt_Name FROM spriden JOIN spraddr ON spraddr_pidm = spriden_pidm WHERE spraddr_atyp_code = 'MA' AND spraddr_zip like '80523%' AND spriden_change_ind IS NULL ORDER BY Stdt_Name
4
Column Ambiguously Defined
If the column names in the two tables are the same, dot notation must be used table_name.column_name SELECT csus_term_info_fal.csu_id CSU_ID ,csus_term_info_fal.name Stdt_Name ,trn_amount Tuition FROM csus_term_info_fal JOIN csut_ar_trn ON csus_term_info_fal.pidm = csut_ar_trn.pidm WHERE csut_ar_trn.term_code = ‘200790’ AND category_code = ‘TUI’
5
Column Ambiguously Defined
table_alias.column_name SELECT ti.csu_id CSU_ID ,ti.name Stdt_Name ,ar.trn_amount Tuition FROM csus_term_info_fal ti JOIN csut_ar_trn ar ON ti.pidm = ar.pidm WHERE ar.term_code = ‘200790’ AND ar.category_code = ‘TUI’ Error will result if Oracle cannot determine which column to use
6
Different Types of Joins
Inner Join Most common Data in the joining columns must match for results to be returned Outer Join Returns all rows from the “outer” table and only the rows with matching data in the joining columns from the other table(s) Self Join Data from one column of a table is related to the data in another column of the same table Create alias’ for the table and use an inner or outer join Cartesian Product Every row of the 1st table is returned with every row of the 2nd table Usually a mistake
7
Example Inner Join How many students have registered for at least one on-campus class?
8
Example Inner Join How many students have registered for at least one on-campus class? SELECT sfrstcr_term_code Term ,COUNT(DISTINCT sfrstcr_pidm) Count FROM sfrstcr INNER JOIN stvrsts ON stvrsts_code = sfrstcr_rsts_code JOIN ssbsect ON ssbsect_term_code = sfrstcr_term_code AND ssbsect_crn = sfrstcr_crn WHERE stvrsts_incl_sect_enrl = 'Y' AND ssbsect_camp_code = 'M' GROUP BY sfrstcr_term_code ORDER BY Term
9
Example Inner Join Results
10
Example Outer Join For how many credits has every person in the database registered?
11
Example Outer Join For how many credits has every person in the database registered? SELECT i.spriden_id CSU_ID ,i.spriden_last_name || ', ' || i.spriden_first_name Stdt_Name ,SUM(NVL(c.sfrstcr_credit_hr, 0)) Credit_Hrs FROM spriden i LEFT OUTER JOIN sfrstcr c ON c.sfrstcr_pidm = i.spriden_pidm WHERE i.spriden_change_ind IS NULL GROUP BY i.spriden_id ,i.spriden_last_name || ', ' || i.spriden_first_name ORDER BY Credit_Hrs ,Stdt_Name
12
Example Outer Join Results
13
Example Self Join Identify the students who have registered for Continuous Registration in addition to another course.
14
Example Self Join Identify the students who have registered for Continuous Registration in addition to another course. SELECT DISTINCT i.spriden_id ,i.spriden_last_name ,i.spriden_first_name ,r.sfrstcr_term_code FROM sfrstcr r JOIN sfrstcr rx ON rx.sfrstcr_pidm = r.sfrstcr_pidm AND rx.sfrstcr_term_code = r.sfrstcr_term_code JOIN spriden i ON i.spriden_pidm = r.sfrstcr_pidm JOIN ssbsect s ON s.ssbsect_term_code = r.sfrstcr_term_code AND s.ssbsect_crn = rx.sfrstcr_crn WHERE i.spriden_change_ind IS NULL AND s.ssbsect_subj_code = 'CR' AND s.ssbsect_crse_numb = 'CONRG’ AND r.sfrstcr_crn <> s.ssbsect_crn AND r.sfrstcr_rsts_code NOT IN ('CW', 'XF', 'XE', 'XD') ORDER BY r.sfrstcr_term_code
15
Example Self Join Identify the students who have registered for Continuous Registration in addition to another course.
16
Example Cartesian Product
How many active mailing addresses have a zip code of 80523?
17
Example Cartesian Product
How many active mailing addresses have a zip code of 80523? SELECT COUNT(*) FROM spriden ,spraddr WHERE spraddr_atyp_code = 'MA' AND SYSDATE BETWEEN NVL(spraddr_from_date, (to_date('01-JAN-1900', 'DD-MON-YYYY'))) AND NVL(spraddr_to_date, (to_date('31-DEC-2099', 'DD-MON-YYYY'))) AND spraddr_zip LIKE '80523%' AND spriden_change_ind IS NULL 8 minutes later… 1,367,196,180 What’s wrong? The answer should be 3,212
18
Questions?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.