Day 13, Slide 1 U:/msu/course/cse/103 CSE 103 Students: Review INNER and OUTER JOINs, Subqueries. Others: Please save your.

Slides:



Advertisements
Similar presentations
Advanced SQL (part 1) CS263 Lecture 7.
Advertisements

© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
 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.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Chapter 6 Set Functions.
NMED 3850 A Advanced Online Design February 25, 2010 V. Mahadevan.
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 7: Subqueries and Set Operations.
1 CSE Students: Please do not log in yet. Check-in with Brian in the back. Review Days 3 and 4 in the book. Others: Please save your work and logout.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 6: Set Functions.
U:/msu/course/cse/103 Day 16, Slide 1 CSE 103 Day 16 Students: Read Day 10. If you are logged in, add records to your contacts.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 5: Subqueries and Set Operations.
U:/msu/course/cse/103 Day 23, Slide 1 Review of Day 22 What query did you use to search for an actor by name? –Return matches.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 4: Joins Part II.
Introduction to Structured Query Language (SQL)
SQL for Data Retrieval. Running Example IST2102 Data Preparation Login to SQL server using your account Download three SQL script files from wiki page.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
Subqueries. So far when data has been filtered the filter has been known and simply added to the Where clause but often you don’t know what the filter.
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 8: Subqueries.
U:/msu/course/cse/103 Day 25, Slide 1 Back-up PHP Files If you have not yet passed the 3.0 BT, make back-up copies of ALL.
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,
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
U:/msu/course/cse/103 Day 10, Slide 1 CSE 103 Students: Your BTs have been graded. See Erica or Jo with questions or stay.
U:/msu/course/cse/103 Day 04, Slide 1 CSE students: Do not log in yet. Computers are SLOW today! Review days 13.
McGraw-Hill Technology Education © 2004 by the McGraw-Hill Companies, Inc. All rights reserved. Office Access 2003 Lab 3 Analyzing Data and Creating Reports.
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.
1 Agenda – 03/25/2014 Login to SQL Server 2012 Management Studio. Answer questions about HW#7 – display answers. Exam is 4/1/2014. It will be in the lab.
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-column subqueries, Multiple-row Subqueries, Correlated Subqueries 11/2/10,
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,
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
U:/msu/course/cse/103 Day 06, Slide 1 CSE students: Do not log in yet. Review Day 6 in your textbook. Think about.
SQL for Data Retrieval. Running Example IST2102 Data Preparation Login to SQL server using your account Select your database – Your database name is.
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 Chapter 10 Joins and Subqueries. 2 Joins & Subqueries Joins – Methods to combine data from multiple tables – Optimizer information can be limited based.
Subqueries Steve Perry 1.
Chapter 12 Subqueries and Merge Statements
U:/msu/course/cse/103 Day 18, Slide 1 CSE 103 Day 18 If you are not enrolled in CSE 103, please save and log out by 10:10.
U:/msu/course/cse/103 Day 03, Slide 1 CSE 103 Day 15: Database Design Students: –Read Day 2 in your textbook –Your BT feedback.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Aliya Farheen October 29,2015.
U:/msu/course/cse/103 Day 21, Slide 1 CSE 103 Makeups –If you didn’t take one over the weekend, take one TUESDAY or WEDNESDAY!
U:/msu/course/cse/103 Day 08, Slide 1 CSE 103 Students: –Review days 7 and 8 if you need to go over relationships and INNER.
DAY 18: MICROSOFT ACCESS – CHAPTER 3 CONTD. Akhila Kondai October 21, 2013.
U:/msu/course/cse/103 Day 12, Slide 1 CSE 103 Students: Read Day 18 and learn about subqueries Others: Please save your.
# 1# 1 QueriesQueries How do we ask questions of the data? What is SELECT? What is FROM? What is WHERE? What is a calculated field? Spring 2010 CS105.
Relational Databases. SQL Sub-queries: queries within queries  So far when data has been filtered the filter has been known and simply added to the Where.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-row Subqueries, Correlated Subqueries.
U:/msu/course/cse/103 Day 08, Slide 1 Debrief Homework What problems arose in trying to import the data from Classical_Music.xls?
Day 5 - More Complexity With Queries Explanation of JOIN & Examples Explanation of JOIN & Examples Explanation & Examples of Aggregation Explanation &
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
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.
MICROSOFT ACCESS – CHAPTER 3 CONTD. Sravanthi Lakkimsetty Mar 09, 2016.
BTM 382 Database Management Chapter 8 Advanced SQL Chitu Okoli Associate Professor in Business Technology Management John Molson School of Business, Concordia.
Lecture 7: Subqueries Tarik Booker California State University, Los Angeles.
CSC314 DAY 9 Intermediate SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall USING AND DEFINING VIEWS  Views provide users controlled.
Using Subqueries to Solve Queries
MySQL Subquery Source: Dev.MySql.com
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
CSE 103 Day 15: Database Design
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Using Relational Databases and SQL
Using Subqueries to Solve Queries
CSE Students: Please do not log in yet.
Using Subqueries to Solve Queries
SQL Subquery.
Using Subqueries to Solve Queries
Using Subqueries to Solve Queries
Subqueries Schedule: Timing Topic 25 minutes Lecture
Presentation transcript:

