DatabaseDatabase cs453 Lab8 1 Ins.Ebtesam AL-Etowi.

Slides:



Advertisements
Similar presentations
SQL Query Slides Sharif University Of Technology Database Systems CE 384 Prepared By: Babak Bagheri Hariri
Advertisements

Subqueries 11. Objectives After completing this lesson, you should be able to do the following: Describe the types of problems that subqueries can solve.
Copyright  Oracle Corporation, All rights reserved. 6 Subqueries.
Database Programming Sections 5 & 6 – Group functions, COUNT, DISTINCT, NVL, GROUP BY, HAVING clauses, Subqueries.
5 Copyright © 2007, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
DATABASE PROGRAMMING Sections 5-7. Write a query that shows the average, maximum, and minimum salaries for all employees with jobs in the programming.
4 การใช้ SQL Functions. Copyright © 2007, Oracle. All rights reserved What Are Group Functions? Group functions operate on sets of rows to give.
6 6 Subqueries Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan (1999), published.
Multiple-Column Subqueries 12. Objectives After completing this lesson, you should be able to do the following: Write a multiple-column subquery Describe.
6-1 Copyright  Oracle Corporation, All rights reserved. Types of Subqueries Single-row subquery Main query Subquery returns CLERK Multiple-row subquery.
Copyright  Oracle Corporation, All rights reserved. 6 Writing Correlated Subqueries.
18 Copyright © Oracle Corporation, All rights reserved. Advanced Subqueries.
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.
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.
6 Copyright © 2004, Oracle. All rights reserved. Using Subqueries to Solve Queries.
6 Copyright © Oracle Corporation, All rights reserved. Subqueries.
Objectives After completing this lesson, you should be able to do the following: Define subqueries Describe the types of problems that the subqueries.
4-1 Copyright  Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Copyright © 2004, Oracle. All rights reserved. Lecture 6 Displaying Data from Multiple Tables ORACLE.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-column subqueries, Multiple-row Subqueries, Correlated Subqueries 11/2/10,
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.
Subqueries.
Subqueries.
Copyright © 2004, Oracle. All rights reserved. U SING S UBQUERIES TO S OLVE Q UERIES.
5 Copyright © Oracle Corporation, All rights reserved. Aggregating Data Using Group Functions.
6 Copyright © 2004, Oracle. All rights reserved. Using Subqueries to Solve Queries.
Structured Query Language. Group Functions What are group functions ? Group Functions Group functions operate on sets of rows to give one result per group.
7 Multiple-Column Subqueries. 7-2 Objectives At the end of this lesson, you should be able to: Write a multiple-column subquery Describe and explain the.
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.
Database Management Systems. NESTING OF QUERIES  Some queries require that existing values in the database be retrieved and then used in a comparison.
Unit 4 Queries and Joins. Key Concepts Using the SELECT statement Statement clauses Subqueries Multiple table statements Using table pseudonyms Inner.
Join, Subqueries and set operators. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
1 JOIN SUBQUERY Structured Query Language (SQL) - Part III.
DatabaseDatabase cs453 Lab5 1 Ins.Ebtesam AL-Etowi.
Structured Query Language
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
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
6 Copyright © Oracle Corporation, All rights reserved. Subqueries.
6 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using Subqueries.
Subqueries.
6 Subqueries. 6-2 Objectives At the end of this lesson, you should be able to: Describe the types of problems that subqueries can solve Define subqueries.
IST 210 More SQL Todd Bacastow IST 210: Organization of Data.
Aggregating Data Using Group Functions. What Are Group Functions? Group functions operate on sets of rows to give one result per group.
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 Introduction to Database Systems, CS420 SQL JOIN, Group-by and Sub-query Clauses.
6 Copyright © 2006, Oracle. All rights reserved. Retrieving Data Using Subqueries.
Reporting Aggregated Data Using the Group Functions
Using Subqueries to Solve Queries
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
Chapter 1 Introduction.
Subqueries.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Working with Tables: Join, Functions and Grouping
Using Subqueries to Solve Queries
Writing Correlated Subqueries
Using Subqueries to Solve Queries
SQL Subquery.
Chapter 1 Introduction.
Chapter 1 Introduction.
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
Presentation transcript:

DatabaseDatabase cs453 Lab8 1 Ins.Ebtesam AL-Etowi

Objectives 2 Ins.Ebtesam AL-Etowi Subqueries Describe the types of problems that subqueries can solve Define subqueries List the types of subqueries Write single-row and multiple-row subqueries.

