Sub Queries Pertemuan 5 Matakuliah: T0413/Current Popular IT II Tahun: 2007.

Slides:



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

Multiple Table Queries
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Module 6: Working with Subqueries. Overview Introduction to Subqueries Using a Subquery as a Derived Table Using a Subquery as an Expression Using a Subquery.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Introduction to Oracle9i: SQL1 Subqueries. Introduction to Oracle9i: SQL2 Chapter Objectives Determine when it is appropriate to use a subquery Identify.
Maintaining Referential Integrity Pertemuan 2 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
1 DDL – subquery Sen Zhang. 2 Objectives What is a subquery? Learn how to create nested SQL queries Read sample scripts and book for different kinds of.
VIEWS Pertemuan 7 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Introduction to Structured Query Language (SQL)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Overview Relational Databases and SQL Pertemuan 1 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Optimizing SQL Pertemuan 13 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data
Security and Transaction Management Pertemuan 8 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Database Systems More SQL Database Design -- More SQL1.
Introduction to Structured Query Language (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.
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 8 Advanced SQL.
Chapter 3 Single-Table Queries
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
Advanced SQL Murat Kantarcioglu Adapted from Silberchatz et al. slides.
Ashwani Roy Understanding Graphical Execution Plans Level 200.
1 Chapter 8: Advanced SQL. Chapter 8 2 Processing Multiple Tables – Joins Join – a relational operation that causes two or more tables with a common domain.
SQL/Lesson 4/Slide 1 of 45 Using Subqueries and Managing Databases Objectives In this lesson, you will learn to: *Use subqueries * Use subqueries with.
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.
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
1 Definition of a subquery Nested subqueries Correlated subqueries The ISNULL function Derived tables The EXISTS operator Mixing data types: CAST & CONVERT.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Using Special Operators (LIKE and IN)
1 © Prentice Hall, 2002 Chapter 8: Advanced SQL Modern Database Management 6 th Edition Jeffrey A. Hoffer, Mary B. Prescott, Fred R. McFadden.
Chapter 4 Multiple-Table Queries
9 Advanced Query Formulation with SQL (Chapter 9).
1 Multiple Table Queries. 2 Objectives  Retrieve data from more than one table by joining tables  Using IN and EXISTS to query multiple tables  Nested.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
Optimization of Nested Queries Sujatha Thanigaimani COSC 6421.
Chapter 12 Subqueries and Merge Statements
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
SQL. Sub-query Course Course_nmcredit_hrsstaff_name logic3Smith semantics5Peters programming5 Jones physics3 Hewlett Faculty Faculty_nameNamerank Science.
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
SQL: Sub-queries Single-value sub-queries Single-column sub-queries Sub-queries that produce tables Correlated sub-queries D. Christozov / G.Tuparov INF.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
Chapter 7 Subqueries. Chapter Objectives  Determine when it is appropriate to use a subquery  Identify which clauses can contain subqueries  Distinguish.
Slide 1Chapter 9: Advanced Query Formulation with SQL Database Design, Application Development, and Administration, 5 th Edition Copyright © 2011 by Michael.
Slide 1 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla In this session, you will learn to: Query data by using joins Query.
BTM 382 Database Management Chapter 8 Advanced SQL Chitu Okoli Associate Professor in Business Technology Management John Molson School of Business, Concordia.
CSC314 DAY 9 Intermediate SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall USING AND DEFINING VIEWS  Views provide users controlled.
Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
IFS180 Intro. to Data Management Chapter 10 - Unions.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
More SQL: Complex Queries,
Relational Database Design
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
References: Text Chapters 8 and 9
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
Database Systems: Design, Implementation, and Management Tenth Edition
Using Subqueries to Solve Queries
Writing Correlated Subqueries
SQL Structured Query Language 11/9/2018 Introduction to Databases.
SQL : Query Language Part II CS3431.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Using Subqueries to Solve Queries
Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management Tenth Edition
Presentation transcript:

Sub Queries Pertemuan 5 Matakuliah: T0413/Current Popular IT II Tahun: 2007

2 AGENDA: How Do Subqueries Work? Subqueries and Join Correlated Subqueries Subquery Example Subqueries Operators: EXISTS, ANY, SOME, ALL, using COUNT Book: Mastering SQL by Martin Gruber Sybex (2000) Chapter : 11-12

3 How Do Subqueries Work? SQL provides you the ability to nest queries within one another The inner queries generates values that are tested in the predicate of the outer query SELECT * FROM Orders WHERE snum = (SELECT snum FROM Salespeople WHERE sname = ‘Motika’);

4 How Do Subqueries Work? (cont’d) Using aggregate functions in Subqueries SELECT * FROM Orders WHERE amt > (SELECT AVG(amt) FROM Orders WHERE odate = ’10/04/2000’); Using IN with Subqueries that produce multiple rows SELECT * FROM Orders WHERE snum in (SELECT snum FROM Salespeople WHERE city – ‘London’);

5 How Do Subqueries Work? (cont’d) Using expressions in Subqueries SELECT * FROM Customers WHERE cnum = (SELECT snum FROM Salespeople WHERE sname = ‘Serres’);

6 Subqueries and Join The subquery version would execute more efficiently. In the Joins version, the DBMS would have to go through each possible combination of rows from the two tables and test it against the compound predicates. Subqueries in HAVING – SELECT rating, COUNT(DISTINCT cnum) FROM Customers GROUP BY rating HAVING rating > (SELECT AVG(rating) FROM Customers WHERE city = ‘San Jose’);

7 Correlated Subqueries When you use subqueries in SQL, you can refer in the inner query to the table in the FROM clause of the outer query, forming a correlated subqueries How the Correlated Subquery Works – Select a row from the table named in the outer query, called the current candidate row. – Store the values from his candidates row in the alias named in the FROM clause of the outer query – Perform the subquery. Use the value from the outer query’s candidate row in the subquery. It is call outer reference. – Evaluate the predicate of the outer query on the basis of the results of the subquery performed. – Repeat the procedure for the next candidate row of the outer query and so on until all the candidate rows have been tested.

8 Correlated Subqueries (cont’d) Using Correlated Subqueries to Find Anomalies – SELECT * FROM Orders main WHERE NOT snum = (SELECT snum FZROM Customers WHERE cnum = main.cnum); Correlated a Table with Itself – Enabling you to extract certain complex forms of derived information. – SELECT * FROM Orders outer WHERE amt > (SELECT AVG(amt) FROM Orders inner WHERE inner.cnum = outer.cnum);

9 Correlated Subqueries (cont’d) Correlated Subqueries in HAVING – SELECT odate, SUM(amt) FROM Orders a GROUP BY odate HAVING SUM(amt) > (SELECT MAX(amt) FROM Orders b WHERE a.odate = b.odate);

10 Subquery Example Advanced Subquery Example SELECT * FROM Salespeople first WHERE EXISTS (SELECT * FROM Customers second WHERE first.snum = second.snum AND 1 < (SELECT COUNT(*) FROM Orders WHERE Orders.cnum = second.cnum));

11 Subqueries Operators: EXISTS EXISTS is an operator that produces a TRUE or FALSE values, in other words, a Boolean expression. EXISTS simply checks whether a query produces output and therefore cannot be UNKNOWN. SELECT cnum, cname, city FROM Customers WHERE EXISTS (SELECT * FROM Customers WHERE city = ‘San Jose’); – The inner query selected all data for all customers in San Jose. – The EXISTS operator in the outer predicate noted that the subquery produced some output and made the predicate to be TRUE.

12 Subqueries Operators: EXISTS (cont’d) Using EXISTS with Correlated Subqueries – With a correlated subquery, the EXISTS clause is evaluated separately for each row of the table referenced in the outer query, as are other predicate operators used with correlated subqueries. – SELECT DISTINCT snum FROM Customers outer WHERE EXISTS (SELECT * FROM Customers inner WHERE inner.snum = outer.snum AND inner.cnum <> outer.cnum);

13 Subqueries Operators: EXISTS (cont’d) Combining EXIST with Joins – SELECT DISTINCT first.snum, sname, first.city FROM Salespeople first, Customers second WHERE EXISTS (SELECT * FROM Customers third WHERE second.snum = third.snum AND second.cnum <> third.cnum) AND first.snum = second.snum); Using NOT EXISTS EXISTS and Aggregates

14 Subqueries Operators: ANY or SOME SOME and ANY are interchangeable; wherever we use the term ANY, SOME would work just the same. SELECT * FROM Salespeople WHERE city = ANY (SELECT city FROM Customers); – The ANY operator takes all values produced by the subquery. – And evaluates them to TRUE if ANY of them equal to the value of the current row of the outer query. – The datatypes should be comparable. – Opposite of EXISTS.

15 Subqueries Operators: ALL With ALL, the predicate is TRUE if every value selected by the subquery satisfies the condition in the predicate of the outer query. SELECT * FROM Customers WHERE rating > ALL (SELECT rating FROM Customers WHERE city = ‘Rome’); ALL is used primarily with inequalities rather than equalities because a value can be “equal to all’ of the results of a subquery only if all of those results are, in fact, identical. Use ALL with nonequalities, using the <> operator.

16 Subqueries Operators (cont’d) One significant difference between ALL and ANY is the way the deal with the situation in which the subquery returns no values. – ALL is automatically TRUE – ANY is automatically FALSE Using COUNT – SELECT * FROM Customers outer WHERE 1 > (SELECT COUNT(*) FROM Customer inner WHERE outer.rating <= inner.rating AND inner.city =‘Rome’);

17 End of Sub Queries Thank you