SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.

Slides:



Advertisements
Similar presentations
Advanced SQL (part 1) CS263 Lecture 7.
Advertisements

© Abdou Illia MIS Spring 2014
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
Chapter 4 Joining Multiple Tables
A Guide to SQL, Seventh Edition. Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables.
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Introduction to Oracle9i: SQL1 Subqueries. Introduction to Oracle9i: SQL2 Chapter Objectives Determine when it is appropriate to use a subquery Identify.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Inner join, self join and Outer join Sen Zhang. Joining data together is one of the most significant strengths of a relational database. A join is a query.
Displaying Data from Multiple Tables. Obtaining Data from Multiple Tables Sometimes you need to use data from more than one table. In the example, the.
Objectives After completing this lesson, you should be able to do the following: Write SELECT statements to access data from more than one table using.
© 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 CHAPTER 7: ADVANCED SQL Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh,
Copyright © 2014 Pearson Education, Inc. 1 CHAPTER 7: ADVANCED SQL Essentials of Database Management Jeffrey A. Hoffer, Heikki Topi, V. Ramesh.
Introduction to Databases Chapter 7: Data Access and Manipulation.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Modern Database Management
Lecture 2 of Advanced Databases Advanced SQL Instructor: Mr.Ahmed Al Astal.
SQL Joins Oracle and ANSI Standard SQL Lecture 6.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Chapter 9 Joining Data from Multiple Tables
Copyright © 2004, Oracle. All rights reserved. Lecture 6 Displaying Data from Multiple Tables ORACLE.
ITBIS373 Database Development
Oracle Database Administration Lecture 2 SQL language.
A Guide to MySQL 5. 2 Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables Use a subquery.
Dr. Chen, Data Base Management Chapter 7: Advanced SQL Jason C. H. Chen, Ph.D. Professor of MIS School of Business Administration Gonzaga University Spokane,
Nested Queries (Sub Queries) A nested query is a form of a SELECT command that appears inside another SQL statement. It is also termed as subquery. The.
Joins & Sub-queries. Oracle recognizes that you may want data that resides in multiple tables drawn together in some meaningful way. One of the most important.
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
Chapter 4 Multiple-Table Queries
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining Multiple Tables.
Unit 4 Queries and Joins. Key Concepts Using the SELECT statement Statement clauses Subqueries Multiple table statements Using table pseudonyms Inner.
劉 志 俊 (Chih-Chin Liu) 中華大學 資訊工程系 October 2001 Chap 9 SQL (III): Advanced Queries.
Join, Subqueries and set operators. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Chapter 7: advanced sql Jeffrey A. Hoffer, V. Ramesh, Heikki Topi
SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries.
Chapter 12 Subqueries and Merge Statements
SQL Select Statement IST359.
Web Programming MySql JDBC Web Programming.
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
Subqueries.
SQL advanced select using Oracle 1 Multiple Tables: Joins and Set Operations Subqueries: Nested Queries.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Chapter 7 Subqueries. Chapter Objectives  Determine when it is appropriate to use a subquery  Identify which clauses can contain subqueries  Distinguish.
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
Copyright © 2016 Pearson Education, Inc. CHAPTER 7: ADVANCED SQL (PART I) Modern Database Management 12 th Edition Jeff Hoffer, Ramesh Venkataraman, Heikki.
4 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
CSC314 DAY 9 Intermediate SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall USING AND DEFINING VIEWS  Views provide users controlled.
Advanced SQL Advanced Database Dr. AlaaEddin Almabhouh.
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
Oracle Join Syntax.
Database Systems: Design, Implementation, and Management Tenth Edition
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
David M. Kroenke and David J
Contents Preface I Introduction Lesson Objectives I-2
Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management Tenth Edition
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Presentation transcript:

SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries

SQL advanced select using Oracle 2 Select, 156 Simple –data from a single table Advanced –data from more tables join sub-queries

SQL advanced select using Oracle 3 Join, 157 Cartesian product –select * from tableA, tableB all rows in tableA combined with all rows from tableB huge result! seldom useful!! Different types of joins –equi-join –non-equi-join –outer join –self-join

