CS122 Using Relational Databases and SQL Huiping Guo Department of Computer Science California State University, Los Angeles 4. Subqueries and joins.

Slides:



Advertisements
Similar presentations
Multiple Table Queries
Advertisements

Chapter 4 Joining Multiple Tables
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 3: Joins Part I.
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.
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
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 Lecture 7: Aggregates.
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 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 Department of Computer Science California State University, Los Angeles Lecture 7:
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.
Inner join, self join and Outer join Sen Zhang. Joining data together is one of the most significant strengths of a relational database. A join is a query.
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 8: Subqueries.
Using Relational Databases and SQL John Hurley Department of Computer Science California State University, Los Angeles Lecture 3: Joins Part I.
Lecture 2 of Advanced Databases Advanced SQL Instructor: Mr.Ahmed Al Astal.
SQL Joins Oracle and ANSI Standard SQL Lecture 6.
Chapter 9 Joining Data from Multiple Tables
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.
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 6: Midterm Review.
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining Multiple Tables.
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 4: Joins Part II.
Database Programming Sections 4 – Joins. Marge Hohly2 Overview  Oracle Proprietary Joins (8i and prior): Cartesian Product Equijoin Non-equijoin Outer.
Database Programming Section 15 – Oracle Proprietary Join Syntax and Review 1.
Displaying Data from Multiple Tables (SQL99 Syntax with examples)
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.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
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.
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
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,
Advanced SQL Advanced Database Dr. AlaaEddin Almabhouh.
Joins (Part II) Tarik Booker California State University, Los Angeles.
Using Subqueries to Solve Queries
More SQL: Complex Queries,
CS3220 Web and Internet Programming More SQL
CS122 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Using Subqueries to Solve Queries
CS122 Using Relational Databases and SQL
Querying Multiple Tables
JOINS (Joinining multiple tables)
CS122 Using Relational Databases and SQL
Putting it back together
CS122 Using Relational Databases and SQL
CS4222 Principles of Database System
CS122 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
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
Displaying Data from Multiple Tables
Manipulating Data Lesson 3.
JOINS (Joinining multiple tables)
CS122 Using Relational Databases and SQL
Presentation transcript:

CS122 Using Relational Databases and SQL Huiping Guo Department of Computer Science California State University, Los Angeles 4. Subqueries and joins

4. Subqueries and joins CS122_W16 Outline r Subqueries m Subqueries in the WHERE and Having clause m Subqueries with IN, ALL & ANY r Joining tables 4-2

4. Subqueries and joins CS122_W16 Sub-Queries r A query within a query r Sub-query placed in parentheses r Sub-queries can be placed in m WHERE clause m HAVING clause m SELECT clause m FROM clause 4-3

4. Subqueries and joins CS122_W16 Sub-Queries examples (1) r List the name of the oldest member m Find the earliest birthday ( ) Select min(birthday) From Members m Find the member whose birthday is ‘ ’ Select firstname, lastname From Members Where birthday = ‘ ’ 4-4

4. Subqueries and joins CS122_W16 Sub-Queries examples (1) r Combine the two queries SELECT firstname, lastname FROM Members WHERE birthday = (SELECT Min(birthday) FROM Members ) 4-5

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

4. Subqueries and joins CS122_W16 Sub-Queries examples (2) r Combine the two queries SELECT tracktitle, lengthseconds FROM Tracks WHERE lengthseconds > ( SELECT Avg(lengthseconds) FROM Tracks ) 4-7

4. Subqueries and joins CS122_W16 Sub-Queries using IN r Report the name of all artists who have recorded a title SELECT artistName FROM Artists WHERE artistID IN (SELECT artistID FROM Titles) 4-8

Question r Is the following query correct? SELECT artistName FROM Artists WHERE artistID = (SELECT artistID FROM Titles) 4. Subqueries and joins CS122_W16 4-9

4. Subqueries and joins CS122_W16 ALL & ANY r Used with Sub-Queries r Can be used with greater than and less than tests r If using ANY, then comparison will be true if it satisfies any value produced by the sub-query r If using ALL, then comparison will be true if it satisfies all values produced by the sub-query. 4-10

4. Subqueries and joins CS122_W16 ALL example r List the name, region, and birthday of every member who is older than all of the members in Georgia SELECT lastname, firstname, region, birthday FROM Members WHERE birthday < ALL (SELECT birthday FROM Members WHERE region = ' GA ' ) 4-11

4. Subqueries and joins CS122_W16 ANY example r 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-12

