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

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.
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
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.
4 Copyright © Oracle Corporation, All rights reserved. Displaying 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.
SQL (DDL & DML Commands)
Joins & Sub-queries. Oracle recognizes that you may want data that resides in multiple tables drawn together in some meaningful way. One of the most important.
ADVANCED SQL SELECT QUERIES CS 260 Database Systems.
Recap of SQL Lab no 8 Advance Database Management System.
Multiple Table Queries (Inner Joins) Week 3. Objective –Write SELECT statements to display data from more than one table using inner joins.
Join, Subqueries and set operators. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
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.
Copyright س Oracle Corporation, All rights reserved. 4 Displaying Data from Multiple Tables.
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.
Chapter 12Introduction to Oracle9i: SQL1 Chapter 12 Additional Database Objects.
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.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
4 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Oracle 10g Retrieving Data Using the SQL SELECT Statement.
Displaying Data from Multiple Tables. Objectives After completing this lesson, you should be able to do the following: –Write SELECT statements to access.
4 Displaying Data from Multiple Tables. 4-2 Objectives At the end of this lesson, you should be able to: Write SELECT statements to access data from more.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
More SQL: Complex Queries,
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Multiple Table Queries
Displaying Data from Multiple Tables
Oracle Join Syntax.
Displaying Data from Multiple Tables
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 5: Subqueries CISB224 02A, 02B Semester I, 2009/2010
Oracle Join Syntax.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
(SQL) Displaying Data from Multiple Tables
Writing Basic SQL SELECT Statements
Lab 4: Displaying Data from Multiple Tables
CISB224 01A, 01B, 02A, 02B CCSB244 01A, 01B Semester I, 2007/2008
Oracle Join Syntax.
Displaying Data from Multiple Tables
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 1

Introduction to Joins College of Information Technology, Universiti Tenaga Nasional 2 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.columnis the condition that joins (or relates)  Table2.column the table together

Introduction to Joins College of Information Technology, Universiti Tenaga Nasional 3 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.columnis the condition that joins (or relates)  Table2.column the table together

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

Introduction to Joins – cont. College of Information Technology, Universiti Tenaga Nasional 5 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).

Introduction to Joins – cont. College of Information Technology, Universiti Tenaga Nasional 6 Example 1 PaymentID, SELECTO.ID, CustID, PaymentID, CONVERT(char(10), TotalPrice, 1) AS TotalPrice OrderInfo O JOIN PaymentType ON OrderInfo.PaymentID= PaymentType.ID FROMOrderInfo 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).

Exercise 1. Execute the command: SP_HELP OrderInfo 2. Find out the foreign keys in the OrderInfo table. College of Information Technology, Universiti Tenaga Nasional7 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

Joining More Than Two Tables College of Information Technology, Universiti Tenaga Nasional 8 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.

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

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

Qualifying Ambiguous Columns – cont. College of Information Technology, Universiti Tenaga Nasional 11 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.

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

Using Table Aliases College of Information Technology, Universiti Tenaga Nasional 13 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.

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

Joining a Table to Itself College of Information Technology, Universiti Tenaga Nasional 15 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.

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

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

Inner Joins College of Information Technology, Universiti Tenaga Nasional 18 JOIN 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.