Day 13, Slide 1 U:/msu/course/cse/103 CSE 103 Students: Review INNER and OUTER JOINs, Subqueries. Others: Please save your work and log out by 10:10.

Day 13, Slide 2 U:/msu/course/cse/103 Day 12 Review What kinds of questions can we solve with subqueries we couldn't otherwise? What kinds of questions can we solve with self-JOINs? How can we use the same table more than once in the same query?

Day 13, Slide 3 U:/msu/course/cse/103 Nested SELECTs in MySQL Nested SELECTs in MySQL MySQL does not (currently) allow views (saved queries) In effect, using a saved query is the same as embedding that entire SELECT statement in the parent (calling) query All RDBMS support this idea of using nested SELECTs (sometimes called subqueries)

Day 13, Slide 4 U:/msu/course/cse/103 Nested SELECT example We can use nested queries as an alternate to JOINs: –SELECT BirthDate FROM tbl_Births WHERE PersonID = (SELECT PersonID FROM tbl_People WHERE FirstName = "Charlton" AND LastName = "Heston") –Same as: SELECT BirthDate FROM tbl_Births AS b INNER JOIN tbl_People AS p ON b.PersonID = p.PersonID WHERE FirstName = "Charlton" AND LastName = "Heston" Subqueries must be completely enclosed in parantheses ()

Day 13, Slide 5 U:/msu/course/cse/103 More on nested queries If the subquery returns exactly one row and one field, you can test with =, or >, or, or <, etc. (for example finding people of "above average height") If the subquery returns multiple rows and/or columns, it is best to use a virtual table and a JOIN (more on this later)

Day 13, Slide 6 U:/msu/course/cse/103 Basic nested queries exercises Use nested queries to find these answers: –Find all people who have above average height [8946] –Find the name of the person or people who have the earliest birthday in 1970 [6] –Find all movies made in the same year as "Harold and Maude" [4106]

Day 13, Slide 7 U:/msu/course/cse/103 Subqueries to make virtual tables: Using SELECT in the FROM clause Recall: music DB - we found albums with total runtimes greater than average –First had to find total runtimes, then average –Used stored queries We need a calculation based on another calculation, so we nest queries We have to SELECT … FROM a virtual table we create with a subquery (see next)

Day 13, Slide 8 U:/msu/course/cse/103 Nested queries in FROM example Problem: find people who are older than the average age of all those in our db –Have to find ages, then average ages, then… –SELECT FirstName, LastName FROM tbl_People AS p INNER JOIN tbl_Births AS b ON p.PersonID = b.PersonID WHERE (YEAR(NOW()) - YEAR(BirthDate)) > (SELECT AVG(Age) FROM (SELECT YEAR(NOW()) - YEAR(BirthDate) AS Age FROM tbl_Births) AS vr_tbl_1 ) AS Age FROM tbl_Births) AS vr_tbl_1 )

