Multiple Table Queries (Inner Joins) Week 3. Objective –Write SELECT statements to display data from more than one table using inner joins.

Slides:



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

Database Programming Sections 5 & 6 – Group functions, COUNT, DISTINCT, NVL, GROUP BY, HAVING clauses, Subqueries.
Notice that No where clause In the syntax.
Writing Basic SQL SELECT Statements. Capabilities of SQL SELECT Statements A SELECT statement retrieves information from the database. Using a SELECT.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
9 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Using the Set Operators Assist. Prof. Pongpisit Wuttidittachotti, Ph.D. Faculty.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
 After completing this lesson, you should be able to do the following: ◦ Interpret the concept of a hierarchical query ◦ Create a tree-structured report.
Ceng 356-Lab1. Objectives After completing this lesson, you should be able to do the following: Get Familiar with the development environment List the.
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.
Objectives After completing this lesson, you should be able to do the following: Define subqueries Describe the types of problems that the subqueries.
Database Programming Sections 5– GROUP BY, HAVING clauses, Rollup & Cube Operations, Grouping Set, Set Operations 11/2/10.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Sections 3 – Joins & Hierarchical Queries
Copyright © 2004, Oracle. All rights reserved. Lecture 6 Displaying Data from Multiple Tables ORACLE.
Basisdata Pertanian. After completing this lesson, you should be able to do the following:  Write SELECT statements to access data from more than one.
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-column subqueries, Multiple-row Subqueries, Correlated Subqueries 11/2/10,
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
Join, Subqueries and set operators. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
JDeveloper 10g and Oracle ADF Business Components Getting the Most Out of Your Data Avrom Roy-Faderman Senior Programmer November, 2005.
Nikolay Kostov Telerik Corporation
Copyright © 2004, Oracle. All rights reserved. D ISPLAYING D ATA FROM M ULTIPLE T ABLES.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Intermediate SQL: Aggregated Data, Joins and Set Operators.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
Database Programming Sections 4 – Joins. Marge Hohly2 Overview  Oracle Proprietary Joins (8i and prior): Cartesian Product Equijoin Non-equijoin Outer.
Using SQL Connecting, Retrieving Data, Executing SQL Commands, … Svetlin Nakov Technical Trainer Software University
Multiple Table Queries (Inner Joins, Equijoins) Week 3.
Database Programming Section 15 – Oracle Proprietary Join Syntax and Review 1.
15 Copyright © Oracle Corporation, All rights reserved. Using SET Operators.
CSED421 Database Systems Lab Join. Human Resources (HR) schema.
Copyright © 2004, Oracle. All rights reserved. Using the Set Operators.
Database Programming Sections 3 – Oracle Joins. Marge Hohly2 Obtaining Data from Multiple Tables: Using Joins.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
Including Constraints. What Are Constraints? Constraints enforce rules at the table level. You can use constraints to do the following: – Enforce rules.
19 Copyright © Oracle Corporation, All rights reserved. Hierarchical Retrieval.
Special Joins Week 4. Objective –Write SELECT statements to display left, right and full outer joins.
Lab 1 Writing Interactive Queries CISB514 Advanced Database Systems.
Chapter 13 Triggers. Trigger Overview A trigger is a program unit that is executed (fired) due to an event Event such as updating tables, deleting data.
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-row Subqueries, Correlated Subqueries.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
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.
7 Copyright © 2004, Oracle. All rights reserved. Hierarchical Retrieval.
1 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
4 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Oracle 10g Retrieving Data Using the SQL SELECT Statement.
Advanced SQL Advanced Database Dr. AlaaEddin Almabhouh.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
7 Copyright © 2004, Oracle. All rights reserved. Using Explicit Cursors.
Oracle Join Syntax.
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Writing Basic SQL SELECT Statements
Displaying Data from Multiple Tables Using Joins
“Manipulating Data” Lecture 6.
Oracle Join Syntax.
“Manipulating Data” Lecture 6.
Writing Basic SQL SELECT Statements
Using CASE Value expression
Oracle Join Syntax.
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Presentation transcript:

Multiple Table Queries (Inner Joins) Week 3

Objective –Write SELECT statements to display data from more than one table using inner joins

NAME DEPTID... IT 60 Sales 80 Executive NAME DEPTID... IT 60 Sales 80 Executive EMPLOYEES DEPARTMENTS ID NAME DEPTID 100 King Kochhar De Haan Hunold60... Related Tables Foreign key Primary key In queries data from several related tables may often have to be shown. These tables should be related through common data: the primary key of one table is often the foreign key of a related table.

ID DEPTIDNAME Executive Executive Executive IT... ID DEPTIDNAME Executive Executive Executive IT... EMPLOYEES DEPARTMENTS ID NAME DEPTID 100 King Kochhar De Haan Hunold60... NAME DEPTID... IT 60 Sales 80 Executive Display Data from Related Tables Rows of output containing information from both tables are to be displayed

Inner Joins Use inner join to display data from each row of one table with a related row in another table Joins are normally made between primary key column(s) and related foreign key column(s) Join condition(s) specified in FROM clause If the only common columns between the 2 tables are the ones used in the join condition then can use NATURAL JOIN If column names are the same in the 2 tables then use JOIN … USING (ColumnName) If column names are different in the 2 tables use JOIN … ON (Column1Name = column2Name)

Inner Join with USING Syntax SELECT column1t1, column3t1, column1t2 FROM table1 JOIN table2 USING (column3)  Assuming that column3 exists as a column in both tables and is both a foreign key in table 1 and the primary key of table2 Example: SELECT employee_id ID, department_id DEPTID, department_name NAME FROM employees JOIN departments USING (department_id) ORDER BY employee_id

Inner Join with ON Syntax SELECT t1.column1t1, t1.column3, t2.column1 FROM table1 t1 JOIN table2 t2 ON (t1.column3 = t2.column1)  Assuming that column3 in table 1 is a foreign key referencing column 1 of table 2 which is the primary key of table2 Example: SELECT subjcode, sectcode,instrno, lastname FROM section JOIN instructor ON (section.instrnum = instructor.instrno)

INNER NATURAL Join SELECT column1t1, column3t1, column1t2 FROM table1 NATURAL JOIN table2  Assuming that column3 is the only commonly named column in both tables and is both a foreign key in table 1 and the primary key of table2 Example: SELECT department_id, department_name, location_id, city FROM departments NATURAL JOIN locations

ID NAME DEPTID 100King90 101Kochhar De Haan Hunold60... ID NAME DEPTID 100King90 101Kochhar De Haan Hunold60... EMPLOYEES ID NAMELOCATION_ID 10 Administration Marketing Shipping IT ID NAMELOCATION_ID 10 Administration Marketing Shipping IT DEPARTMENTS CITY LOCATION_ID Roma 1000 Venice 1100 Tokyo 1200 Hiroshima1300 Southlake CITY LOCATION_ID Roma 1000 Venice 1100 Tokyo 1200 Hiroshima1300 Southlake How many join conditions needed? Joining More Than Two Tables LOCATIONS

SELECT employee_id ID, department_name NAME, city FROM employees JOIN departments USING (department_id) NATURAL JOIN locations ORDER BY employee_id