Multiple Table Queries 1: Inner Joins CS 320. Introduction: Join Queries Usually queries combine data from multiple tables:  List how much (pounds) of.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Relational Database Example in Access - Order System Please use speaker notes for additional information!
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.
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.
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.
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.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
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.
SQL Joins.
Chapter 2 Basic SQL SELECT Statements
IFS180 Intro. to Data Management Chapter 9 – Outer Joins.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Microsoft Access 2010 Building and Using Queries.
Chapter 9 Joining Data from Multiple Tables
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.
SQL SELECT QUERIES CS 260 Database Systems. Overview  Introduction to SQL  Single-table select queries  Using the DBMS to manipulate data output 
Creating PHPs to Insert, Update, and Delete Data CS 320.
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.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Lab 4: Displaying Data from Multiple Tables CISB224 02A, 02B Semester I, 2009/2010 College of Information Technology, Universiti Tenaga Nasional 1.
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.
Database Programming Section 15 – Oracle Proprietary Join Syntax and Review 1.
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.
Single-Table Queries 2: Advanced Topics CS 320. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data SELECT field1, field2,
# 1# 1 QueriesQueries How do we ask questions of the data? What is SELECT? What is FROM? What is WHERE? What is a calculated field? Spring 2010 CS105.
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.
4 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.
Advanced SQL Advanced Database Dr. AlaaEddin Almabhouh.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
IFS180 Intro. to Data Management Chapter 10 - Unions.
Displaying Data from Multiple Tables
LESSON Database Administration Fundamentals Inserting Data.
Displaying Data from Multiple Tables
Writing Basic SQL SELECT Statements
Displaying Data from Multiple Tables Using Joins
Lab 4: Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Presentation transcript:

Multiple Table Queries 1: Inner Joins CS 320

Introduction: Join Queries Usually queries combine data from multiple tables:  List how much (pounds) of each product that was purchased on a given date  List the customer name, product name, and pounds for a specific purchase Queries that retrieve data from multiple tables require joining the tables through primary key/foreign key relationships  This is called in inner join or equijoin

Example Inner Join CANDY_CUSTOMER CANDY_PURCHASE CANDY_PRODUCT

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

Join Query Example (ANSI 1992) Notes:  Order of tables in FROM clause doesn’t matter  Order of tables in ON condition doesn’t matter SELECT purch_id, purch_date, prod_desc FROM candy_purchase JOIN candy_product ON candy_purchase.prod_id = candy_product.prod_id;

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

Older Join Query Syntax Example (ANSI 1986) Note that the join condition is specified in the WHERE clause rather than the FROM clause SELECT purch_id, purch_date, prod_desc FROM candy_purchase, candy_product WHERE candy_purchase.prod_id = candy_product.prod_id;

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

What Happens If You Omit a Join Condition (1986 Syntax)? Creates a Cartesian product  Table1 records X Table2 records

Qualifying Field Names If a join query lists a field in the SELECT clause that appears in both tables, an error occurs To avoid this error, you need to qualify the field name: SELECT table_name.field_1, … SELECT candy_product.prod_id, purch_id, prod_desc FROM candy_product JOIN candy_purchase ON candy_product.prod_id = candy_purchase.prod_id;

 Provide a shorthand way to write queries by abbreviating table names  Once you create a table alias, you have to use it everywhere! Table Aliases SELECT field1, field2, … FROM table1 alias1 JOIN table2 alias2 ON alias1.join_field = alias2.join_field SELECT prod.prod_id, purch_id, prod_desc FROM candy_product prod JOIN candy_purchase purch ON prod.prod_id = purch.prod_id;

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 field1, field2, … FROM table1 JOIN table2 ON table1.join_field_a = table2.join_field_a JOIN table3 ON table2.join_field_b = table3.join_field_b WHERE search_condition(s)

3 Table Inner Join Example SELECT purch_date, cust_name, prod_desc FROM candy_purchase purch JOIN candy_customer cust ON purch.cust_id = cust.cust_id JOIN candy_product prod ON purch.prod_id = prod.prod_id WHERE purch_date = ' ';

Joining N Tables You can join any number of tables, provided primary key/foreign key relationships exist Challenge: determining which tables you need to include in the query  SELECT field tables  Search field tables  Tables that are needed for intermediate joins

Example: Find the product descriptions of all products purchased by customer "Bobby Bon Bons" 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'

Creating Query Design Diagrams Process: 1. Identify every table in the query that contains a display field or search field 2. Add additional tables containing join fields as needed to link all tables through primary key/foreign key links

Your Turn 1: Create a query design diagram and associated SQL query to retrieve the description of every product purchase by customers of type "Wholesale." Use "Wholesale" (rather than "W") as the search condition.