Day 13, Slide 9 U:/msu/course/cse/103 Nested queries in FROM clause Example analysis Nested queries in FROM clause Example analysis Yellow text (outermost query): selects final fields for display and restriction: ages have to be greater than average –Don't know average yet; write subqueries to find it and compare to that –Could test with say, > 50 to verify syntax Blue text (1st subquery): selects average age for comparison w/outer query –It returns AVG(Age) but we have no field called Age –AVG() only works on a field; not on something like an entire table (even if the table has only one field, we have to specify the field) –Its FROM line has a subquery… Green text (second subquery): selects everyone's Ages –We create calculated field called Age for use by outer query –In a FROM line, the subquery returns a (virtual) data table to be used –All tables in a FROM line need a name… Red text: Provides the virtual table with a name, just like any alias –The alias goes outside the () around the subquery –Alias name only matters if you would need to JOIN, etc. with it

Day 13, Slide 10 U:/msu/course/cse/103 Nicolas Cage Example Who has costarred with Mr. Cage? –Step 1: Find Cage's ActorID –Step 2: Use that ActorID to find the MovieIDs he's been in –Step 3: Use those MovieIDs to find the ActorIDs in those movies –Step 4: Use those ActorIDs to find the names of the other actors Can solve using self-JOINs and/or subqueries –Mix & match as you like; Some solutions will be faster SELF-JOINs generally are very slow –Use subqueries instead of repeating a table, where possible

Day 13, Slide 11 U:/msu/course/cse/103 Nicolas Cage, continued One possible solution (there are many): –SELECT p1.FirstName, p1.LastName FROM ((tbl_People AS p1 INNER JOIN tbl_Roles AS r1 ON p1.PersonID = r1.ActorID) INNER JOIN (SELECT ActorID, MovieID FROM tbl_Roles AS r2 INNER JOIN tbl_People AS p2 ON r2.ActorID = p2.PersonID WHERE p2.FirstName = 'Nicolas' AND p2.LastName = 'Cage') AS vr_tbl_1 ON r1.MovieID = vr_tbl_1.MovieID ORDER BY LastName, FirstName Blue text: subquery to find NC's Movies –See the use of INNER JOIN with the virtual table produced by the subquery (red text) For further thought: without restricting another firstname/lastname, make Nicolas Cage not star with himself.

Day 13, Slide 12 U:/msu/course/cse/103 Nested queries in FROM exercise Find all the genres which had a higher- than-average number of movies made in 2003 (compared only to the average per genre for 2003 movies). Show the GenreName and that Number. –E.g., if the average Genre in '03 has 300 movies made, and Action has 500 while Comedy has 200, your query should return Action, 500

Day 13, Slide 13 U:/msu/course/cse/103 Upcoming Bridge Task Everyone should come; get next BT you haven't passed –This is 5 th opportunity out of 12 –If you take & pass the 1.5, you have 7 more attempts to pass 3 BTs, but if you fail the 1.5, you have only 7 attempts for 4 BTs (less than 2 tries each) Review text, Web and homework –Get help from helprooms (best if you missed entire days), office hours (good for specific questions), (specific questions after hours) If taking 2.0, must have C&U database from passed 1.5 Bring any books, notes, back-up disk, etc. & a photo ID Last BT opportunity before Spring Break!

Day 13, Slide 14 U:/msu/course/cse/103 Bridge Task 2.0 Covers materials primarily from Days –Advanced queries of all sorts –Functions (Day 10), Aggregates (Day 11), Stored queries/views (Day 12), [Nested] Subqueries (Day 13) –Also be sure you understand INNER/OUTER JOIN and Calculated fields, as many advanced problems rely on those more basic concepts Review notes, homework, classwork –Notes are on the Web pages