CISB224 01A, 01B, 02A, 02B CCSB244 01A, 01B Semester I, 2007/2008

Slides:



Advertisements
Similar presentations
What Is a User-defined Function? Scalar Functions –Similar to a built-in function Multi-Statement Table-valued Functions –Content like a stored procedure.
Advertisements

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.
Introduction to Oracle9i: SQL1 Subqueries. Introduction to Oracle9i: SQL2 Chapter Objectives Determine when it is appropriate to use a subquery Identify.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved. 1.
6 Copyright © 2004, Oracle. All rights reserved. Using Subqueries to Solve Queries.
6 Copyright © Oracle Corporation, All rights reserved. Subqueries.
Sample queries Practice in using Oracle SQL (1 of 2)
Tutorial 5 Multi-table queries. Tutorial 5 objectives Displaying Data from Multiple Tables –[ ]Write SELECT statements to access data from more than one.
Lab 5: Subqueries CISB224 02A, 02B Semester I, 2009/2010 College of Information Technology, Universiti Tenaga Nasional 1.
1 Joining Tables in Queries. 2 Objectives You will be able to Write SQL queries that use Join operations to retrieve information from multiple tables.
Final Exam Guide PHP NOTE: PHP CODE WILL BE BLUE, HTML IS BLACK EXAMPLE
INTERACTIVE SQL PART II Prepared By: Mitali Sonar (Assistant Prof.) 1 10/17/2015.
Module 3: Retrieving Data. Overview Retrieving Data by Using the SELECT Statement Filtering Data Formatting Result Sets How Queries Are Processed Performance.
IMS 4212: Intro to SQL 1 Dr. Lawrence West, Management Dept., University of Central Florida Introduction to SQL—Topics Introduction to.
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.
Module 7: Modifying Data. Overview Using Transactions Inserting Data Deleting Data Updating Data Performance Considerations.
More queries Outer joins and summary queries. Inner and outer joins An Inner join only returns matching rows from two tables –E.g. if I join the customer.
Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2.
10-1 An Introduction to Systems A _______ is a set of sentences joined by the word ____ or by a ________________. Together these sentences describe a ______.
Subqueries.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
IMS 4212: Intro to Multi-Table SELECT Statements 1 Dr. Lawrence West, MIS Dept., University of Central Florida Multi-Table SELECT Statements—Topics.
College of Information Technology, Universiti Tenaga Nasional1 Lab 2: Single-row Functions CISB224 01A CCSB244 01A Semester I, 2008/2009.
Day 5 - More Complexity With Queries Explanation of JOIN & Examples Explanation of JOIN & Examples Explanation & Examples of Aggregation Explanation &
Lab week 10 Aggregates and sub-queries And assignment details.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
Module 5: Joining Multiple Tables. Overview Using Aliases for Table Names Combining Data from Multiple Tables Combining Multiple Result Sets.
Dr. Chen, Oracle Database System (Oracle) 1 Basic Nested Queries and Views Jason C. H. Chen, Ph.D. Professor of MIS School of Business Gonzaga University.
6 Copyright © 2006, Oracle. All rights reserved. Retrieving Data Using Subqueries.
Using Subqueries to Solve Queries
Structured Query Language
Rob Gleasure robgleasure.com
Querying in Access Objectives: Learn how to use the Access Query Design Tool manipulate data in Access: Sorting data Aggregating Data Performing Calculations.
Module 3: Retrieving Data
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
Inner Joins Objectives: Creating Queries with data from Multiple Tables Joining two tables using an Inner Join Referential Data Integrity Cascade Update.
ATS Application Programming: Java Programming
Subqueries Schedule: Timing Topic 25 minutes Lecture
Structured Query Language – The Basics
Using Subqueries to Solve Queries
Sage Password Recovery Sage Group Sage is a British Multinational Enterprise software company Sage headquarter is in New castle UK Sage.
Basic Nested Queries and Views
TVX-in-Industry Paper Title in One or Two Lines
Writing SELECT Queries
20761B 10: Using Subqueries Module 10   Using Subqueries.
Web Services שפת SQL כתבה: זהבה יעקובסון ליווי מקצועי : ארז קלר
Lab 5: Subqueries CISB224 02A, 02B Semester I, 2009/2010
Aggregations Various Aggregation Functions GROUP BY HAVING.
Introduction To Structured Query Language (SQL)
Rob Gleasure robgleasure.com
SQL Introduction Standard language for querying and manipulating data
Using Subqueries to Solve Queries
M1G Introduction to Database Development
Chapter 7 Using SQL in Applications
Subqueries Schedule: Timing Topic 25 minutes Lecture
Lab 4: Displaying Data from Multiple Tables
Projecting output in MySql
Advanced Joins IN ( ) Expression Subqueries with IN ( ) Expression
Views Views CREATE VIEW AS Uses for Views.
Rob Gleasure robgleasure.com
Rob Gleasure robgleasure.com
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
Pivoting and Grouping Sets
Lab 2: Retrieving Data from the Database
Presentation transcript:

CISB224 01A, 01B, 02A, 02B CCSB244 01A, 01B Semester I, 2007/2008 Lab 6: Subqueries CISB224 01A, 01B, 02A, 02B CCSB244 01A, 01B Semester I, 2007/2008 College of Information Technology, Universiti Tenaga Nasional

Introduction to Subqueries Suppose that you: Want to know all the other products supplied by the same company that supplies Outback Lager. Don’t know who supplies Outback Lager. 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 who the supplier is by executing: SELECT SupplierID FROM Products WHERE ProductName = ‘Outback Lager’ 7 College of Information Technology, Universiti Tenaga Nasional

Introduction to Subqueries – cont. Solution 1 – cont. Then, use the Supplier ID obtained to display the other products: SELECT ProductName FROM Products WHERE SupplierID = 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. 7 College of Information Technology, Universiti Tenaga Nasional

Introduction to Subqueries – cont. Solution 2 Type out the query that solves the main problem. SELECT ProductName FROM Products WHERE SupplierID = 7 College of Information Technology, Universiti Tenaga Nasional

Introduction to Subqueries – cont. Solution 2 – cont. At the search condition, delete the Supplier ID value and type in a pair of parentheses. SELECT ProductName FROM Products WHERE SupplierID = () 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 ProductName FROM Products WHERE SupplierID = ( SELECT SupplierID FROM Products WHERE ProductName = ‘Outback Lager’ ) 7 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 superior as Michael Suyama. SELECT LastName, ReportsTo FROM Employees WHERE ReportsTo = ( SELECT ReportsTo WHERE LastName = ‘Suyama’) College of Information Technology, Universiti Tenaga Nasional

Example of Subquery in HAVING Display the number of products supplied by each supplier, but only for suppliers who supply products more than or as many as Supplier 7. Also display the supplier’s company name. College of Information Technology, Universiti Tenaga Nasional

Example of Subquery in HAVING – cont. SELECTS.SupplierID, CompanyName, Count(ProductID) ‘No. of Products’ FROM Products P JOIN Suppliers S ON P.SupplierID = S.SupplierID GROUP BY S.SupplierID, CompanyName HAVING Count(ProductID) >= (SELECT Count(ProductID) FROM Products WHERE SupplierID = 7) 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 North America. SELECT CompanyName, Country FROM Customers WHERE Country = ‘USA’ OR Country = ‘Canada’ OR Country = ‘Mexico’ Country IN (‘USA’, ‘Canada’, ‘Mexico’ College of Information Technology, Universiti Tenaga Nasional

Using More Than One Search Conditions with AND or OR – cont. Display products that are under the Beverages and Seafood category and are not discontinued. SELECT ProductName FROM Products WHERE Discontinued = 0 AND CategoryID IN ( SELECT CategoryID FROM Categories WHERE CategoryName IN (‘Beverages’, ‘Seafood’)) College of Information Technology, Universiti Tenaga Nasional