SELECT from Multiple Tables

Slides:



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

© Abdou Illia MIS Spring 2014
Sometimes you need to use data from more than one table. In example1, the report displays data from two separate tables. Employee IDs exist in the EMPLOYEES.
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
Chapter 4 Joining Multiple Tables
More SQL Data Definition
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.
ZEIT2301 Design of Information Systems Multi-Table Queries in SQL School of Engineering and Information Technology Dr Kathryn Merrick.
Database Systems Lecture 7 Natasha Alechina
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Relational Algebra Ch. 7.4 – 7.6 John Ortiz. Lecture 4Relational Algebra2 Relational Query Languages  Query languages: allow manipulation and retrieval.
Introduction to Oracle9i: SQL1 Subqueries. Introduction to Oracle9i: SQL2 Chapter Objectives Determine when it is appropriate to use a subquery Identify.
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.
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.
More SQL Select Database Systems Lecture 8 Natasha Alechina.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
The Relational Model Database Systems Lecture 3 Natasha Alechina.
Using Relational Databases and SQL John Hurley Department of Computer Science California State University, Los Angeles Lecture 3: Joins Part I.
Physical DB Issues, Indexes, Query Optimisation Database Systems Lecture 13 Natasha Alechina.
Chapter 9 Joining Data from Multiple Tables
ITBIS373 Database Development
Programming using C# Joins SQL Injection Stored Procedures
Chapter 2 Adapted from Silberschatz, et al. CHECK SLIDE 16.
Using Special Operators (LIKE and IN)
Week 10 Quiz 9 Answers Group 28 Christine Hallstrom Deena Phadnis.
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining Multiple Tables.
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.
Relational Databases.  In week 1 we looked at the concept of a key, the primary key is a column/attribute that uniquely identifies the rest of the data.
While you are waiting for class to start... (1) Login to SQL Server 2012 Management Studio (2) Execute the file called “SQLLab3.sql”. It is located on.
SQL Select Statement IST359.
 CS 405G: Introduction to Database Systems Lecture 6: Relational Algebra Instructor: Chen Qian.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
LINQ to DATABASE-2.  Creating the BooksDataContext  The code combines data from the three tables in the Books database and displays the relationships.
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.
QUERY CONSTRUCTION CS1100: Data, Databases, and Queries CS1100Microsoft Access1.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 5: SQL I Rob Gleasure robgleasure.com.
Day 5 - More Complexity With Queries Explanation of JOIN & Examples Explanation of JOIN & Examples Explanation & Examples of Aggregation Explanation &
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.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
IS6146 Databases for Management Information Systems Lecture 2: SQL II – Joins, Updates, Insertions, and Deletions Rob Gleasure robgleasure.com.
IFS180 Intro. to Data Management Chapter 10 - Unions.
SQL – join.
More SQL: Complex Queries,
CS3220 Web and Internet Programming More SQL
Chapter 5 Introduction to SQL.
Rob Gleasure robgleasure.com
COMP3017 Advanced Databases
COMP 430 Intro. to Database Systems
A Table with a View: Database Queries
LESSON Database Administration Fundamentals Inserting Data.
SQL: Structured Query Language DML- Queries Lecturer: Dr Pavle Mogin
SQL – Subqueries.
Relational Algebra 461 The slides for this text are organized into chapters. This lecture covers relational algebra, from Chapter 4. The relational calculus.
CS 405G: Introduction to Database Systems
COMP 430 Intro. to Database Systems
Relational Databases The Relational Model.
Relational Databases The Relational Model.
Chapter Name SQL: Data Manipulation
JOINs JOINs can be used to combine tables A CROSS JOIN B
Rob Gleasure robgleasure.com
A Table with a View: Database Queries
CSC 453 Database Systems Lecture
CS639: Data Management for Data Science
A Very Brief Introduction to Relational Databases
Rob Gleasure robgleasure.com
A Table with a View: Database Queries
Displaying Data from Multiple Tables
Manipulating Data Lesson 3.
SubQueries Part III.
Presentation transcript:

SELECT from Multiple Tables Often you need to combine information from two or more tables You can get the effect of a product by using SELECT * FROM Table1, Table2... If the tables have columns with the same name ambiguity results You resolve this by referencing columns with the table name TableName.Column

SELECT from Multiple Tables Student ID First Last S103 John Smith S104 Mary Jones S105 Jane Brown S106 Mark Jones S107 John Brown SELECT First, Last, Mark FROM Student, Grade WHERE (Student.ID = Grade.ID) AND (Mark >= 40) Grade ID Code Mark S103 DBS 72 S103 IAI 58 S104 PR1 68 S104 IAI 65 S106 PR2 43 S107 PR1 76 S107 PR2 60 S107 IAI 35

SELECT from Multiple Tables SELECT ... FROM Student, Grade WHERE... Are matched with the first entry from the Student table... All of the entries from the Grade table ID First Last ID Code Mark S103 John Smith S103 DBS 72 S103 John Smith S103 IAI 58 S103 John Smith S104 PR1 68 S103 John Smith S104 IAI 65 S103 John Smith S106 PR2 43 S103 John Smith S107 PR1 76 S103 John Smith S107 PR2 60 S103 John Smith S107 IAI 35 S104 Mary Jones S103 DBS 72 S104 Mary Jones S103 IAI 58 S104 Mary Jones S104 PR1 68 S104 Mary Jones S104 IAI 65 S104 Mary Jones S106 PR2 43 And then with the second… and so on

SELECT from Multiple Tables SELECT ... FROM Student, Grade WHERE (Student.ID = Grade.ID) AND ... Grade.ID Student.ID ID First Last ID Code Mark S103 John Smith S103 DBS 72 S103 John Smith S103 IAI 58 S104 Mary Jones S104 PR1 68 S104 Mary Jones S104 IAI 65 S106 Mark Jones S106 PR2 43 S107 John Brown S107 PR1 76 S107 John Brown S107 PR2 60 S107 John Brown S107 IAI 35

SELECT from Multiple Tables SELECT ... FROM Student, Grade WHERE (Student.ID = Grade.ID) AND (Mark >= 40) ID First Last ID Code Mark S103 John Smith S103 DBS 72 S103 John Smith S103 IAI 58 S104 Mary Jones S104 PR1 68 S104 Mary Jones S104 IAI 65 S106 Mark Jones S106 PR2 43 S107 John Brown S107 PR1 76 S107 John Brown S107 PR2 60

SELECT from Multiple Tables SELECT First, Last, Mark FROM Student, Grade WHERE (Student.ID = Grade.ID) AND (Mark >= 40) First Last Mark John Smith 72 John Smith 58 Mary Jones 68 Mary Jones 65 Mark Jones 43 John Brown 76 John Brown 60

SELECT from Multiple Tables When selecting from multiple tables you almost always use a WHERE clause to find entries with common values SELECT * FROM Student, Grade, Course WHERE Student.ID = Grade.ID AND Course.Code = Grade.Code

SELECT from Multiple Tables Student Course ID First Last ID Code Mark Code Title S103 John Smith S103 DBS 72 DBS Database Systems S103 John Smith S103 IAI 58 IAI Intro to AI S104 Mary Jones S104 PR1 68 PR1 Programming 1 S104 Mary Jones S104 IAI 65 IAI Intro to AI S106 Mark Jones S106 PR2 43 PR2 Programming 2 S107 John Brown S107 PR1 76 PR1 Programming 1 S107 John Brown S107 PR2 60 PR2 Programming 2 S107 John Brown S107 IAI 35 IAI Intro to AI Student.ID = Grade.ID Course.Code = Grade.Code

JOINs JOINs can be used to combine tables A CROSS JOIN B There are many types of JOIN CROSS JOIN INNER JOIN NATURAL JOIN OUTER JOIN OUTER JOINs are linked with NULLs - more later A CROSS JOIN B returns all pairs of rows from A and B A NATURAL JOIN B returns pairs of rows with common values for identically named columns and without duplicating columns A INNER JOIN B returns pairs of rows satisfying a condition

