Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 http://www.cse.msu.edu/~cse103 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.

2 http://www.cse.msu.edu/~cse103 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.

3 http://www.cse.msu.edu/~cse103 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.

4 http://www.cse.msu.edu/~cse103 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]

5 http://www.cse.msu.edu/~cse103 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

6 http://www.cse.msu.edu/~cse103 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)

7 http://www.cse.msu.edu/~cse103 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]

8 http://www.cse.msu.edu/~cse103 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

9 http://www.cse.msu.edu/~cse103 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

10 http://www.cse.msu.edu/~cse103 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.

11 http://www.cse.msu.edu/~cse103 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

12 http://www.cse.msu.edu/~cse103 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


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

Similar presentations


Ads by Google