U:/msu/course/cse/103 Day 12, Slide 1 CSE 103 Students: Read Day 18 and learn about subqueries Others: Please save your.

Slides:



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

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.
4d. Structured Query Language – JOIN Operation Lingma Acheson Department of Computer and Information Science IUPUI CSCI N207 Data Analysis with Spreadsheets.
Start First step Create a new blank database Create a database using the option that will enable you to build your database using pre-set options. Save.
Create a new blank database First step SUBMITTry again.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 7: Aggregates.
Introduction to Structured Query Language (SQL)
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.
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.
Databases Week 7 LBSC 690 Information Technology.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 7: Aggregates.
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.
1.1 CAS CS 460/660 Introduction to Database Systems File Organization Slides from UC Berkeley.
Introduction to Structured Query Language (SQL)
Microsoft Access 2010 Chapter 7 Using SQL.
Advanced Tables Lesson 9. Objectives Creating a Custom Table When a table template doesn’t suit your needs, you can create a custom table in Design view.
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.
Working with Numbers in Alice - Converting to integers and to strings - Rounding numbers. - Truncating Numbers Samantha Huerta under the direction of Professor.
Microsoft Access 2010 Chapter 7 Using SQL. Change the font or font size for SQL queries Create SQL queries Include fields in SQL queries Include simple.
With Microsoft Office 2007 Introductory© 2008 Pearson Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Office 2007 Introductory.
Copyright © 2014 Pearson Education, Inc. 1 CHAPTER 7: ADVANCED SQL Essentials of Database Management Jeffrey A. Hoffer, Heikki Topi, V. Ramesh.
Moodle (Course Management Systems). Assignments 1 Assignments are a refreshingly simple method for collecting student work. They are a simple and flexible.
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.
XP New Perspectives on Microsoft Office Access 2003 Tutorial 9 1 Microsoft Office Access 2003 Tutorial 9 – Using Action Queries, and Defining Table Relationships.
1 Intro to JOINs SQL INNER JOIN SQL OUTER JOIN SQL FULL JOIN SQL CROSS JOIN Intro to VIEWs Simple VIEWs Considerations about VIEWs VIEWs as filters ALTER.
Complete theory from last lesson… Put today’s date in the front of your book Read pages 46 and 47 of the textbook…
Databases Week 5 LBSC 690 Information Technology.
U:/msu/course/cse/103 Day 07, Slide 1 Day 15 Review What datatype would be an appropriate choice for: –A cell phone number.
Using Special Operators (LIKE and IN)
U:/msu/course/cse/103 Day 06, Slide 1 CSE students: Do not log in yet. Review Day 6 in your textbook. Think about.
IS201 Agenda: 10/15/2013 Do form and report exercise. Identify general guidelines for form and report design. Discuss a few key points about reports in.
Views Lesson 7.
SESSION 3.1 This section covers using the query window in design view to create a query and sorting & filtering data while in a datasheet view. Microsoft.
Day 13, Slide 1 U:/msu/course/cse/103 CSE 103 Students: Review INNER and OUTER JOINs, Subqueries. Others: Please save your.
1 CSE 103 Section 002: M/W 10:20am-12:10pm 216 Berkey Hall Section 004: T/R 10:20am-12:10pm 1210 Anthony Hall.
Copyright 2008 McGraw-Hill Ryerson 1 TECHNOLOGY PLUG-IN T7 PROBLEM SOLVING USING ACCESS.
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 4: Joins Part II.
1 CSE 2337 Introduction to Data Management Access Book – Ch 1.
Database Applications – Microsoft Access Lesson 4 Working with Queries 36 Slides in Presentation.
Subqueries Steve Perry 1.
Instructor: Craig Duckett Lecture 03: Tuesday, April 14, 2015 SQL Sorting, Aggregates and Joining Tables 1.
Computer Science & Engineering 2111 Outer Joins 1CSE 2111 Lecture- Inner Vs. Outer Jioins.
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.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
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.
In order for Cardholders to enroll in the internet Payment Center tool, they need to copy and paste the following URL in the address bar:
T7-1 LEARNING OUTCOMES – ACCESS PROBLEM SOLVING 1.Describe the process of using the Simple Query Wizard using Access 2.Describe the process of using the.
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
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.
FINAL EXAM OVERVIEW Aliya Farheen
U:/msu/course/cse/103 Day 08, Slide 1 Debrief Homework What problems arose in trying to import the data from Classical_Music.xls?
Homework 2 Hints. General Tips Remember what FORM view you are in! – Design, form, and layout view TABLE views include: – Design and Datasheet view.
1 Taking Notes. 2 STOP! Have I checked all your Source cards yet? Do they have a yellow highlighter mark on them? If not, you need to finish your Source.
Day 5 - More Complexity With Queries Explanation of JOIN & Examples Explanation of JOIN & Examples Explanation & Examples of Aggregation Explanation &
Copyright © 2016 Pearson Education, Inc. CHAPTER 7: ADVANCED SQL (PART I) Modern Database Management 12 th Edition Jeff Hoffer, Ramesh Venkataraman, Heikki.
Access Queries and Forms. Access Queries Simple Queries  To create an Access query, don’t use the query wizard. Instead, create query in Design view.
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.
CSE 103 Day 15: Database Design
Multiple Table Queries
Dynamic Programming.
SQL – Entire Select.
CSE Students: Please do not log in yet.
Lab 3 and HRP259 Lab and Combining (with SQL)
Debrief Homework Import data from the text file CanadianUniversities06.txt into a temporary table. What is the primary key for this table? Construct an.
M1G Introduction to Database Development
Presentation transcript:

U:/msu/course/cse/103 Day 12, Slide 1 CSE 103 Students: Read Day 18 and learn about subqueries Others: Please save your work and log out by 10:10.

U:/msu/course/cse/103 Day 12, Slide 2 Subqueries What are subqueries? What are self-join queries? What are self-join queries? What questions can you answer with self- join queries and subqueries that you can't do with a simple query? NOTE: Other RDBMS call saved queries "views." The same ideas apply; you can use a view exactly like we will use saved queries today.

U:/msu/course/cse/103 Day 12, Slide 3 Creating a subquery To find all albums for which the MP3 total filesize is within 10 MB of the maximum album MP3 total filesize: –First, create a query that lists the total disk space consumed by each album Must return AlbumID and TotalFilesize Call the query qry_day12_AlbumSize_1.

U:/msu/course/cse/103 Day 12, Slide 4 Using a subquery Second, create qry_day12_MaxFilesize_2 that uses the previous query and finds the maximum total filesize for all albums. Finally, create qry_day12_LargeMP3s_3 that uses the two previous queries and tbl_Albums to produce the list of all album titles (and their TotalFilesize) that are within 10 MB of the maximum total filesize. [2 records]

U:/msu/course/cse/103 Day 12, Slide 5 Subqueries: Pro, Con, and Tips What are the advantages of using subqueries? What are the disadvantages of using subqueries? Tips for writing better subqueries: –At each step, only SELECT the minimum amount of data needed for the next step –Use IDs/Primary Keys instead of names; then in your last step, you can JOIN on those if you need names –Test each step as you write it

U:/msu/course/cse/103 Day 12, Slide 6 General tips on complicated queries 1. Determine what you need to SELECT –Are there any calculated or aggregate fields? Leave placeholders if you're not sure what to do and fix it later. –Are there intermediate calculations you need? Write subqueries, following these same steps for each. 2. List tables that fields you select come FROM –Use INNER / OUTER JOINs as needed 3. What conditions must apply to individual records? (WHERE) 4. If using aggregates, what property defines a set? (GROUP BY) 5. If using aggregates, what conditions must a set have? (HAVING) 6. How should the data be sorted? (ORDER BY)

U:/msu/course/cse/103 Day 12, Slide 7 Exercise on Subqueries Save successive queries in the sequence by increasing the X value. Find the filenames of the smallest and the biggest digital track from each album (display Album Titles, Filenames, and sizes) (qry_day12_extreme_digi_sizes_X) [50] Find which albums (& their number of tracks) have the shortest and the longest total runtime (display Album Titles, # tracks, and total runtimes) (qry_day12_long_and_short_albums_X) [2]

U:/msu/course/cse/103 Day 12, Slide 8 Self-JOINs Some problems ask to find "all X that have some property in common with a particular X" –e.g., find all artists who recorded an album with They Might Be Giants Questions of this nature require using the same table twice, JOINed to itself Using table aliases, we can do this simply

U:/msu/course/cse/103 Day 12, Slide 9 Self-JOIN example To find artists recording with TBMG: 1.We need TMBG's albums (JOIN Artists and PerformsIn). Use AlbumIDs for simplicity. – Notice we don't need tbl_Albums (the ID from Performs_In works just fine) 2.We need the ArtistIDs from these Albums (JOIN result of 1 with Performs_In) – Must be a new copy; the other Performs_In is restricted by the JOIN to tbl_Albums 3.We need names of these Artists (JOIN 2 with Artists) – Again, must be new copy; other Artists restricted by WHERE clause

U:/msu/course/cse/103 Day 12, Slide 10 Self-JOIN notes Access automatically uses table aliases _1, _2, etc. when you add a 2 nd (3 rd ) copy of a table. –Watch carefully when using Design View to restrict/select from the right copy Subqueries can be used to avoid using the same table more than once in the same step –Can help keep it cleaner, but harder to see big picture of what query is doing. If you don't want to include the "particular X" (e.g., TMBG records with TMBG), be sure to restrict appropriately.

U:/msu/course/cse/103 Day 12, Slide 11 Self-JOIN exercise Find all different pairs of Artists who have recorded on the same Label [28] –Note that [They Might Be Giants, Chicago Symphony Orchestra] is the same pair as [Chicago S O, They Might Be Giants], but DISTINCT won't help with that –Possible stepping stones on the way to the final list: (# of records) 161/160, 100, 94, 78, 56

U:/msu/course/cse/103 Day 12, Slide 12 Homework Homework Review EVERYTHING you know about JOINs and subqueries –Reread days 8, 11, 18 Finish any queries you didn't get done in class Some more practice ones are on the homework