SUBQUERIES. ACTIVITY 4-1 Create a database called Subexamples Start a logfile called ACT4-1 Save the subtext.txt file – Copy the contents of the file.

Slides:



Advertisements
Similar presentations
SUBQUERIES. ACTIVITY 4-1 Create a database called Subexamples Start a logfile called ACT4-1 Save the subtext.txt file – Copy the contents of the file.
Advertisements

© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
Chapter 4 Joining Multiple Tables
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.
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
 Database is SQL1.mdb ◦ import using MySQL Migration Toolkit 
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 7: Subqueries and Set Operations.
Introduction to Oracle9i: SQL1 Subqueries. Introduction to Oracle9i: SQL2 Chapter Objectives Determine when it is appropriate to use a subquery Identify.
Introduction to Structured Query Language (SQL)
Introduction to Structured Query Language (SQL)
Concepts of Database Management Sixth Edition
SQL for Data Retrieval. Save your SQL Scripts When working with SQL Management Studio, you should keep saving your scripts as a.sql file to somewhere.
Objectives After completing this lesson, you should be able to do the following: Define subqueries Describe the types of problems that the subqueries.
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 8: Subqueries.
Introduction to SQL J.-S. Chou Assistant Professor.
Copyright 2007, Paradigm Publishing Inc. BACKNEXTEND 3-1 LINKS TO OBJECTIVES Save a Filter as a Query Save a Filter as a Query Parameter Query Inner, Left,
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
Introduction to Databases Chapter 7: Data Access and Manipulation.
Chapter 9 Joining Data from Multiple Tables
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.
CSC 405: Web Application And Engineering II7.1 Database Programming with SQL Aggregation and grouping with GROUP BY Aggregation and grouping with GROUP.
NMED 3850 A Advanced Online Design January 12, 2010 V. Mahadevan.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Concepts of Database Management Seventh Edition
SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.
1 Agenda – 10/24/2013 Answer questions from lab on 10/22. Present SQL View database object. Present SQL UNION statement.
CS146 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Intro to SQL Management Studio. Please Be Sure!! Make sure that your access is read only. If it isn’t, you have the potential to change data within your.
1 Multiple Table Queries. 2 Objectives  Retrieve data from more than one table by joining tables  Using IN and EXISTS to query multiple tables  Nested.
STRUCTURED QUERY LANGUAGE SQL-II IST 210 Organization of Data IST210 1.
SQL for Data Retrieval. Save your SQL Scripts When working with SQL Management Studio, you should keep saving your scripts as a.sql file to somewhere.
Chapter 12 Subqueries and Merge Statements
Structured Query Language
Hassan Tariq MULTIPLE TABLES: SQL provides a convenient operation to retrieve information from multiple tables.SQL provides a convenient operation to.
SQL: Sub-queries Single-value sub-queries Single-column sub-queries Sub-queries that produce tables Correlated sub-queries D. Christozov / G.Tuparov INF.
1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Access 2010 by Robert Grauer, Keith Mast, and Mary Anne.
Subqueries.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
1 Chapter 3 Single Table Queries. 2 Simple Queries Query - a question represented in a way that the DBMS can understand Basic format SELECT-FROM Optional.
Chapter 7 Subqueries. Chapter Objectives  Determine when it is appropriate to use a subquery  Identify which clauses can contain subqueries  Distinguish.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
Slide 1 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla In this session, you will learn to: Query data by using joins Query.
Copyright © 2016 Pearson Education, Inc. CHAPTER 7: ADVANCED SQL (PART I) Modern Database Management 12 th Edition Jeff Hoffer, Ramesh Venkataraman, Heikki.
BTM 382 Database Management Chapter 8 Advanced SQL Chitu Okoli Associate Professor in Business Technology Management John Molson School of Business, Concordia.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Using Subqueries to Solve Queries
Databases.
SQL – Part 2.
MySQL Subquery Source: Dev.MySql.com
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
Subqueries Schedule: Timing Topic 25 minutes Lecture
Using the Set Operators
Using Subqueries to Solve Queries
Lecture#7: Fun with SQL (Part 2)
David M. Kroenke and David J
Using the Set Operators
Structured Query Language (II) (結構化查詢語言)
Structured Query Language
Using Subqueries to Solve Queries
SQL Subquery.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Using Subqueries to Solve Queries
Subqueries.
Using Subqueries to Solve Queries
Using the Set Operators
Subqueries Schedule: Timing Topic 25 minutes Lecture
Subqueries Schedule: Timing Topic 25 minutes Lecture
Presentation transcript:

SUBQUERIES

ACTIVITY 4-1 Create a database called Subexamples Start a logfile called ACT4-1 Save the subtext.txt file – Copy the contents of the file – Paste into MYSQL – This will create the tables we will use for all Activities this week Make sure that all tables have been created and that there is data in each – SERVICES, CLIENTS, BRANCHES, BRANCHES_SERVICES

List all clients with branch offices in California only SELECT cname, bdesc, bloc FROM clients, branches WHERE clients.cid = branches.cid AND branches.bloc = 'CA'; | cname | bdesc |bloc| | JV Real Estate | Corporate HQ |CA | | Rabbit Foods Inc |Branch Office (West)|CA | | Sharp Eyes Detective|Head Office |CA| rows in set (0.00 sec)

List of all the branch offices belonging to "Rabbit Foods Inc". You could run two SELECT statements: mysql> SELECT cid FROM clients WHERE cname = 'Rabbit Foods Inc'; | cid | | 104 | row in set (0.17 sec) mysql> SELECT bdesc FROM branches WHERE cid = 104; | bdesc | | Branch Office (East) | | Branch Office (West) | rows in set (0.17 sec)

OR run one subquery: mysql> SELECT bdesc FROM branches WHERE cid = (SELECT cid FROM clients WHERE cname = 'Rabbit Foods Inc'); | bdesc | | Branch Office (East) | | Branch Office (West) | rows in set (0.22 sec)

A subquery makes it possible to combine two or more queries into a single statement Uses the results of one query in the conditional clause of the other Subqueries are usually regular SELECT statements, and are separated from their parent query by parentheses

Subqueries are usually preceded by a conditional WHERE clause, which can contain any of the following comparison and logical operators = values are equal <> values are unequal = value on left is greater than or equal to value on right value on left is greater than value on right BETWEEN value on left lies between values on right NOT logical NOT AND logical AND OR logical OR

List of all those customers with exactly two branch offices. First, you need to figure out a way to obtain the number of branch offices per customer SELECT cid, COUNT(bid) FROM branches GROUP BY cid; | cid | count(bid) | | 101 | 3 | | 103 | 3 | | 104 | 2 | | 110 | 1 | rows in set (0.27 sec)

You can nest subqueries to any depth, so long as the basic rules above are followed. List the services used by Sharp Eyes Detective Agency SELECT sname FROM services WHERE sid = (SELECT sid FROM branches_services WHERE bid = (SELECT bid FROM branches WHERE cid = (SELECT cid FROM clients WHERE cname = 'Sharp Eyes Detective Agency'))); | sname | | Accounting | row in set (0.28 sec)

Then filter out those with just two offices with a HAVING clause SELECT cid, COUNT(bid) FROM branches GROUP BY cid HAVING COUNT(bid) = 2; | cid | count(bid) | | 104 | 2 | row in set (0.16 sec)

Then hand the client ID over to the "clients" table in order to get the client name SELECT cname FROM clients WHERE cid = 104; | cname | | Rabbit Foods Inc | row in set (0.22 sec)

This subquery will take care of the three steps: SELECT cname FROM clients WHERE cid = (SELECT cid FROM branches GROUP BY cid HAVING COUNT(bid) = 2); | cname | | Rabbit Foods Inc | row in set (0.28 sec)