SQL advanced select using Oracle 4 Equi-join, 158 Joining 2 tables using common attributes [usually primary and foreign keys] Structure select … from tableA, tableB where tableA.PK = tableB.FK Comparison using equality (=) is equi-join Examples select * from student, city where city.postcode = student.postcode select * from student, course, studentCourse where student.ID = studentCourse.studentID and studentCourse.courseID = course.ID

SQL advanced select using Oracle 5 Non-equi-joins, 161 Where clause using operations other than equality –Fig. 7-6, page 162

SQL advanced select using Oracle 6 Outer joins, 163 Ordinary join –if a value in one table has no matching value in the other table, the row is not included in the result. Outer join –values without matching values are included in the result. Oracle has 2 syntaxes –Special syntax Special (old) Oracle syntax –New syntax SQL99 standard syntax

SQL advanced select using Oracle 7 Outer join, old syntax Old / special Oracle syntax SELECT … FROM tableA, tableB WHERE tableA.col1 (+)= tableB.col2 rows from tableA with no matching attribute in tableB is included in the result Fig. 7-7, page 163

SQL advanced select using Oracle 8 Outer join, new syntax New / SQL 99 standard syntax SELECT d.department_id, e.last_name FROM departments d LEFT OUTER JOIN employees e ON d.department_id = e.department_id ORDER BY d.department_id Result –Department with and without employees are included in the result New in Oracle 9i (not present in Oracle 8i) No examples in the book! Look in the Oracle SQL documentation for more examples

SQL advanced select using Oracle 9 Self-join, 165 Joining a table with itself. –ER recursive relationship –Rel. model foreign key to another attribute in the same relation. –same table used twice in the from clause we need to name the two instances of the table (table aliases) –select … from employee e, employee s where e.supervisorID = s.ID –Fig. 7-9 page 165

SQL advanced select using Oracle 10 Set operators, 166 Tables may be used in set operations –uniontableA union tableB all rows in tableA and all rows in tableB –union alltableA union all tableB as union, but includes duplicates –intersecttableA intersect tableB all rows that appears in both tableA and tableB –minustableA minus tableB all rows in tableA, but not in tableB –Tables must be "union compatible" same number + types + order of attributes

SQL advanced select using Oracle 11 Sub-query, 174 The where clause in a select statement may contain another select statement (sub-query)! –Fig. 8-1, page 175 –Two types of sub-queries single row –returning a single row of data, and most often a single data item multiple row –returning more than on row of data

SQL advanced select using Oracle 12 Single row query, 174 Inner query is executed first, returning a value that is used in the outer query. select … from tableA where attributA operator (sub-query) –Operator may be: =, >, <, etc.

SQL advanced select using Oracle 13 Sub-queries in create, insert, update and delete, 176 create table tableName as select query –pure redundancy! insert into tableName select query –pure redundancy! update tableName set (columnNames) = (query) where condition delete from tableName where columnName = (query)

SQL advanced select using Oracle 14 Multiple-row sub-queries, 181 Sub-query returning more than one row of data. select … from … where attribute operator (sub-query) –Operator may be in all any, =any –Figure 8-11, 8-12, page 182 –Figure 8-13, page 183

SQL advanced select using Oracle 15 Top-N analysis,183 MS Access SQL and SQL Server has a special keyword top –select top 10 attribute … –Oracle has no such feature, but it can be simulated using the pseudo-column rownum. –Fig , page 184 –sub-query in from clause inline view [more on views later on]

SQL advanced select using Oracle 16 Correlated subquery,185 Ordinary subquery –The inner query does not reference columns from the outer query –The inner query can be “calculated” once and for all. Correlated subquery –The inner query references columns from the outer query –Like programming 2 loops, one inside the other –Figure 8-17, page 187

SQL advanced select using Oracle 17 The EXISTS operator, 187 New operator used with correlated sub-queries Result –False If the current result of the sub-query is empty –True If the current result of the sub-query is not empty Examples –Figure 8-18, page 187 At least one student has the faculty (teacher) Select ‘1’ –We have to select something, but in this case we don’t use it! –Figure 8-19, page 188 No student has the faculty (teacher)

SQL advanced select using Oracle 18 Final remarks SQL (specially select) is like programming. –There's only one place to learn it - at the keyboard! Make advanced select statements incrementally –Starting from the innermost sub-query. –Monitor the results Take advantage of advanced select statements –Don't try to program the same features in your application.