CS122 Using Relational Databases and SQL

Slides:



Advertisements
Similar presentations
Multiple Table Queries
Advertisements

© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 3: Joins Part I.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 7: Subqueries and Set Operations.
DatabaseDatabase cs453 Lab8 1 Ins.Ebtesam AL-Etowi.
Subqueries 11. Objectives After completing this lesson, you should be able to do the following: Describe the types of problems that subqueries can solve.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 7: Aggregates.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 7: Subqueries and Set Operations.
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.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 2: Single-Table Selections.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 9: Data Manipulation Language.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 7: Aggregates.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 5: Subqueries and Set Operations.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 2: Single-Table Selections.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 4: Joins Part II.
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.
Xin  Syntax ◦ SELECT field1 AS title1, field2 AS title2,... ◦ FROM table1, table2 ◦ WHERE conditions  Make a query that returns all records.
Using Relational Databases and SQL John Hurley Department of Computer Science California State University, Los Angeles Lecture 3: Joins Part I.
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 6: Midterm Review.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 4: Intermediate.
Using Relational Databases and SQL John Hurley Department of Computer Science California State University, Los Angeles Lecture 2: Single-Table Selections.
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 4: Joins Part II.
Subqueries Steve Perry 1.
Subqueries.
The Student Registry Database Ian Van Houdt and Muna alfahad.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
Fall Lab 131 CS105 Lab 13 – Logical Operator Precedence and Joining Tables Announcements: MP 3 released Friday, 11/20 Honors project due: Tuesday,
CS122 Using Relational Databases and SQL Huiping Guo Department of Computer Science California State University, Los Angeles 2. Single Table Queries.
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.
CS 122: Lecture 3 Joins (Part 1) Tarik Booker CS 122 California State University, Los Angeles October 7, 2014.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
Lecture 7: Subqueries Tarik Booker California State University, Los Angeles.
Tarik Booker CS 122. What we will cover… Tables (review) SELECT statement DISTINCT, Calculated Columns FROM Single tables (for now…) WHERE Date clauses,
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Joins (Part II) Tarik Booker California State University, Los Angeles.
CS122 Using Relational Databases and SQL Huiping Guo Department of Computer Science California State University, Los Angeles 4. Subqueries and joins.
Using Subqueries to Solve Queries
CS3220 Web and Internet Programming More SQL
CS122 Using Relational Databases and SQL
Subqueries Schedule: Timing Topic 25 minutes Lecture
CS122 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
Using Subqueries to Solve Queries
Chapter 4: Intermediate SQL Joins
SQL – Subqueries.
CS122 Using Relational Databases and SQL
JOINS (Joinining multiple tables)
CSC 453 Database Systems Lecture
CS122 Using Relational Databases and SQL
Putting it back together
CS122 Using Relational Databases and SQL
CS4222 Principles of Database System
CMPT 354: Database System I
Using Subqueries to Solve Queries
CS122 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
Subqueries Schedule: Timing Topic 25 minutes Lecture
CS122 Using Relational Databases and SQL
Using Subqueries to Solve Queries
CS1222 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
Using Subqueries to Solve Queries
Subqueries Schedule: Timing Topic 25 minutes Lecture
Subqueries Schedule: Timing Topic 25 minutes Lecture
JOINS (Joinining multiple tables)
CS122 Using Relational Databases and SQL
Presentation transcript:

CS122 Using Relational Databases and SQL 10/29/2019 CS122 Using Relational Databases and SQL 4. Subqueries and joins Daniel Firpo Slides prepared by Randy Moss Department of Computer Science California State University, Los Angeles

Outline Subqueries Joining tables Subqueries in the WHERE clause 10/29/2019 Outline Subqueries Subqueries in the WHERE clause Subqueries with IN, ALL & ANY Joining tables Learn how SQL joins tables Join tables using an Equi Join Join tables using an Inner Join 4. Subqueries and joins CS1222-S2019

Sub-Queries A query within a query Sub-query placed in parentheses 10/29/2019 Sub-Queries A query within a query Sub-query placed in parentheses Sub-queries can be placed in WHERE clause HAVING clause SELECT clause FROM clause 4. Subqueries and joins CS1222-S2019

