Join Queries CS 146. Introduction: Join Queries So far, our SELECT queries have retrieved data from a single table Usually queries combine data from multiple.

Slides:



Advertisements
Similar presentations
SQL – Lesson II Grade 12.
Advertisements

Database Relationships in Access As you recall, the data in a database is stored in tables. In a relational database like Access, you can have multiple.
Advanced SQL Topics Edward Wu.
SQL/PL SQL Oracle By Rana Umer. Quiz 2 Q1.Create a table called "Persons" that contains five columns: PersonID, LastName, FirstName, Address, and City.
Advanced SQL (part 1) CS263 Lecture 7.
© 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
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.
ZEIT2301 Design of Information Systems Multi-Table Queries in SQL School of Engineering and Information Technology Dr Kathryn Merrick.
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
MULTIPLE-TABLE QUERIES
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables. Objectives After completing this lesson, you should be able to do the following:  Write SELECT statements to access.
After completing this lesson, you should be able to do the following: Write SELECT statements to access data from more than one table using equality and.
Multiple Table Queries 2: Outer Joins, Self Joins, Nested Queries, and Views CS 320.
Introduction to Oracle9i: SQL1 Basic SQL SELECT Statements.
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.
Database Relationships Objective 5.01 Understand database tables used in business.
Displaying Data from Multiple Tables. Obtaining Data from Multiple Tables Sometimes you need to use data from more than one table. In the example, the.
Objectives After completing this lesson, you should be able to do the following: Write SELECT statements to access data from more than one table using.
Databases & Data Warehouses Chapter 3 Database Processing.
Learningcomputer.com SQL Server 2008 – Entity Relationships in a Database.
Using Relational Databases and SQL John Hurley Department of Computer Science California State University, Los Angeles Lecture 3: Joins Part I.
SQL Joins Oracle and ANSI Standard SQL Lecture 6.
ITBIS373 Database Development
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.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
JOI/1 Data Manipulation - Joins Objectives –To learn how to join several tables together to produce output Contents –Extending a Select to retrieve data.
ADVANCED SQL SELECT QUERIES CS 260 Database Systems.
® Microsoft Access 2010 Tutorial 9 Using Action Queries and Advanced Table Relationships.
Chapter 4 Multiple-Table Queries
Texas State Technical College DISCOVER! Inner Joins Let’s get together… yeah yeah yeah!
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining Multiple Tables.
XP Chapter 3 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Analyzing Data For Effective Decision Making Chapter.
Lab 4: Displaying Data from Multiple Tables CISB224 02A, 02B Semester I, 2009/2010 College of Information Technology, Universiti Tenaga Nasional 1.
Tutorial 9 Using Action Queries and Advanced Table Relationships.
Microsoft Access 2013 ®® Tutorial 9 Using Action Queries and Advanced Table Relationships.
1 Multiple Table Queries. 2 Objectives  Retrieve data from more than one table by joining tables  Using IN and EXISTS to query multiple tables  Nested.
Rebecca McCready Faculty of Medical Sciences Newcastle University Lecture 2 – Relationships and Lookup fields.
WDM3304 week 6 Table Joins. Primary keys, foreign keys Primary key: A field of a table designated to provide a unique identifier for a specific row of.
Multiple Table Queries 1: Inner Joins CS 320. Introduction: Join Queries Usually queries combine data from multiple tables:  List how much (pounds) of.
Displaying Data from Multiple Tables (SQL99 Syntax with examples)
1 CHƯƠNG 4 Creating Relational Databases Understanding Table Relationships Example: This database tracks customers and their orders in two separate.
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.
Single-Table Queries 2: Advanced Topics CS 320. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data SELECT field1, field2,
Database Relationships Objective 5.01 Understand database tables used in business.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Join Queries CS 146. Introduction: Join Queries  So far, our SELECT queries have retrieved data from a single table  Usually queries combine data from.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
CS 122: Lecture 3 Joins (Part 1) Tarik Booker CS 122 California State University, Los Angeles October 7, 2014.
Copyright © 2016 Pearson Education, Inc. CHAPTER 7: ADVANCED SQL (PART I) Modern Database Management 12 th Edition Jeff Hoffer, Ramesh Venkataraman, Heikki.
CSC314 DAY 9 Intermediate SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall USING AND DEFINING VIEWS  Views provide users controlled.
Advanced SQL Advanced Database Dr. AlaaEddin Almabhouh.
IFS180 Intro. to Data Management Chapter 10 - Unions.
More SQL: Complex Queries,
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
David M. Kroenke and David J
5.02 Understand database queries, forms, and reports used in business.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Creating and Maintaining
Putting it back together
Lab 4: Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Presentation transcript:

Join Queries CS 146

Introduction: Join Queries So far, our SELECT queries have retrieved data from a single table Usually queries combine data from multiple tables: List how much (pounds) of each product that was purchased today List the customer name and product name for a specific purchase Queries that retrieve data from multiple tables require joining the tables through primary key/foreign key relationships

Main Types of Join Queries Inner Join Retrieves all matching fields in joined tables Also called equijoin or natural join Outer Join Retrieves all fields in one table, and matching fields in second table if they exist

Example Inner Join CANDY_CUSTOMER CANDY_PURCHASE CANDY_PRODUCT

Join Query Syntax (ANSI 1992) The word "INNER" is optional SELECT Column1, Column2, … FROM Table1 INNER JOIN Table2 ON Table1.JoinColumn = Table2.JoinColumn WHERE SearchCondition(s) Join condition

Join Query Example (ANSI 1992) Note: Order of tables in FROM clause doesnt matter Order of tables in ON condition doesnt matter

Qualifying Field Names What if a join query retrieves a field that exists in both tables?

Qualifying Field Names You qualify the field name in the SELECT clause Preface the field name with the name of either table

Shorthand way to write queries by abbreviating table names Pros & cons? Table Aliases NOTE: Once you create a table alias, you have to use it everywhere…

Inner Join of 3 Tables General syntax: Note: Placing each INNER JOIN and ON clause on a separate line makes the query easier to read and understand SELECT Column1, Column2, … FROM Table1 INNER JOIN Table2 ON Table1.JoinColumn = Table2.JoinColumn INNER JOIN Table3 ON Table2.JoinColumn = Table3.JoinColumn WHERE SearchCondition(s)

3 Table Inner Join Example

Joining N Tables You can join any number of tables, provided primary key/foreign key relationships exist Challenge: Including all necessary tables in the query

You can join any number of tables, provided primary key/foreign key relationships exist Challenge: you need to include table in join queries to provide needed links even if you don't include fields in the SELECT clause… Joining N Tables

Example CANDY_PRODUCT prod_desc (D) prod_id (J) CANDY_CUSTOMER cust_name (S) cust_id (J) CANDY_PURCHASE prod_id (J) cust_id (J) SELECT prod_desc FROM candy_product INNER JOIN candy_purchase ON candy_product.prod_id = candy_purchase.prod_id INNER JOIN candy_customer ON candy_purchase.cust_id = candy_customer.cust_id WHERE cust_name = 'Bobby Bon Bons'

Designing Complex Join Queries Terminology: Display field: Retrieved data field Appears in the SELECT clause Join field Primary or foreign key used to join tables Appears in a join condition Search field Used in a search condition Appears in the WHERE clause Join queries must include all tables that contain display, join, or search fields

Query Design Diagrams Visual way to identify display, join, and search fields Process: 1. Identify every table in the query 2. Identify every involved field in each table Label whether it is a display, search, or join field 3. Create join condition links