Lab 4: Displaying Data from Multiple Tables

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

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.
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
Writing Basic SQL SELECT Statements. Capabilities of SQL SELECT Statements A SELECT statement retrieves information from the database. Using a SELECT.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
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.
Lab 5: Subqueries CISB224 02A, 02B Semester I, 2009/2010 College of Information Technology, Universiti Tenaga Nasional 1.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Chapter 9 Joining Data from Multiple Tables
Copyright © 2004, Oracle. All rights reserved. Lecture 6 Displaying Data from Multiple Tables ORACLE.
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.
Recap of SQL Lab no 8 Advance Database Management System.
Join, Subqueries and set operators. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
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.
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.
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.
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.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
IMS 4212: Intro to Multi-Table SELECT Statements 1 Dr. Lawrence West, MIS Dept., University of Central Florida Multi-Table SELECT Statements—Topics.
College of Information Technology, Universiti Tenaga Nasional1 Lab 2: Single-row Functions CISB224 01A CCSB244 01A Semester I, 2008/2009.
Join Queries CS 146. Introduction: Join Queries  So far, our SELECT queries have retrieved data from a single table  Usually queries combine data from.
4 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
IFS180 Intro. to Data Management Chapter 10 - Unions.
SQL Query Getting to the data ……..
More SQL: Complex Queries,
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
SQL: Schema Definition and Constraints Chapter 6 week 6
3d. Structured Query Language – JOIN Operations
Multiple Table Queries
Displaying Data from Multiple Tables
Oracle Join Syntax.
Displaying Data from Multiple Tables
ATS Application Programming: Java Programming
Displaying Data from Multiple Tables
LESSON Database Administration Fundamentals Inserting Data.
Database Relationships
Displaying Data from Multiple Tables
03 | Querying Multiple Tables with Joins
Writing Basic SQL SELECT Statements
Displaying Data from Multiple Tables Using Joins
Lab 5: Subqueries CISB224 02A, 02B Semester I, 2009/2010
SQL LANGUAGE and Relational Data Model TUTORIAL
Aggregations Various Aggregation Functions GROUP BY HAVING.
Oracle Join Syntax.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
(SQL) Displaying Data from Multiple Tables
Writing Basic SQL SELECT Statements
Rob Gleasure robgleasure.com
Relational Database Design
CISB224 01A, 01B, 02A, 02B CCSB244 01A, 01B Semester I, 2007/2008
Oracle Join Syntax.
Displaying Data from Multiple Tables
Lab 3: Single-row Functions
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Lab 2: Retrieving Data from the Database
Presentation transcript:

Lab 4: Displaying Data from Multiple Tables CISB224 02A, 02B Semester I, 2009/2010 College of Information Technology, Universiti Tenaga Nasional

Introduction to Joins Syntax SELECT table.column, table.column, table.column… FROM table1, table 2 WHERE table1.column = table2.column Table.column: denotes the table and column from which data is being retrieved Table1.column is the condition that joins (or relates) Table2.column the table together College of Information Technology, Universiti Tenaga Nasional

Introduction to Joins Syntax SELECT table.column, table.column, table.column… FROM table1 JOIN table 2 ON table1.column = table2.column Table.column: denotes the table and column from which data is being retrieved Table1.column is the condition that joins (or relates) Table2.column the table together College of Information Technology, Universiti Tenaga Nasional

Introduction to Joins SELECT ID, CustID, PaymentID Suppose you require the following query: SELECT ID, CustID, PaymentID CONVERT(char(10), TotalPrice, 1) AS TotalPrice FROM OrderInfo The payment type are displayed as ID numbers. College of Information Technology, Universiti Tenaga Nasional

Introduction to Joins – cont. But what if you want to display payment description instead of the payment IDs i.e. Cash instead of 1? You’ll need to join the OrderInfo table and the PaymentType table (where the payment description are located). College of Information Technology, Universiti Tenaga Nasional