CROSS JOIN SELECT * FROM Student CROSS JOIN Enrolment Student ID Name 123 John 124 Mary 125 Mark 126 Jane SELECT * FROM Student CROSS JOIN Enrolment ID Name ID Code 123 John 123 DBS 124 Mary 123 DBS 125 Mark 123 DBS 126 Jane 123 DBS 123 John 124 PRG 124 Mary 124 PRG 125 Mark 124 PRG 126 Jane 124 PRG 123 John 124 DBS 124 Mary 124 DBS Enrolment ID Code 123 DBS 124 PRG 124 DBS 126 PRG

NATURAL JOIN SELECT * FROM Student NATURAL JOIN Enrolment Student ID Name 123 John 124 Mary 125 Mark 126 Jane SELECT * FROM Student NATURAL JOIN Enrolment ID Name 123 John 124 Mary 126 Jane Code DBS Enrolment ID Code 123 DBS 124 PRG 124 DBS 126 PRG PRG DBS PRG

CROSS and NATURAL JOIN is the same as SELECT * FROM A CROSS JOIN B SELECT * FROM A, B SELECT * FROM A NATURAL JOIN B is the same as SELECT A.col1,… A.coln, [and all other columns apart from B.col1,…B.coln] FROM A, B WHERE A.col1 = B.col1 AND A.col2 = B.col2 ...AND A.coln = B.col.n (this assumes that col1… coln in A and B have common names)

INNER JOINs specify a condition which the pairs of rows satisfy SELECT * FROM A INNER JOIN B ON <condition> Can also use SELECT * FROM A INNER JOIN B USING (col1, col2,…) Chooses rows where the given columns are equal

INNER JOIN SELECT * FROM Student INNER JOIN Enrolment USING (ID) ID Name 123 John 124 Mary 125 Mark 126 Jane SELECT * FROM Student INNER JOIN Enrolment USING (ID) ID Name ID Code 123 John 123 DBS 124 Mary 124 PRG 124 Mary 124 DBS 126 Jane 126 PRG Enrolment ID Code 123 DBS 124 PRG 124 DBS 126 PRG

INNER JOIN SELECT * FROM Buyer INNER JOIN Property ON Name Budget Smith 100,000 Jones 150,000 Green 80,000 SELECT * FROM Buyer INNER JOIN Property ON Price <= Budget Name Budget Address Price Smith 100,000 15 High St 85,000 Jones 150,000 15 High St 85,000 Jones 150,000 12 Queen St 125,000 Property Address Price 15 High St 85,000 12 Queen St 125,000 87 Oak Row 175,000

WHERE <condition> INNER JOIN SELECT * FROM A INNER JOIN B ON <condition> is the same as SELECT * FROM A, B WHERE <condition> SELECT * FROM A INNER JOIN B USING(col1, col2,...) is the same as SELECT * FROM A, B WHERE A.col1 = B.col1 AND A.col2 = B.col2 AND ...

JOIN vs WHERE Clauses JOINs (so far) are not needed Yes, because You can have the same effect by selecting from multiple tables with an appropriate WHERE clause So should you use JOINs or not? Yes, because They often lead to concise queries NATURAL JOINs are very common No, because Support for JOINs varies a fair bit among SQL dialects

Writing Queries When writing queries Most DBMSs have query optimisers There are often many ways to write the query You should worry about being correct, clear, and concise in that order Don’t worry about being clever or efficient Most DBMSs have query optimisers These take a user’s query and figure out how to efficiently execute it A simple query is easier to optimise We’ll look at some ways to improve efficiency later

Exercise Track cID Num Title Time aID 1 Violent 239 1 1 2 Every Girl 410 1 3 Breather 217 1 1 4 Part of Me 279 1 1 Star 362 1 2 2 Teaboy 417 2 CD cID Title Price Mix 9.99 Compilation 12.99 Artist aID Name Stellar Cloudboy

Exercise 1- Find a list of all the CD titles. 2- Find a list of the titles of tracks that are more than 300 seconds long. 3- Find a list of the names of those artists who have a track on the CD with the title “Compilation”.