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

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.
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.
Chapter 4 Joining Multiple Tables
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.
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
Multiple Table Queries 2: Outer Joins, Self Joins, Nested Queries, and Views CS 320.
Action Queries CS 320. Review: SQL Command Types  Data Definition Language (DDL)  Used to create and modify database objects  Data Manipulation Language.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Introduction to Oracle9i: SQL1 Basic SQL SELECT Statements.
Creating Database Tables CS 320. Review: Levels of data models 1. Conceptual: describes WHAT data the system contains 2. Logical: describes HOW the database.
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.
ITS232 Introduction To Database Management Systems Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi MARA (UiTM), Kedah.
Chapter 2 Basic SQL SELECT Statements
IFS180 Intro. to Data Management Chapter 9 – Outer Joins.
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.
Chapter 9 Joining Data from Multiple Tables
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
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
Single-Table Queries 1: Basics CS 320 Online. Review: SQL Command Types  Data Definition Language (DDL)  Used to create and modify database objects.
Database Design 1: Introduction and Terminology CS 320.
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.
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.
Rebecca McCready Faculty of Medical Sciences Newcastle University Lecture 2 – Relationships and Lookup fields.
Advanced SELECT Queries CS 146. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data Note the default formats… SELECT.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Introduction to Databases Queries CS 146. Sample Database: CANDY_CUSTOMER CANDY_PURCHASE CANDY_CUST_TYPE CANDY_PRODUCT.
Multiple Table Queries (Inner Joins, Equijoins) Week 3.
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)
SQL ACTION QUERIES AND TRANSACTION CONTROL CS 260 Database Systems.
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.
+ Complex SQL Week 9. + Today’s Objectives TOP GROUP BY JOIN Inner vs. Outer Right vs. Left.
Single-Table Queries 2: Advanced Topics CS 320. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data SELECT field1, field2,
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
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.
Introduction to Databases Queries CS 146. Sample Database: CANDY_CUSTOMER CANDY_PURCHASE CANDY_CUST_TYPE CANDY_PRODUCT.
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
Structured Query Language (SQL) William Klingelsmith
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Creating and Maintaining
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Joining Your Data to a Map
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 doesn’t matter  Order of tables in ON condition doesn’t matter

Join Query Syntax (ANSI 1986) SELECT Column1, Column2, … FROM Table1, Table2 WHERE Table1.JoinColumn = Table2.JoinColumn AND SearchCondition(s) Join condition

Join Query Example (ANSI 1986) Notice the join is specified in the WHERE clause, not in the FROM clause

Advantages of 1992 Syntax  Separates the join conditions and search conditions  Makes it impossible to omit a join condition  Required for this class

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