4. Subqueries and joins CS122_W16 Exercises r Report the firstname, lastname and birthday of the members who has the same birthday as someone in Virginia r Is the following query correct? SELECT lastname, firstname, region, birthday FROM Members WHERE birthday = ALL (SELECT birthday FROM Members WHERE region = 'GA' ) It doesn’t make sense to test whether a single value equals a list of values 4-13

4. Subqueries and joins CS122_W16 Exercises (cont.) r 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 compare 3 with (3,4,5,6) 4-14

4. Subqueries and joins CS122_W16 Exercises (cont.) r 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-15

4. Subqueries and joins CS122_W16 Subquery in Having clause r Find the title id which has the longest average track length Select titleID, avg(lengthseconds) From Tracks Group by titleID Having avg(lengthseconds) >= all (select avg(lengthseconds) from Tracks group by titleID) 4-16

4. Subqueries and joins CS122_W16 Join r When information spreads over multiple tables m Find the projects which John is working on eidename 01John 02Susan eidpid 01T4 01X3 02S2 Employees Projects 4-17

4. Subqueries and joins CS122_W16 Step1: Cartesian product eidename eidpid John 01 X3 John 01 T4 John 01 02S2 01T4 Susan 02 01X3 Susan 02 S2Susan 02 EmployeesProjects Join every row in the first table to every row in the second table in every possible combination 4-18

4. Subqueries and joins CS122_W16 Step2: Throw out non-matching records pid eidename eid John 01 X3 John 01 T4 John 01 02S2 01T4 Susan 02 01X3 Susan 02 S2Susan 02 Non-matching records 4-19

4. Subqueries and joins CS122_W16 Results eidename eidpid John 01 X3 John 01 T4 02S2Susan

4. Subqueries and joins CS122_W16 The kinds of join r Equi Join r Inner join r Outer join m Left join m Right join m Full join r Self join r Cross join 4-21

4. Subqueries and joins CS122_W16 Equi Join example 1 r List the CD title and the title of all tracks recorded in Studio 2 SELECT title, tracktitle FROM Titles, Tracks WHERE Titles.titleID = Tracks.titleID AND studioID=2 Which tables to join Which fields to match (throw non-mathing fields 4-22

4. Subqueries and joins CS122_W16 Tips to join tables 1. Find which tables to join: TA, TB 2. Find the common attributes of those tables: field 3. Specify the relationship in the where clause: TA.field=TB.field 4. Put the two tables’ fields in the select clause as needed 4-23

4. Subqueries and joins CS122_W16 Equi join example 2 r List the names of all artists who have recorded a title and the number of titles they have SELECT artistname, COUNT(Titles.artistID) AS NumTitles FROM Artists, Titles WHERE Artists.artistID=Titles.artistID GROUP BY artistname 4-24

4. Subqueries and joins CS122_W16 Equi join summary r The tables to join are in the FROM clause m Only join just enough tables r The relationship specifications are in the WHERE clause r WHERE clause will get messy if more tables are added 4-25

4. Subqueries and joins CS122_W16 Inner Join r Same result as Equi Join r Different Syntax m Tables are listed with the keyword INNER JOIN m Relationship specifications moved from WHERE clause to ON clause Free WHERE clause for traditional conditions 4-26

4. Subqueries and joins CS122_W16 Inner Join example 1 r List the CD title and the title of all tracks recorded in Studio 2 SELECT title, tracktitle FROM Titles INNER JOIN Tracks ON Titles.titleid = Tracks.titleid WHERE studioID=2 Tables to join Relationship specification 4-27

4. Subqueries and joins CS122_W16 Inner Join example 2 r List the names of all artists who have recorded a title and the number of titles they have SELECT artistname, COUNT(Titles.artistID) AS NumTitles FROM Artists INNER JOIN Titles ON Artists.artistID=Titles.artistID GROUP BY artistName 4-28

4. Subqueries and joins CS122_W16 Table Aliases r Saves typing r Table name cannot be used in rest of query r Case-sensitive in some systems r Example m List the names of members from Georgia (GA) and their salespeople SELECT M.Lastname, M.FirstName, S.Lastname,S.Firstname FROM Members M, Salespeople S WHERE M.SalesID= S.SalesID And Region='GA' 4-29

4. Subqueries and joins CS122_W16 Summary r Sub-queries in WHERE clauses r Sub-queries with IN, ALL, ANY r Sub-queries in HAVING clauses r Equi-joins r Inner joins 4-30