The inner query is executed first - this query takes care of grouping the branches by customer ID and counting the number of records (branch offices) in each group. Those customers which have exactly two branch offices can easily be filtered out with a HAVING clause The corresponding customer IDs are returned to the main query, which then maps the IDs into the customers table and returns the corresponding customer name.

Select all those customers using the service with the maximum service fee SELECT cname, bdesc FROM clients, branches, branches_services, services WHERE services.sid = branches_services.sid AND clients.cid = branches.cid AND branches.bid = branches_services.bid AND sfee = (SELECT MAX(sfee) FROM services); | cname | bdesc | | JV Real Estate | Customer Grievances Department | row in set (0.01 sec)

HAVING CLAUSE Which sites are using more than 50% of all available services SELECT bid FROM branches_services GROUP BY bid HAVING COUNT(sid) > (SELECT COUNT(*) FROM services)/2; | bid | | 1011 | row in set (0.22 sec)

Which sites are using more than 50% of all available services and get the branch name and customer name as well SELECT c.cid, c.cname, b.bid, b.bdesc FROM clients AS c, branches AS b, branches_services AS bs WHERE c.cid = b.cid AND b.bid = bs.bid GROUP BY bs.bid HAVING COUNT(bs.sid) > (SELECT COUNT(*) FROM services)/2; | cid | cname | bid | bdesc | | 101|JV Real Estate | 1011 | Corporate HQ | row in set (0.28 sec)

List all clients using all available services across their branch offices SELECT clients.cname FROM clients, branches, branches_services WHERE branches.bid = branches_services.bid AND branches.cid = clients.cid GROUP BY clients.cid HAVING COUNT(sid) = (SELECT COUNT(*) FROM services); | cname | | JV Real Estate | row in set (0.01 sec)

IN OPERATOR A subquery can also return a single column of multiple values HOW? – By using the IN operator makes it possible to test if a particular value exists in the result set, and perform the outer query if the test is successful

List of all services being used by Branch ID 1031 SELECT sid FROM branches_services WHERE bid = 1031; | sid | | 2 | | 3 | | 4 | rows in set (0.16 sec)

And then: SELECT sname FROM services WHERE sid = 2; | sname | | Recruitment | row in set (0.28 sec) SELECT sname FROM services WHERE sid = 3; | sname | | Data Management | row in set (0.17 sec) SELECT sname FROM services WHERE sid = 4; | sname | | Administration | row in set (0.11 sec)

Using the IN operator in a subquery is more efficient for this: SELECT sname FROM services WHERE sid IN (SELECT sid FROM branches_services WHERE bid = 1031); | sname | | Recruitment | | Data Management | | Administration | rows in set (0.27 sec)

List of all branches using the "Accounting" service (service ID 1) SELECT bdesc FROM branches WHERE bid IN (SELECT bid FROM branches_services WHERE sid = 1); | bdesc | | Corporate HQ | | Accounting Department | | Branch Office (East) | | Branch Office (West) | | Head Office | rows in set (0.17 sec)

Now add the customer name for each branch as well SELECT cname, bdesc FROM branches, clients WHERE branches.bid IN (SELECT bid FROM branches_services WHERE sid = 1) AND clients.cid = branches.cid; | cname | bdesc | | JV Real Estate | Corporate HQ | | JV Real Estate | Accounting Department | | Rabbit Foods Inc | Branch Office (East) | | Rabbit Foods Inc | Branch Office (West) | | Sharp Eyes Detective Agency | Head Office | rows in set (0.16 sec)

Now just show the customer list (DISTINCT) SELECT DISTINCT cname FROM branches, clients WHERE branches.bid IN (SELECT bid FROM branches_services WHERE sid = 1) AND clients.cid = branches.cid; | cname | | JV Real Estate | | Rabbit Foods Inc | | Sharp Eyes Detective Agency | rows in set (0.17 sec)

logfile for your Activtiy 4-1 submission. Review your logfile as Activity 4-2 will continue with subqueries.