© 2007 by Prentice Hall6-1 Introduction to Oracle 10g Chapter 6 Creating Multitable Queries and Views James Perry and Gerald Post
© 2007 by Prentice Hall6-2 Chapter Outline Creating and Using Multitable Queries Creating and Using Views
© 2007 by Prentice Hall6-3 Table 6.1 Asking price distribution table CategoryIDLowLimitHighLimit 1000$0$50, $50,000$100, $100,000$150, $150,000$200, $200,000$250, $250,000$300, $300,000$350, $350,000$400, $400,000$2,000,000
© 2007 by Prentice Hall6-4 Table 6.2 SQL set operators Set operatorDescription UNIONReturns all unique rows retrieved by the queries. UNION ALLReturns all the rows retrieved by the queries, including any duplicate rows. INTERSECTReturns rows that are retrieved by both queries. MINUSReturns the rows that remain when the rows retrieved by a second query are removed (subtracted) from the rows retrieved by the first query.
© 2007 by Prentice Hall6-5 Table 6.3 ANY and ALL operator descriptions OperatorMeaning =ANYEqual to any value the subquery returns. It is the same as the IN operator. <ANYSmaller than the largest value the subquery returns. >ANYLarger than the smallest value the subquery returns. >ALLLarger than the largest value the subquery returns. <ALLSmaller than the smallest value the subquery returns.
© 2007 by Prentice Hall6-6 Table 6.4 Description of some of the user_views columns Column NameData TypeDescription view_nameVARCHAR2(30)Name of the view. text_lengthNUMBERNumber of characters in the view’s subquery. textLONGText of the view’s subquery that created the view.
© 2007 by Prentice Hall6-7 IDNameDiv 1001BobMkt 1002DawnSales 1003BettySales 1006NancyMkt 1009FredSales PK2Emp # Amount $2, $2, $1, $ $ $ $425 EmployeesSales Result of joining the Employees and Sales tables: from Employees from Sales 1002 Dawn Sales $2, Dawn Sales $2, Betty Sales $1, Betty Sales $ Fred Sales $ Fred Sales $ Fred Sales $ Joining two tables
© 2007 by Prentice Hall6-8 joining tables on primary key/foreign key pairs 6.2 Joining the Customers and Properties tables
© 2007 by Prentice Hall6-9 specifies column in both tables that links them 6.3 Joining tables on like-named columns
© 2007 by Prentice Hall Using a natural join
© 2007 by Prentice Hall Joining three tables and filtering rows
© 2007 by Prentice Hall6-12 IDNameDiv 1001BobMkt 1002DawnSales 1003BettySales 1006NancyMkt 1009FredSales PK2Emp # Amount $2, $2, $1, $200 SalespersonsSales Result of joining the Salespersons and Sales tables: from Salespersons from Sales 1002 Dawn Sales $2, Dawn Sales $2, Betty Sales $1, Betty Sales $ Bob Mkt null null null 1009 Nancy Mkt null null null 1009 Fred Sales null null null 6.6 Outer join illustration
© 2007 by Prentice Hall6-13 inner joinleft outer join SalespersonSales rows with matching values (selected rows) excluded rows rows with matching values excluded rows SalespersonSales selected rows 6.7 Venn diagrams showing inner join and left outer join
© 2007 by Prentice Hall Using a left outer join
© 2007 by Prentice Hall6-15 value 0 indicates no match in Listings (left) table 6.9 Showing non matching rows with a COUNT function
© 2007 by Prentice Hall Displaying the EmpSelfJoin table
© 2007 by Prentice Hall6-17 EmployeeIDBossID EmployeeIDBossID EmpSelfJoin eEmpSelfJoin m e.BossID = m.EmployeeID 6.11 Illustration of a table joined to itself by using aliases
© 2007 by Prentice Hall Listing managers and their employees with a self join query
© 2007 by Prentice Hall6-19 “Board of Directors” appears when the BossID is null 6.13 Managers and their employees produced using an outer join query
© 2007 by Prentice Hall6-20 non-equijoin join conditions 6.14 Creating the PriceCat table and running a non-equijoin query
© 2007 by Prentice Hall Comparing a non-equijoin and a right outer non-equijoin
© 2007 by Prentice Hall Using UNION in a compound query
© 2007 by Prentice Hall6-23 display homes newer by 18 years than the average for Arcata display agents who have the same title as Jessica Taylor 6.17 Using subqueries in WHERE clauses
© 2007 by Prentice Hall Examples of the IN operator and subqueries
© 2007 by Prentice Hall6-25 (additional rows are out of sight) 6.19 Description and contents of the AgentsHR table
© 2007 by Prentice Hall Using ALL and ANY operators in a query
© 2007 by Prentice Hall Using two subqueries and the ALL operator
© 2007 by Prentice Hall A correlated subquery to locate above-average asking prices
© 2007 by Prentice Hall A correlated subquery to display above-average salaries
© 2007 by Prentice Hall6-30 Count the customers without agents Count the customers without properties list the agents who are not designated “Licensed” 6.24 Using the EXISTS operator in several correlated subqueries
© 2007 by Prentice Hall Creating and describing views
© 2007 by Prentice Hall Displaying rows through the Nonbrokers view
© 2007 by Prentice Hall Defining a view with alternative column names
© 2007 by Prentice Hall Executing DML statements on a view
© 2007 by Prentice Hall Disallowing an update on a one-table complex view
© 2007 by Prentice Hall6-36 creating a multiple table view querying a multiple table view and the results 6.30 Creating a multiple table view and querying it
© 2007 by Prentice Hall6-37 creating a five-table view format columns display rows using the ForSale view 6.31 Creating a multiple table view joining five tables
© 2007 by Prentice Hall Displaying view information
© 2007 by Prentice Hall Listing and then dropping views you created