2 Lesson C ObjectivesAfter completing this lesson, you should be able to:Create SQL queries that join multiple tablesCreate nested SQL queriesCombine query results using SET operatorsCreate and use database views
3 Joining Multiple Tables a JoinCombines data from multiple tables using foreign key referencesSyntaxSELECT column1, column2, …FROM table1, table2WHERE table1.joincolumn = table2.joincolumnAND search_condition(s);SELECT s_id, s_last, f_lastFROM student, facultyWHERE student.f_id = faculty.f_idAND f_last IN (‘Marx’, ‘Zhulin’);
4 Joining Multiple Tables (continued) Must qualify column name in SELECT clauseSpecify name of table that contains column followed by period then column nameExample: SELECT s_id, s_last, student.f_idJoin conditionSpecifies table names to be joined and column names on which to join tablesExample: WHERE student.f_id = faculty.f_id
5 FROM Student NATURAL JOIN faculty; Inner JoinsSimplest type of joinAlso called: Equality join, Equijoin, Natural joinVALUES in one table equal to values in other tableQuery design diagram helps get the query rightSELECT s_id, s_last, s_first, student.f_id, f_lastFROM student, facultyWHERE student.f_id = faculty.f_id;Could be replaced by:FROM Student NATURAL JOIN faculty;
6 Display column, search column, join column Display columns: appear in SELECT clauseSearch columns: appear in search conditionJoin columns: primary key and foreign key column on which you join the tables.Linkage table: contains join column to link other tables through foreign key values.SELECT f_lastFROM faculty, course_section, termWHERE faculty.f_id = course_section.f_idAND course_section.term_id = term.term_idAND term_desc = 'Summer 2007';6
7 Deriving a SQL Query From a Query Design Diagram 4 tables, 3 linksAll 4 tables must be named in the FROM clauseQuery must have 3 join conditions because there are 3 linksAlways 1 fewer join condition than number of tables that query joins.If you omit one join condition, the query creates a Cartesian product (every row in one table is joined with every row in other table) with more row than expected.SELECT course_name, gradeFROM student, enrollment, course_section, courseWHERE student.s_id = enrollment.s_idAND enrollment.c_sec_id = course_section.c_sec_idAND course_section.course_no = course.course_noAND s_last = 'Jones'AND s_first = 'Tammy';Search conditions
8 Outer JoinsInner joins return row only if values exist in all joined tablesOuter joins returnall rows from one table (called inner table) andonly matching rows from second table (outer table)Syntax: inner_table.join_col = outer_table.join_col(+)(+) operator signals Oracle to insert NULL for columns from the outer table with no matching rows in the inner table.
9 Self-join Query that joins table to itself Must create table alias Alternate name assigned to table in query’s FROM clauseSyntax: FROM table1 alias1, table1 alias2 …
10 Creating Nested Queries Nested queryConsists of a main query and one or more subqueriesMain queryFirst query that appears in SELECT commandSubqueryRetrieves values that main query’s search condition must matchSubquery is evaluated first. Then, DBMS substitute subquery’s output into main query.
11 Creating Nested Queries Q: What would happen if a subquery generated more values than the main query is expecting?11
12 Creating subqueries that return multiple values 12
13 Using Multiple Subqueries Within a Nested Query Use AND and OR operatorsTo join search conditions associated with subqueries
14 Using SET operators to combine Query Results UNIONQueries must have same number of display column in their SELECT clauseCorresponding display columns must have same data typeNote: S_LAST, S_FIRST, S_PHONE used as display title even though there are faculty members names displayed along with students.
15 Using SET operators to combine Query Results INTERSECTQueries must have same number of display column in their SELECT clauseCorresponding display columns must have same data typeSuppresses duplicates15
16 Using SET operators to combine Query Results MINUSQueries must have same number of display column in their SELECT clauseCorresponding display columns must have same data typeSuppresses duplicatesFinds difference between two query results16
17 Creating and Using Database Views Source queryUsed to create viewSpecify subset of single table’s columns or rows or join multiple tablesUpdatable viewsCan be used to update databaseSyntaxCREATE VIEW view_nameAS source_query;OrCREATE OR REPLACE VIEW view_name
18 Removing Views DROP VIEW command Remove view from user schema Syntax DROP VIEW view_name;