SQL. Sub-query Course Course_nmcredit_hrsstaff_name logic3Smith semantics5Peters programming5 Jones physics3 Hewlett Faculty Faculty_nameNamerank Science.

Slides:



Advertisements
Similar presentations
Multiple Table Queries
Advertisements

© Abdou Illia MIS Spring 2014
© 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.
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
 Database is SQL1.mdb ◦ import using MySQL Migration Toolkit 
DatabaseDatabase cs453 Lab8 1 Ins.Ebtesam AL-Etowi.
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  Oracle Corporation, All rights reserved. 6 Writing Correlated Subqueries.
Introduction to Oracle9i: SQL1 Subqueries. Introduction to Oracle9i: SQL2 Chapter Objectives Determine when it is appropriate to use a subquery Identify.
CSEN 5314 Quiz What type of join is needed when you wish to include rows that do not have matching values? A. Equi-joinB. Natural join C. Outer.
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.
--The SQL Query Language DML--1 The SQL Query Language DML The SQL Query Language DML (SELECT)
Sub Queries Pertemuan 5 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
IS 4420 Database Fundamentals Chapter 8: Advanced SQL Leon Chen
Nov 24, 2003Murali Mani SQL B term 2004: lecture 12.
Chapter 6 SQL: Data Manipulation Cont’d. 2 ANY and ALL u ANY and ALL used with subqueries that produce single column of numbers u ALL –Condition only.
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
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.
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.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
CSC271 Database Systems Lecture # 12. Summary: Previous Lecture  Row selection using WHERE clause  WHERE clause and search conditions  Sorting results.
Copyright Curt Hill SQL Queries Yet Another Set of Query Features.
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.
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.
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
ORT Braude, CSE 61309, ©2004 Gary Schloss Exam Question #1: ER, RA & SQL Consider a DB schema with the following relations: - Student (s#, sname) - Professor.
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.
Subqueries.
Nested Subqueries in SQL By Kenneth Cheung CS 157A Section 2 Professor Lee.
IST 210 More SQL Todd Bacastow IST 210: Organization of Data.
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 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.
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.
Using Subqueries to Solve Queries
CH 9 SQL 9.1 Querying A Single Table 9.2 Querying Multiple Tables
Indexes By Adrienne Watt.
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
References: Text Chapters 8 and 9
Subqueries.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Dumps4Download Oracle 1z0-051 Exam Dumps - Oracle 1z0-051 Exam Dumps Questions
Using Subqueries to Solve Queries
Writing Correlated Subqueries
SQL Structured Query Language 11/9/2018 Introduction to Databases.
20761B 12: Using Set Operators Module 12   Using Set Operators.
Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
JOINs JOINs can be used to combine tables A CROSS JOIN B
CSC 453 Database Systems Lecture
SQL: Structured Query Language
Using Subqueries to Solve Queries
SQL Subquery.
SQL: Structured Query Language
Subqueries Schedule: Timing Topic 25 minutes Lecture
Using Subqueries to Solve Queries
Subqueries.
SQL set operators and modifiers.
Using Subqueries to Solve Queries
Subqueries Schedule: Timing Topic 25 minutes Lecture
Subqueries Schedule: Timing Topic 25 minutes Lecture
SELECT from Multiple Tables
SQL.
Presentation transcript:

SQL

Sub-query Course Course_nmcredit_hrsstaff_name logic3Smith semantics5Peters programming5 Jones physics3 Hewlett Faculty Faculty_nameNamerank Science HewlettProf. Maths SmithSL Maths PetersSL MathsJonesPL select course_nm from course where credit_hrs<=4 and staff_name in (select name from faculty where rank='Prof.'); 'What are the course names where the credit hours are four or less and staffed by professors?'

subquery first from faculty ScienceHewlettProf. Maths SmithSL MathsPetersSL Maths JonesPL where rank='Prof. Science HewlettProf. select name Hewlett

then outer query from course logic 3Smith semantics5Peters programming5 Jones physics3Hewlett where credit_hrs<=4 and staff_name in Hewlett physics 3Hewlett select course_nm physics result of sub query

Correlated Subqueries SELECT customer.name FROM customer WHERE EXISTS (SELECT city FROM SUPPLIER WHERE supplier.city=customer.city); Often a sub-query can be processed independently of the main query.In correlated sub-queries this is not possible. Processing of the sub-query relies on some value from the main query as above i.e.customer.city. Processing must start with the main query. FROM clause produces an intermediate table identical to CUSTOMER. For each row in this intermediate table the WHERE clause is executed. So there are probably multiple executions of the sub-query unless the intermediate table consists of a single row.

1.Sub-query FROM clause produces a table identical to SUPPLIER. 2. Sub-query WHERE clause picks out all rows satisfying the sub-query condition. 3. That condition uses the value of city in the current row of the main query being considered. 4. Sub-query SELECT clause projects CITY column to form final table for sub-query. 5. EXISTS returns TRUE if the final table from sub- query contains any values. FALSE otherwise. 6. If TRUE returned then main query SELECT clause chooses the current value of customer row to be included in final result. 7. SELECT projects from this row the customer.name. 8. If EXISTS returns FALSE current customer row not selected for final table. 9. Sub-query then executed for next row of customer table. SELECT customer.name FROM customer WHERE EXISTS (SELECT city FROM SUPPLIER WHERE supplier.city=customer.city;

Customer NameCity United BiscuitsNewcastle General Food OilsHull Tate and LyleYork Supplier Name City General Flour MillsCambridge Eastern Crops Hull RHMYork select customer.name from customer where exists (select city from supplier where supplier.city=customer.city); 'Find the customers that have a supplier in the same city.' Compares the first row in customer with all the rows in supplier. If one or more rows in supplier match the where condition the customer name from customer is placed in result table. None match where condition so- Partial Result Name

Then takes next row of customer and compares with all the rows in supplier- Second row of supplier matches condition so 'exists' returns true and customer.name selected for partial result. NameCity United Biscuits Newcastle General Food Oils Hull Tate and Lyle York Customer Supplier Name City General Flour Mills Cambridge Eastern Crops Hull RHM York select customer.name from customer where exists (select city from supplier where supplier.city=customer.city); Partial Result Name General Food Oils

Customer NameCity United BiscuitsNewcastle General Food OilsHull Tate and LyleYork Supplier NameCity General Flour MillsCambridge Eastern CropsHull RHMYork Result Name General Food Oils Tate and Lyle

IN, ALL, ANY IN Lists a set of values and tests if a value is or is not in the list. SELECT * FROM SUPPLIER WHERE PRICE IN (2.00, 3.00, 4.00); This list could be a set of column values returned from a sub-query. ALL/ANY ALL has the meaning 'every member of the set.'. So all can be used to test every value in a column against some condition. SELECT S_NAME FROM SUPPLIER WHERE PRICE < ALL (SELECT PRICE FROM SUPPLIER WHERE CITY='Leeds'); 'names of suppliers whose prices are lower than all the suppliers in Leeds'.

SELECT S_NAME FROM SUPPLIER WHERE PRICE< ANY (SELECT PRICE FROM SUPPLIER WHERE CITY='Leeds'); 'find all the suppliers with a price lower than any supplier in Leeds'.