Lab 5: Subqueries CISB224 02A, 02B Semester I, 2009/2010 College of Information Technology, Universiti Tenaga Nasional 1.

Slides:



Advertisements
Similar presentations
ON RULES, PROCEDURES, CACHING AND VIEWS IN DATA BASE SYSTEM by M. Stonebraker, A. Jhingran, J. Goh, and S. Potamianos UC Berkley Presented by Zhou Ji.
Advertisements

UNIVERSITI TENAGA NASIONAL “Generates Professionals” SOFTWARE DEVELOPMENT METHOD EXERCISE.
DatabaseDatabase cs453 Lab8 1 Ins.Ebtesam AL-Etowi.
SQL Subqueries Objectives of the Lecture : To consider the general nature of subqueries. To consider simple versus correlated subqueries. To consider the.
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-7. Write a query that shows the average, maximum, and minimum salaries for all employees with jobs in the programming.
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.
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.
Chapter 11.1 and 11.2 Data Manipulation: Relational Algebra and SQL Brian Cobarrubia Introduction to Database Management Systems October 4, 2007.
Objectives After completing this lesson, you should be able to do the following: Define subqueries Describe the types of problems that the subqueries.
Creating a Table Create a table, “emp”, containing: –empno – a 4 digit employee number –ename – up to 10 character string –job – up to 9 character string.
CSc-340 2b1 Introduction to SQL Chapter 3 [2 of 2] Null Values Aggregate Functions Nested Subqueries Modification of the Database.
4-1 Copyright  Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
1 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
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.
SQL (DDL & DML Commands)
Subqueries.
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.
1 Information Retrieval and Use (IRU) CE An Introduction To SQL Part 1.
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.
Join, Subqueries and set operators. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
Lab 4: Displaying Data from Multiple Tables CISB224 02A, 02B Semester I, 2009/2010 College of Information Technology, Universiti Tenaga Nasional 1.
Structured Query Language Introduction. Basic Select SELECT lname, fname, phone FROM employees; Employees Table LNAMEFNAMEPHONE JonesMark SmithSara
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
Displaying Data from Multiple Tables (SQL99 Syntax with examples)
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Subqueries These slides are licensed under.
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.
Lab 3: Single-row Functions College of Information Technology, Universiti Tenaga Nasional 1 CISB224 02A, 02B Semester I, 2009/2010.
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-row Subqueries, Correlated Subqueries.
College of Information Technology, Universiti Tenaga Nasional1 Lab 2: Single-row Functions CISB224 01A CCSB244 01A Semester I, 2008/2009.
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.
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.
6 Copyright © 2006, Oracle. All rights reserved. Retrieving Data Using Subqueries.
Using Subqueries to Solve Queries
Chapter 3 Introduction to SQL(3)
Subqueries.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Introduction to Database Systems, CS420
Using Subqueries to Solve Queries
Generalization.
Writing Correlated Subqueries
مناهــــج البحث العلمي
Lab 5: Subqueries CISB224 02A, 02B Semester I, 2009/2010
Using Subqueries to Solve Queries
SQL Subquery.
College Student Management System
Subqueries Schedule: Timing Topic 25 minutes Lecture
Lab 4: Displaying Data from Multiple Tables
CISB224 01A, 01B, 02A, 02B CCSB244 01A, 01B Semester I, 2007/2008
Using Subqueries to Solve Queries
Subqueries.
Using Subqueries to Solve Queries
Lab 3: Single-row Functions
Subqueries Schedule: Timing Topic 25 minutes Lecture
Subqueries Schedule: Timing Topic 25 minutes Lecture
Lab 2: Retrieving Data from the Database
Presentation transcript:

Lab 5: Subqueries CISB224 02A, 02B Semester I, 2009/2010 College of Information Technology, Universiti Tenaga Nasional 1

Introduction to Subqueries College of Information Technology, Universiti Tenaga Nasional 2 Suppose that you:  Want to know all the other employees that work in the same department as Zarina Majid  Don’t know Zarina Majid’s department. You need to solve the sub-problem before you can solve the main problem. There are two ways to do this. Let’s call this part the main problem. And this part, the sub-problem.

Introduction to Subqueries – cont. College of Information Technology, Universiti Tenaga Nasional 3 Solution 1 1. Find out the department by executing: SELECT Dept FROM emp WHERE Name = ‘Zarina Majid’ Admin

Introduction to Subqueries – cont. College of Information Technology, Universiti Tenaga Nasional 4 Solution 1 – cont. 2. Then, use the Deparment name obtained to display the other employees: SELECT Name, Title FROM emp WHERE Dept = Admin This method requires you to execute two queries:  One query to solve the sub-problem  Another query to solve the main problem Instead, you could solve the sub-problem and the main problem in just one query.

Introduction to Subqueries – cont. College of Information Technology, Universiti Tenaga Nasional 5 Solution 2 1. Type out the query that solves the main problem. SELECT Name, Title FROM emp WHERE Dept = ‘Admin’

Introduction to Subqueries – cont. College of Information Technology, Universiti Tenaga Nasional 6 Solution 2 – cont. 2. At the search condition, delete the Deparment value and type in a pair of parentheses. SELECT Name, Title FROM emp () WHERE Dept = ()

Introduction to Subqueries – cont. College of Information Technology, Universiti Tenaga Nasional 7 Solution 2 – cont. 3. Within the parentheses, type in the query that solves the sub-problem.

Introduction to Subqueries – cont. College of Information Technology, Universiti Tenaga Nasional 8 Solution 2 – cont. SELECT Name, Title FROM emp WHERE Dept = (SELECT Dept FROM emp WHERE Name = ‘Zarina Majid’ ) Admin

Where You Can Use Subqueries College of Information Technology, Universiti Tenaga Nasional 9  In the WHERE clause  In the HAVING clause  In the FROM clause (later)

Example of Subquery in WHERE College of Information Technology, Universiti Tenaga Nasional 10 Display employees who have the same title as Nordin. SELECT Name, Title, Dept FROM emp WHERE Title = (SELECT Title FROM emp WHERE Name like ‘Nordin%’)

Example of Subquery in HAVING College of Information Technology, Universiti Tenaga Nasional 11 Display all the departments that have an average salary bill greater than the average of Admin deparment

Example of Subquery in HAVING – cont. College of Information Technology, Universiti Tenaga Nasional 12 SELECT Dept, AVG(salary) FROM emp GROUP BY Dept HAVING AVG(salary) < (SELECT AVG(salary) FROM emp WHERE Dept=‘Admin’)

Important!!! College of Information Technology, Universiti Tenaga Nasional 13  You may not have an ORDER BY clause in the subquery  If the subquery returns more than one value i.e. a list of values, than you must use the IN operator in the main query’s search condition

More Operators College of Information Technology, Universiti Tenaga Nasional 14 Operator IS NULL Example: ReportsTo IS NULL IS NOT NULL The negation of the operators that you have learnt NOT BETWEEN NOT IN NOT LIKE

Using More Than One Search Conditions with AND or OR College of Information Technology, Universiti Tenaga Nasional 15 Display the customers who are located in the KL, Perak and Johor SELECT CompName, State FROM cust WHERE State IN (‘KL’, ‘Perak, ‘Johor’ State = ‘KL’ OR State = ‘Perak’ OR State = ‘Johor’