Introduction to Joins – cont. Example 1 SELECT O.ID, CustID, PaymentID, CONVERT(char(10), TotalPrice, 1) AS TotalPrice FROM OrderInfo O JOIN PaymentType ON OrderInfo.PaymentID= PaymentType.ID [OrderInfo , PaymentType WHERE OrderInfo.PaymentID= PaymentType.ID] Once you have joined OrderInfo and PaymentType, you can use any columns from the two tables. OrderInfo and PaymentType are joined through the use of the PaymentID column in OrderInfo (the foreign key) and the ID column in PaymentType (the referenced primary key). College of Information Technology, Universiti Tenaga Nasional

Exercise Execute the command: SP_HELP OrderInfo Find out the foreign keys in the OrderInfo table. The SP_HELP command followed by a table name will display the details of the table such as: the columns in the table, their data types and lengths the table’s primary key the foreign keys in the table and the tables that they reference other tables that reference the table College of Information Technology, Universiti Tenaga Nasional

Joining More Than Two Tables You may also join more than two tables in a query. Just add another JOIN condition. If you are joining two tables, you need one JOIN condition, if you are joining three tables, you need two JOIN conditions, etc. Suppose now you also want to display each product’s supplier by name and not by ID. College of Information Technology, Universiti Tenaga Nasional

Joining More Than Two Tables– cont. Example 2 SELECT ID, Name, WarehouseID, AmountInStock, OrdId, Quantity FROM Product JOIN Inventory ON Product.ID = Inventory.ProductID JOIN OrderDetails ON Product.ID = OrderDetails.ProductID College of Information Technology, Universiti Tenaga Nasional

Qualifying Ambiguous Columns Now, suppose that you require the query in Example 2, but you also want to display the Product IDs from OrderDetails table. SELECT ID, Name, WarehouseID, AmountInStock, OrdId, Quantity,ProductID FROM Product JOIN Inventory ON Product.ID = Inventory.ProductID JOIN OrderDetails ON Product.ID = OrderDetails.ProductID College of Information Technology, Universiti Tenaga Nasional

Qualifying Ambiguous Columns – cont. You will get an error because both Inventory and OrderDetails have a column named ProductID and SQL does not know which one you mean. You need to qualify the ambiguous ProductID column. In this example, you may choose to use either the one in Inventory or the one in OrderDetails . College of Information Technology, Universiti Tenaga Nasional

Qualifying Ambiguous Columns – cont. Example 3 SELECT ID, Name, WarehouseID, AmountInStock, OrdId, Quantity,Inventory.ProductID FROM Product JOIN Inventory ON Product.ID = Inventory.ProductID JOIN OrderDetails ON Product.ID = OrderDetails.ProductID College of Information Technology, Universiti Tenaga Nasional

Using Table Aliases When you need to qualify the ambiguous columns in your query, you may find it tiring to type the table names again and again. You can give your tables aliases that are shorter than their actual names. College of Information Technology, Universiti Tenaga Nasional

Using Table Aliases – cont. Example 4 SELECT ID, Name, WarehouseID, AmountInStock, OrdId, Quantity,I.ProductID FROM Product P JOIN Inventory I ON P.ID = I.ProductID JOIN OrderDetails O ON P.ID = O.ProductID College of Information Technology, Universiti Tenaga Nasional

Joining a Table to Itself You may also join a table to itself. By using table aliases, you can make it look as if you are using two different tables instead. In the next example, you want to display the employees as subordinates, and the names of their superiors. College of Information Technology, Universiti Tenaga Nasional

Joining a Table to Itself – cont. Example 5 SELECT worker.name, ‘Reports to’, manager.name FROM emp worker, emp manager WHERE worker.MgrID = manager.ID College of Information Technology, Universiti Tenaga Nasional

Additional Search Condition You may use WHERE clause for your search condition SELECT cust.CompName FROM cust JOIN orderinfo ON cust.ID = orderinfo.CustID WHERE orderinfo.SalesRepID = 'E40001‘ [ FROM cust, orderinfo WHERE cust.ID = orderinfo.CustID AND orderinfo.SalesRepID = 'E40001‘] College of Information Technology, Universiti Tenaga Nasional

Inner Joins The inner join is the default when you do not specify the type of join in the join condition i.e. emp worker JOIN emp manager. All the queries in the previous examples use inner joins. The inner join only joins records where the foreign key column have a match in the referenced column, and vice versa. College of Information Technology, Universiti Tenaga Nasional