SubQueries Part III.

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

© Abdou Illia MIS Spring 2014
© 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.
 Database is SQL1.mdb ◦ import using MySQL Migration Toolkit 
DatabaseDatabase cs453 Lab8 1 Ins.Ebtesam AL-Etowi.
SQL Sub (or Nested ) Query. Examples Q: Find students whose GPA is below the average. –The criteria itself requires a SQL statement. –SELECT * FROM student.
Database Programming Sections 5 & 6 – Group functions, COUNT, DISTINCT, NVL, GROUP BY, HAVING clauses, Subqueries.
Introduction to Oracle9i: SQL1 Subqueries. Introduction to Oracle9i: SQL2 Chapter Objectives Determine when it is appropriate to use a subquery Identify.
CSEN 5314 Quiz What type of join is needed when you wish to include rows that do not have matching values? A. Equi-joinB. Natural join C. Outer.
6 Copyright © 2004, Oracle. All rights reserved. Using Subqueries to Solve Queries.
6 Copyright © Oracle Corporation, All rights reserved. Subqueries.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Introduction to Databases Chapter 7: Data Access and Manipulation.
SQL Joins Oracle and ANSI Standard SQL Lecture 6.
Chapter 9 Joining Data from Multiple Tables
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
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.
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.
Using Special Operators (LIKE and IN)
6 Copyright © 2004, Oracle. All rights reserved. Using Subqueries to Solve Queries.
Week 10 Quiz 9 Answers Group 28 Christine Hallstrom Deena Phadnis.
Chapter 4 Multiple-Table Queries
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining Multiple Tables.
Unit 4 Queries and Joins. Key Concepts Using the SELECT statement Statement clauses Subqueries Multiple table statements Using table pseudonyms Inner.
Joins, views, and subqueries CMSC 461 Michael Wilson.
I NTRODUCTION TO SQL II. T ABLE JOINS A simple SQL select statement is one that selects one or more columns from any single table There are two types.
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.
Programming in R SQL in R. Running SQL in R In this session I will show you how to: Run basic SQL commands within R.
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
SQL Select Statement IST359.
1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.
Hassan Tariq MULTIPLE TABLES: SQL provides a convenient operation to retrieve information from multiple tables.SQL provides a convenient operation to.
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
1 SQL – IV Grouping data from tables in SQL –The concept of grouping –GROUP BY clause –HAVING Clause –Determining whether values are unique –Group by using.
Subqueries.
SQL Miscellaneous Topics. Views A database view is: – a virtual or logical table based on a query. – a stored query. CREATE VIEW viewname AS query; –CREATE.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
Chapter 7 Subqueries. Chapter Objectives  Determine when it is appropriate to use a subquery  Identify which clauses can contain subqueries  Distinguish.
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.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Chapter 11 – Data Manipulation: Relational Algebra and SQL1 Unit 9: Data Manipulation: Relational Algebra and SQL IT238: Data Modeling and Database Design.
IFS180 Intro. to Data Management Chapter 10 - Unions.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
MySQL Subquery Source: Dev.MySql.com
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
Multiple Table Queries
02 | Advanced SELECT Statements
Subqueries Schedule: Timing Topic 25 minutes Lecture
Database Systems: Design, Implementation, and Management Tenth Edition
SQL – Subqueries.
06 | Using Subqueries and APPLY
David M. Kroenke and David J
Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
JOINs JOINs can be used to combine tables A CROSS JOIN B
SQL – Entire Select.
Chapter 4 Summary Query.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Subqueries Schedule: Timing Topic 25 minutes Lecture
Database Management System
Subqueries.
Database Systems: Design, Implementation, and Management Tenth Edition
Subqueries Schedule: Timing Topic 25 minutes Lecture
SELECT from Multiple Tables
Table Joins Part II.
Presentation transcript:

SubQueries Part III

Subquery A subquery is a select statement (inner query) nested within another select statement (outer query) A subquery is always enclosed within parentheses Non-Correlated Subquery Allows each row of an outer query to be compared to the results of the inner query Correlated Subquery A subquery that references 1 or more columns from the outer query Executes every time a row is selected by the outer query

Non-Correlated Subquery SELECT ti.csu_id ,ti.name ,ti.term_gpa FROM csus_term_info_eot_cur ti WHERE ti.term_gpa > (SELECT AVG(tx.term_gpa) FROM csus_term_info_eot_cur tx) ORDER BY ti.term_gpa DESC

Correlated Subquery SELECT c.id CSU_ID ,c.name Name ,c.course_reference_number CRN FROM csus_section_info_cur c JOIN csus_student_attribute a ON a.person_uid = c.person_uid AND a.academic_period = c.academic_period WHERE a.student_attribute = 'ASHS' AND NOT EXISTS (SELECT ‘X' FROM schedule_attribute s WHERE s.course_reference_number = c.course_reference_number AND s.academic_period = c.academic_period AND s.schedule_attribute IN ('DHAN', 'DHHS', 'DHHY', 'DHRI')) ORDER BY Name

Inline View A subquery defined in the FROM clause The subquery must be named SELECT a_column a_col FROM z_table JOIN (SELECT b_column b_col FROM y_table) inline_view ON inline_view.b_col = a_col After it has been defined, an inline view is treated as if it were another joined table

Example Inline View SELECT x1.ssbxlst_term_code AS term, MAX(xs1.ssrxlst_crn) AS crn, s.ssbsect_subj_code AS subj, s.ssbsect_crse_numb AS crse, s.ssbsect_seq_numb AS sect, x1.ssbxlst_xlst_group AS xlist, summary.max_enrl AS seat_sum, x1.ssbxlst_max_enrl AS xlist_maxseat, s.ssbsect_camp_code AS campus FROM ssbxlst x1 JOIN ssrxlst xs1 ON xs1.ssrxlst_term_code = x1.ssbxlst_term_code AND xs1.ssrxlst_xlst_group = x1.ssbxlst_xlst_group JOIN ssbsect s ON s.ssbsect_term_code = xs1.ssrxlst_term_code AND s.ssbsect_crn = xs1.ssrxlst_crn LEFT OUTER JOIN (SELECT x.ssbxlst_term_code AS term, x.ssbxlst_xlst_group AS grp, SUM(s.ssbsect_max_enrl) AS max_enrl FROM ssbxlst x ON s.ssbsect_term_code = x.ssbxlst_term_code AND s.ssbsect_crn IN (SELECT xs.ssrxlst_crn FROM ssrxlst xs WHERE xs.ssrxlst_term_code = x.ssbxlst_term_code AND xs.ssrxlst_xlst_group = x.ssbxlst_xlst_group) GROUP BY x.ssbxlst_term_code ,x.ssbxlst_xlst_group) summary ON summary.term = x1.ssbxlst_term_code AND summary.grp = x1.ssbxlst_xlst_group List of cross listed courses where the cross list max enrollment does not equal the sum of the max enrollment for each section.

Example Inline View (cont) WHERE x1.ssbxlst_term_code >= csus_f_cur_term() AND x1.ssbxlst_max_enrl != summary.max_enrl GROUP BY x1.ssbxlst_term_code ,s.ssbsect_subj_code ,s.ssbsect_crse_numb ,s.ssbsect_seq_numb ,x1.ssbxlst_xlst_group ,summary.max_enrl ,x1.ssbxlst_max_enrl ,s.ssbsect_camp_code ORDER BY term ,xlist ,subj ,crse ,sect

Questions?