Using a Subquery to Solve a Problem Ins.Ebtesam AL-Etowi 3 “ Which employees have a salary greater than John salary?” “what is John salary?” To solve this problem, you need two query: First, one query to find what Jones earns and Second query, to find who earns more than that amount

Ins.Ebtesam AL-Etowi 4 Subqueries SELECT select_list FROM table WHERE expr operator (SELECT select_list FROM table) The subquery (inner query) executes once before the main query. The result of the subquery is used by the main query (outer query). Using a Subquery The inner query determines the salary of employee the outer query takes the result of the inner query and uses this result to display all the employees who earn more than this amount

Ins.Ebtesam AL-Etowi 5 SELECT Fname from Employee WHERE salary > (SELECT salary FROM employee WHERE Ssn = ' )

Ins.Ebtesam AL-Etowi 6 Guidelines for Using Subqueries Enclose subqueries in parentheses. Place subqueries on the right side of the comparison operator. Don not add an ORDER BY clause to a subquery. Using single-row operators with single-row subqueries. Use multiple-row operators with multiple-row subqueries Types of Subqueries Single-row subquery Queries that return only one row from the inner SELECT statement Multiple-row subquery Queries that return more than one row from the inner SELECT statement Multiple-column subquery Queries that return more than one column from the inner SELECT statement

Ins.Ebtesam AL-Etowi 7 Single-Row Subqueries Return only one row Use single-row comparison operators. Operator (=, >, >=, ) SELECT Fname, Dno from Employee WHERE Dno= (SELECT Dno FROM employee WHERE Ssn= ' ‘)

Ins.Ebtesam AL-Etowi 8 Executing Single-Row Subqueries Display employee whose depatement number is the same as that employee and whose salary is greater than that of employee The example consists of three query blocks: the outer query and two inner queries. The inner query blocks are executed first. SELECT Fname, Dno from Employee WHERE Dno= (SELECT Dno FROM employee WHERE Ssn= ' ' ) AND salary > (SELECT salary FROM employee WHERE Ssn = ' ')

Ins.Ebtesam AL-Etowi 9

10 Using Group Functions in a Subquery You can display data from a main query by using a group function in a subquery to return a single row. Displays the employee name, department number and salary of all employee whose salary is equal to the minimum salary SELECT Fname, Dno, Salary FROM employee WHERE Salary = (SELECT MIN(Salary) FROM employee)

Ins.Ebtesam AL-Etowi 11 Having Clause with Subqueries The SQL Server executes subqueries first. The SQL Server returns results into the HAVING clause of the main query SELECT Dno, MIN(Salary) FROM employee GROUP BY Dno HAVING MIN(salary) > (SELECT MIN(Salary) FROM employee WHERE Dno = 5)

Ins.Ebtesam AL-Etowi 12 What is Wrong with this Statement SELECT Dno, avg(Salary) FROM employee GROUP BY Dno HAVING avg(salary) > (SELECT avg(Salary) FROM employee GROUP BY Dno) Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=,, >= or when the subquery is used as an expression.

Multiple-Row Subqueries Ins.Ebtesam AL-Etowi 13 Return more than one row Use Multiple-row comparison operators MeaningOperator Equal to any member in the list IN Compare value to each value returned by the subquery ANY Compare value to every value returned by the subquery ALL SELECT Fname, Dno, Salary FROM employee WHERE Salary IN (SELECT MIN(Salary) FROM employee GROUP BY Dno)

Ins.Ebtesam AL-Etowi 14 Find the employee salary in (30000, 5000, 2500), first name, and departement number SELECT Fname, Dno, Salary FROM employee WHERE Salary IN (30000, 25000,2500)

Ins.Ebtesam AL-Etowi 15 Using ANY Operator in Multiple-Row Subqueries SELECT Ssn, Fname, Dno, Salary FROM employee WHERE Salary < ANY (SELECT Salary FROM employee WHERE Dno=4) AND Dno <>4

Ins.Ebtesam AL-Etowi 16 Using ALL Operator in Multiple-Row Subqueries SELECT Ssn, Fname, Dno, Salary FROM employee WHERE Salary < ALL (SELECT AVG(Salary) FROM employee GROUP BY Dno)

17 Ins.Ebtesam AL-Etowi

Exercises Ins.Ebtesam AL-Etowi 18 Write a query to display the first name employee and birthdates for all employee in the same department as create a query to display the Ssn and Fname for all employees who salary more than the average salary. Sort the results in descending order of salary. 3-Write a query that will display the employee number and name for all employees who work in a department with any employee whose name contains a o. 4-Display the Fname and salary of all employee who supervisor ‘ ’ 5- Display the Dno, Fname, and salary for all emplyee in the 5 Departement number.