Lab 5: Subqueries CISB224 02A, 02B Semester I, 2009/2010

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

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.
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.
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.
Lab 5: Subqueries CISB224 02A, 02B Semester I, 2009/2010 College of Information Technology, Universiti Tenaga Nasional 1.
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)
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.
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.
SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries.
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.
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.
Lecture 2 Joins and sub-queries. 2 Topics zJoins ySimple; Outer zSub-queries yaliases zIN Operator zNULL values zSaving and running files.
College of Information Technology, Universiti Tenaga Nasional1 Lab 2: Single-row Functions CISB224 01A CCSB244 01A Semester I, 2008/2009.
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.
Reporting Aggregated Data Using the Group Functions
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
Using Subqueries to Solve Queries
Chapter 3 Introduction to SQL
Chapter 3 Introduction to SQL(2)
Chapter 3 Introduction to SQL(3)
Subqueries.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Introduction to Database Systems, CS420
Group Functions Lab 6.
Working with Tables: Join, Functions and Grouping
Using Subqueries to Solve Queries
Generalization.
Writing Correlated Subqueries
Access Path Selection in a Relational Database Management System
مناهــــج البحث العلمي
Structured Query Language
Using Subqueries to Solve Queries
SQL Subquery.
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
POWER CHALLENGES Several Ways To Solve 7 CHALLENGES.
Using Subqueries to Solve Queries
Lab 3: Single-row Functions
Subqueries Schedule: Timing Topic 25 minutes Lecture
Subqueries Schedule: Timing Topic 25 minutes Lecture
Print a list of all people in department 15 who either are salaried and make more than or are part time or full time and make more than 25 per hour.
Query Tuning.
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

Introduction to Subqueries 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. College of Information Technology, Universiti Tenaga Nasional

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

Introduction to Subqueries – cont. Solution 1 – cont. Then, use the Deparment name obtained to display the other employees: SELECT Name, Title FROM emp WHERE Dept = 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. Admin College of Information Technology, Universiti Tenaga Nasional

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

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

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

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

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

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

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

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

Important!!! 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 College of Information Technology, Universiti Tenaga Nasional

More Operators 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 College of Information Technology, Universiti Tenaga Nasional

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