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

Slides:



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

© Abdou Illia MIS Spring 2014
Sometimes you need to use data from more than one table. In example1, the report displays data from two separate tables. Employee IDs exist in the EMPLOYEES.
© 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.
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
1 Lecture 3: Subqueries DCO11310 Database Systems and Design By Rose Chang.
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.
Copyright © 2014 Pearson Education, Inc. 1 CHAPTER 7: ADVANCED SQL Essentials of Database Management Jeffrey A. Hoffer, Heikki Topi, V. Ramesh.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
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
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
Copyright © 2004, Oracle. All rights reserved. Lecture 6 Displaying Data from Multiple Tables ORACLE.
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.
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.
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.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Intermediate SQL: Aggregated Data, Joins and Set Operators.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
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.
6 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using Subqueries.
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.
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. SQL - Nulls Nulls are not equal to anything - Null is not even equal to Null where columna != ‘ABC’ --this will not return records where.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
6 Copyright © 2006, Oracle. All rights reserved. Retrieving Data Using Subqueries.
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
Oracle Join Syntax.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
More SQL Nested and Union queries, and more
Displaying Data from Multiple Tables Using Joins
CSC 453 Database Systems Lecture
Oracle Join Syntax.
Chapter 8 Advanced SQL.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Database Systems: Design, Implementation, and Management Tenth Edition
Displaying Data from Multiple Tables
Using Subqueries to Solve Queries
Displaying Data from Multiple Tables
Subqueries Schedule: Timing Topic 25 minutes Lecture
Subqueries Schedule: Timing Topic 25 minutes Lecture
Presentation transcript:

SQL advanced select using Oracle 1

2 Select Simple –data from a single table Advanced –data from more tables join sub-queries

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

SQL advanced select using Oracle 4 Equi-join 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 Where clause using operations other than equality –, etc.

SQL advanced select using Oracle 6 Outer joins 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.

SQL advanced select using Oracle 7 Outer join Syntaz 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

SQL advanced select using Oracle 8 Self-join 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

SQL advanced select using Oracle 9 Set operators 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 10 Sub-query The where clause in a select statement may contain another select statement (sub-query)! –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 11 Single row query 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 12 Sub-queries in create, insert, update and delete 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)

Sub-queries in select Select …. Sub from … sub where … sub Example: Subquery in select clause –select d.*, (select avg(salary) from teacher where teacher.department_id = d.id) from department d; SQL advanced select using Oracle 13

SQL advanced select using Oracle 14 Multiple-row sub-queries Sub-query returning more than one row of data. select … from … where attribute operator (sub-query) –Operator may be in all any, =any –Example: Teacher(s) with highest salary select * from teacher where salary >=all ( select salary from teacher);

SQL advanced select using Oracle 15 Top-N analysis 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. –sub-query in from clause inline view [more on views later on] –Example: Teachers with highest salaries select rownum, name, salary from (select * from teacher order by salary desc) where rownum <= 5;

SQL advanced select using Oracle 16 Correlated subquery 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 Example: Teachers with a salary higher than the average salary in their department select * from teacher t1 where salary > (select avg(salary) from teacher t2 where t1.department_id = t2.department_id);

SQL advanced select using Oracle 17 The EXISTS operator 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 –Good students (has at least one course) select * from student where exists (select * from student_course where studentid = student.id); –Lazy students (no courses) select * from student where not exists (select * from student_course where studentid = student.id);

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.