Agenda for 02/16/2006 Answer any questions about SQL project. Do you want to see any of the answers for the queries? Discuss additional formatting options.

Slides:



Advertisements
Similar presentations
SQL/PL SQL Oracle By Rana Umer. Quiz 2 Q1.Create a table called "Persons" that contains five columns: PersonID, LastName, FirstName, Address, and City.
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.
Chapter 4 Joining Multiple Tables
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
Displaying Data from Multiple Tables
Data Bits Many to Many Subkeys JoinsQueries Attributes $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 Final DataBit.
Multiple-Column Subqueries 12. Objectives After completing this lesson, you should be able to do the following: Write a multiple-column subquery Describe.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Introduction to Oracle9i: SQL1 Basic SQL SELECT Statements.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
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.
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.
Chapter 2 Basic SQL SELECT Statements Oracle 10g: SQL.
IFS180 Intro. to Data Management Chapter 9 – Outer Joins.
Learningcomputer.com SQL Server 2008 – Entity Relationships in a Database.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Lecture 2 of Advanced Databases Advanced SQL Instructor: Mr.Ahmed Al Astal.
SQL Joins Oracle and ANSI Standard SQL Lecture 6.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Chapter 9 Joining Data from Multiple Tables
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
1 Agenda – 03/25/2014 Login to SQL Server 2012 Management Studio. Answer questions about HW#7 – display answers. Exam is 4/1/2014. It will be in the lab.
Oracle’s take on joins Where it differs from ANSI standard.
1 Intro to JOINs SQL INNER JOIN SQL OUTER JOIN SQL FULL JOIN SQL CROSS JOIN Intro to VIEWs Simple VIEWs Considerations about VIEWs VIEWs as filters ALTER.
1 Agenda for Class 03/07/2006  Learn to simplify queries for complex questions through the use of views.  Concept of a view.  Syntax to create and access.
JOI/1 Data Manipulation - Joins Objectives –To learn how to join several tables together to produce output Contents –Extending a Select to retrieve data.
Week 10 Quiz 9 Answers Group 28 Christine Hallstrom Deena Phadnis.
Agenda for 02/21/2006 Learn how to use more than one table in a query. Discuss how DBMS processes multiple tables. Explain the different types of joins.
1 Agenda – 10/24/2013 Answer questions from lab on 10/22. Present SQL View database object. Present SQL UNION statement.
Chapter 4 Multiple-Table Queries
1 Agenda for Class 03/02/2006  Do SQL join “quiet participation” exercise in class.  Learn to simplify queries for complex questions through the use.
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining Multiple Tables.
Unit 4 Queries and Joins. Key Concepts Using the SELECT statement Statement clauses Subqueries Multiple table statements Using table pseudonyms Inner.
Join, Subqueries and set operators. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
1 Chapter 10 Joins and Subqueries. 2 Joins & Subqueries Joins – Methods to combine data from multiple tables – Optimizer information can be limited based.
Programming in R SQL in R. Running SQL in R In this session I will show you how to: Run basic SQL commands within R.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
While you are waiting for class to start... (1) Login to SQL Server 2012 Management Studio (2) Execute the file called “SQLLab3.sql”. It is located on.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries.
Displaying Data from Multiple Tables (SQL99 Syntax with examples)
While you are waiting for class to start... (1)Login to SQL Server 2012 Management Studio (2) Execute the file called “SQLLab4.sql”. It is located on the.
Agenda for Class 2/20/2014 Introduce Microsoft’s SQL Server database management system. Use the lab to discuss how to CREATE, DROP and populate (INSERT)
+ Complex SQL Week 9. + Today’s Objectives TOP GROUP BY JOIN Inner vs. Outer Right vs. Left.
LINQ to DATABASE-2.  Creating the BooksDataContext  The code combines data from the three tables in the Books database and displays the relationships.
SQL advanced select using Oracle 1 Multiple Tables: Joins and Set Operations Subqueries: Nested Queries.
Agenda for Class - 03/04/2014 Answer questions about HW#5 and HW#6 Review query syntax. Discuss group functions and summary output with the GROUP BY statement.
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
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.
Advanced SQL Advanced Database Dr. AlaaEddin Almabhouh.
IFS180 Intro. to Data Management Chapter 10 - Unions.
Quiz Questions Q.1 An entity set that does not have sufficient attributes to form a primary key is a (A) strong entity set. (B) weak entity set. (C) simple.
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Using Subqueries to Solve Queries
Displaying Data from Multiple Tables Using Joins
Relational Queries (query 12) Display vendor contact info (contact person and phone number) for inventory products (relationship query) Query: Inventory.
Structured Query Language
Using Subqueries to Solve Queries
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Presentation transcript:

Agenda for 02/16/2006 Answer any questions about SQL project. Do you want to see any of the answers for the queries? Discuss additional formatting options available via SQLPlus. Learn how to use more than one table in a query. Discuss how DBMS processes multiple tables. Explain the different types of joins. Describe join conditions.

Sample Database ERD Assume referential integrity is NOT maintained in this database

Accessing data from multiple tables Why do you want to access data from multiple tables in a single query? To provide more complete information in a result table. To support decision making. What happens when multiple tables are accessed in a single query?

Vocabulary Words Select List Result Table Join Cartesian Product/Cross Join Join Condition Multiple Table Access SELECT * FROMemp, time;

Eliminate columns to enhance readability SELECTname, contractid, datetime, amount/60 FROMemp, time; Does this SQL code produce a reasonable response? Let’s eliminate some columns from the select list to make a more readable result table.

Combining Tables Based on a Shared Column Option 1 (older syntax - using WHERE clause) SELECTname, contractid, datetime, amount/60 FROMtime, emp WHEREtime.empid = emp.empid; The shared column is usually the foreign key that sustains the relationship between the tables on the ERD.

Combining Tables Based on a Shared Column Option 2 (newer syntax – using join condition) SELECTname, contractid, datetime, amount/60 FROMtime INNER JOINemp ONtime.empid = emp.empid

Combining tables on a shared column that has the Same name: Natural Join SELECTname, contractid, datetime, amount/60 FROMtime NATURAL JOINemp

Viewing all rows from one of the tables in a join Option 1 (older syntax using WHERE clause) Syntax for a left outer-join in Oracle: SELECTname, time.empid contractid, datetime, amount/60 FROMtime, emp WHEREtime.empid = emp.empid (+) All data in the table on the left side of the condition will be displayed because the plus sign (+) is on the right side of the condition.

Viewing all rows from one of the tables in a join Option 2 (newer syntax using join condition) SELECTname, time.empid contractid, datetime, amount/60 FROMtime LEFT OUTER JOINemp ONemp.empid = time.empid Imagine that the time table is “left” (because it is declared first) and the emp table is “right” (because it is declared second)

Viewing all rows from the other table in a join Option 1 (older syntax using WHERE clause) Syntax for a right outer-join in Oracle: SELECTname, time.empid contractid, datetime, amount/60 FROMtime, emp WHEREtime.empid (+) = emp.empid

Right outer join – Viewing all rows in other table Option 2 (newer syntax using join condition) SELECT name, time.empid, contractid, datetime, amount/60 FROM time RIGHT OUTER JOINemp ON time.empid = emp.empid ; Remember that the time table is “left” (because it is declared first) and the emp table is “right” (because it is declared second)

Full outer join – Viewing all rows in both tables Option 2 (newer syntax using join condition – option 1 is not available) SELECTname, time.empid, contractid, datetime, amount/60 FROM time FULL OUTER JOINemp ON time.empid = emp.empid;

SELECTemp.name, time.empid, time.worktypeid, work.description, datetime, amount/60 FROMtime INNER JOINemp ONtime.empid = emp.empid INNER JOINwork ONtime.worktypeid = work.worktypeid; Combining more than two tables into a single result table

Displaying all data in TIME table COLUMN emp_name heading “Employee Name” COLUMN descr heading “Type of Work” SELECT NVL(emp.name,'NOT IN EMPLOYEE TABLE') emp_name, time.empid, time.worktypeid, NVL(work.description,'NOT IN WORK TABLE’) descr, datetime, amount/60 FROMtime LEFT OUTER JOINemp ONtime.empid = emp.empid LEFT OUTER JOINwork ONtime.worktypeid = work.worktypeid ORDER BYemp.name;

Summarizing Data COLUMN descr heading “Type of Work” SELECT NVL(work.description, ‘No Description’) descr, round(sum(amount/60),2) FROM time LEFT OUTER JOIN work ON work.worktypeid = time.worktypeid GROUP BY work.description ; The GROUP BY statement is frequently used with a result table created by multiple underlying tables

Recursive Relationship with Employee to Assign Manager

Self-join also called a recursive join Option 1 – (older syntax using WHERE clause) SELECTworker.empid "worker#", worker.name "worker name", manager.empid "manager#", manager.name "manager name" FROMemp worker, emp manager WHEREworker.mgrid = manager.empid; What change is necessary to make all employees appear whether there is a manager?

Self-join also called a recursive join Option 2 – (newer syntax using join condition) SELECTworker.empid "worker#", worker.name "worker name", manager.empid "manager#", manager.name "manager name" FROMemp worker INNER JOINemp manager ONworker.mgrid = manager.empid; What change is necessary to make all employees appear whether there is a manager?