Nested Queries (Sub-Queries). Objectives Learn how to run a query as a nested sub-query Condition on nested query Application of nested query Restriction.

Slides:



Advertisements
Similar presentations
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
Advertisements

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.
SQL-week5-1 In-Class Exercise Answer IST 210 Organization of Data IST2101.
Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs WHERE SUM (duration) = 100; (this will give an error)
DatabaseDatabase cs453 Lab8 1 Ins.Ebtesam AL-Etowi.
SQL Sub (or Nested ) Query. Examples Q: Find students whose GPA is below the average. –The criteria itself requires a SQL statement. –SELECT * FROM student.
SQL Subqueries Objectives of the Lecture : To consider the general nature of subqueries. To consider simple versus correlated subqueries. To consider the.
Advanced SQL Programming Mark Holm Centerfield Technology.
Subqueries 11. Objectives After completing this lesson, you should be able to do the following: Describe the types of problems that subqueries can solve.
Database Programming Sections 5 & 6 – Group functions, COUNT, DISTINCT, NVL, GROUP BY, HAVING clauses, Subqueries.
DATABASE PROGRAMMING Sections 5-7. Write a query that shows the average, maximum, and minimum salaries for all employees with jobs in the programming.
Introduction to Oracle9i: SQL1 Subqueries. Introduction to Oracle9i: SQL2 Chapter Objectives Determine when it is appropriate to use a subquery Identify.
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.
Objectives After completing this lesson, you should be able to do the following: Define subqueries Describe the types of problems that the subqueries.
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.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
Fundamentals, Design, and Implementation, 9/e CPE 481 Database Processing Chapter 6 Structured Query Language (SQL) Instructor:Suthep Madarasmi, Ph.D.
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-column subqueries, Multiple-row Subqueries, Correlated Subqueries 11/2/10,
INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015.
Using sub query. Sub query A query inside another query.
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.
Subqueries.
5 Copyright © Oracle Corporation, All rights reserved. Aggregating Data Using Group Functions.
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
Database Management Systems. NESTING OF QUERIES  Some queries require that existing values in the database be retrieved and then used in a comparison.
I NTRODUCTION TO SQL II. T ABLE JOINS A simple SQL select statement is one that selects one or more columns from any single table There are two types.
1 JOIN SUBQUERY Structured Query Language (SQL) - Part III.
Programming in R SQL in R. Running SQL in R In this session I will show you how to: Run basic SQL commands within R.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Views. Objectives Create views Modify/Drop Views Insert/Delete/Update to/from views Retrieve data from views.
Structured Query Language Introduction. Basic Select SELECT lname, fname, phone FROM employees; Employees Table LNAMEFNAMEPHONE JonesMark SmithSara
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
Query Tuning. Types of Nested Queries Uncorrelated subqueries with aggregates in the nested query SELECT ssnum FROM employee WHERE salary > (select avg(salary)
Chapter Eleven Data Manipulation Language (DML) Nested Queries Dr. Chitsaz Objectives Nested queries Application of nested queries Conditions on nested.
6 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using Subqueries.
Subqueries.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
Agenda for Class - 03/04/2014 Answer questions about HW#5 and HW#6 Review query syntax. Discuss group functions and summary output with the GROUP BY statement.
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-row Subqueries, Correlated Subqueries.
Chapter 7 Subqueries. Chapter Objectives  Determine when it is appropriate to use a subquery  Identify which clauses can contain subqueries  Distinguish.
1 Database Systems SQL: Advanced Queries. 2 Union, Intersection, and Except (2)   Find the names of those people who are either a graduate student or.
1 Introduction to Database Systems, CS420 SQL JOIN, Group-by and Sub-query Clauses.
5-1 Copyright © 2004, Oracle. All rights reserved. DISPLAYING DATA FROM MULTIPLE TABLES OUTER JOIN.
6 Copyright © 2006, Oracle. All rights reserved. Retrieving Data Using Subqueries.
Using Subqueries to Solve Queries
CS580 Advanced Database Topics
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
6/22/2018.
Subqueries.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Working with Tables: Join, Functions and Grouping
Using Subqueries to Solve Queries
SQL – Subqueries.
Generalization.
Writing Correlated Subqueries
Lab 5: Subqueries CISB224 02A, 02B Semester I, 2009/2010
Yong Choi School of Business CSU, Bakersfield
Tuning Queries from (E&N)
Using Subqueries to Solve Queries
SQL Subquery.
Relational Algebra Sample Questions.
Advance Database Systems
Subqueries Schedule: Timing Topic 25 minutes Lecture
Using Subqueries to Solve Queries
Subqueries.
Using Subqueries to Solve Queries
Subqueries Schedule: Timing Topic 25 minutes Lecture
Subqueries Schedule: Timing Topic 25 minutes Lecture
Query Tuning.
Presentation transcript:

Nested Queries (Sub-Queries)

Objectives Learn how to run a query as a nested sub-query Condition on nested query Application of nested query Restriction of nested query Pair wise and not pair wise comparison Nested query at multiple level Use correlated sub-query

Definitions Sub-query is a query inside another query Outer query vs. Inner query Inner query executed first Enclose queries are parenthesis Do not add ORDER BY to sub-query (why)? Result of sub-query will be used by outer query

Example 1 Employee (Name, SSN, Salary, B_date, Starting_Date, Dept) List Employees name who have salary higher than Mr. Smith: SELECT Name FROM Employee WHERE Salary> (SELECT Salary FROM Employee WHERE Name=‘Smith’);

Example 1 List Employees name who have salary higher than Mr. Smith: SELECT A.Name FROM Employee A, Employee B WHERE B.Name=‘Smith’ AND A.salary>B.salary;

Example 2 SELECT Name FROM Employee WHERE Salary = (SELECT MAX(Salary) FROM Employee);

Use of IN in Sub-Query SELECT Name FROM Employee WHERE Id IN (SELECT Id FROM Employee WHERE Salary BETWEEN AND 50000);

Use of IN in Sub-Query SELECT Name FROM Employee WHERE Id IN (SELECT Id FROM Employee WHERE NOT Salary BETWEEN AND 50000);

Use Sub-Query to join tables Department (Name, Phone, Address, No_Employee) SELECT Name FROM Employee WHERE Dept IN (SELECT Name FROM Department WHERE No_Emplyee>20);

Example SELECT Employee.Name FROM Employee, Department WHERE Department.Name= Employee.Dept AND No_Employee>20;

Multiple Levels Nested Queries SELECT Name FROM Employee WHERE Salary> (SELECT AVG(Salary) FROM Employee WHERE Dept IN (SELECT Name FROM Department WHERE No_Emplyee>20);

Pair Wise Comparison in Sub- Query SELECT Name, Id, GPA FROM Student WHERE (Major, Minor) IN (SELECT (Minor, Major) FROM Student WHERE Name=‘Smith’);