Sub-Queries examples (1) 10/29/2019 Sub-Queries examples (1) List the name of the oldest member Find the earliest birthday (1955-11-01) Select min(birthday) From Members Find the member whose birthday is ‘1955-11-01’ Select firstname, lastname Where birthday = ‘1955-11-01’ 4. Subqueries and joins CS1222-S2019

Sub-Queries examples (1) 10/29/2019 Sub-Queries examples (1) Combine the two queries SELECT firstname, lastname FROM Members WHERE birthday = (SELECT Min(birthday) FROM Members ) 4. Subqueries and joins CS1222-S2019

Outer query looks for records with Birthday matching Sub-Query value Sub-Query Example (1) Outer query looks for records with Birthday matching Sub-Query value Select Lastname, Firstname From Members Where Birthday = (Select Min(Birthday) From Members) Sub-Query returns ---------- 1955-11-01 Query returns Lastname Firstname ------------------------- ------------------------- Wong Tony

Sub-Queries examples (2) 10/29/2019 Sub-Queries examples (2) List all track titles and lengths of all tracks whose length is longer than the average of all track lengths Find the average track lenghths (276.08) SELECT Avg(lengthseconds) FROM Tracks Find the all track titles and lengths of all tracks whose length is greater than 276.08 SELECT tracktitle, lengthseconds FROM Tracks WHERE lengthseconds > 276.08 4. Subqueries and joins CS1222-S2019

Sub-Queries examples (2) 10/29/2019 Sub-Queries examples (2) Combine the two queries SELECT tracktitle, lengthseconds FROM Tracks WHERE lengthseconds > ( SELECT Avg(lengthseconds) FROM Tracks ) 4. Subqueries and joins CS1222-S2019

10/29/2019 Sub-Queries using IN Report the name of all artists who have recorded a title SELECT artistName FROM Artists WHERE artistID IN (SELECT artistID FROM Titles) 4. Subqueries and joins CS1222-S2019

ALL & ANY Used with Sub-Queries 10/29/2019 ALL & ANY Used with Sub-Queries Can be used with greater than and less than tests If using ANY, then comparison will be true if it satisfies any value produced by the sub-query SOME is functionally equivalent to ANY If using ALL, then comparison will be true if it satisfies all values produced by the sub-query. 4. Subqueries and joins CS1222-S2019

ALL example 10/29/2019 List the name, region, and birthday of every member who is older than all of the members in Georgia Select Birthday From Members Where Region='GA'   Birthday ----------- 1963-08-04 1959-06-22 1964-03-15 Select Lastname, Firstname, Region, Birthday From Members Where Birthday < ALL(Select Birthday From Members Where Region='GA') Lastname Firstname Region Birthday -------------------- ------------------ ---------- ------------ Ranier Brian ONT 1957-10-19 Kale Caroline VA 1956-05-30 Wong Tony ONT 1955-11-01 Cleaver Vic VT 1957-02-10 4. Subqueries and joins CS1222-S2019

ANY example WHERE region = 'GA' ) 10/29/2019 ANY example List the name, region, and birthday of every member who is older than any of the members in Georgia SELECT lastname, firstname, region, birthday FROM Members WHERE birthday < ANY (SELECT birthday FROM Members WHERE region = 'GA' ) 4. Subqueries and joins CS1222-S2019

10/29/2019 Exercises Report the firstname, lastname and birthday of the members who has the same birthday as someone in Georgia Is the following query correct? SELECT lastname, firstname, region, birthday FROM Members WHERE birthday = ALL (SELECT birthday FROM Members WHERE region = 'GA' ) No! We cannot compare a single value with a LIST of values 4. Subqueries and joins CS1222-S2019

Exercises (cont.) How about this query? 10/29/2019 Exercises (cont.) How about this query? SELECT lastname, firstname, region, birthday FROM Members WHERE birthday = (SELECT birthday FROM Members WHERE region = 'GA') No! We cannot compare a single value with a LIST of values We cannot compre 3 with (3,4,5,6) 4. Subqueries and joins CS1222-S2019

Exercises (cont.) This one? 10/29/2019 Exercises (cont.) This one? SELECT lastname, firstname, region, birthday FROM Members WHERE birthday = ANY (SELECT birthday FROM Members WHERE region = 'GA') Correct! =ANY is equivalent to IN 4. Subqueries and joins CS1222-S2019