SQL – Part 2 Multiple Tables CIS 324 – Chapter 5.

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

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.
4d. Structured Query Language – JOIN Operation Lingma Acheson Department of Computer and Information Science IUPUI CSCI N207 Data Analysis with Spreadsheets.
Displaying Data from Multiple Tables
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
SQL-week5-1 In-Class Exercise Answer IST 210 Organization of Data IST2101.
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Multiple-Column Subqueries 12. Objectives After completing this lesson, you should be able to do the following: Write a multiple-column subquery Describe.
Introduction to Oracle9i: SQL1 Subqueries. Introduction to Oracle9i: SQL2 Chapter Objectives Determine when it is appropriate to use a subquery Identify.
Structured Query Language Chapter Three Part 3 – Inserts, Updates, Deletes.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
Database Systems More SQL Database Design -- More SQL1.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 David M. Kroenke’s Chapter Seven: SQL for Database Construction and.
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.
SQL for Data Retrieval. Save your SQL Scripts When working with SQL Management Studio, you should keep saving your scripts as a.sql file to somewhere.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
SQL for Data Retrieval. Running Example IST2102 Data Preparation Login to SQL server using your account Download three SQL script files from wiki page.
Advanced Query Formulation with SQL Week 10 Quiz Jaymond Huynh Tim Nguyen.
The Relational Database Model
IFS180 Intro. to Data Management Chapter 9 – Outer Joins.
Learningcomputer.com SQL Server 2008 – Entity Relationships in a Database.
SQL – Structured Query Language CIS 324 – Chapter 5.
Lecture 2 of Advanced Databases Advanced SQL Instructor: Mr.Ahmed Al Astal.
Chapter 9 Joining Data from Multiple Tables
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.
Fundamentals, Design, and Implementation, 9/e CPE 481 Database Processing Chapter 6 Structured Query Language (SQL) Instructor:Suthep Madarasmi, Ph.D.
Lecture8:Data Manipulation in SQL Advanced SQL queries Ref. Chapter5 Lecture8 1.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
Nested Queries (Sub Queries) A nested query is a form of a SELECT command that appears inside another SQL statement. It is also termed as subquery. The.
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.
Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 6/1 Copyright © 2004 Please……. No Food Or Drink in the class.
Chapter 4 Multiple-Table Queries
SQL for Data Retrieval. Review Questions of Previous Class Q1. Show the sum of hours worked for project with ID 1200 (use ASSIGNMENT table) – Use “SUM”
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.
Intro to SQL Management Studio. Please Be Sure!! Make sure that your access is read only. If it isn’t, you have the potential to change data within your.
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.
From Relational Algebra to SQL CS 157B Enrique Tang.
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.
STRUCTURED QUERY LANGUAGE SQL-II IST 210 Organization of Data IST210 1.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
STRUCTURED QUERY LANGUAGE SQL-III IST 210 Organization of Data IST210 1.
SQL for Data Retrieval. Save your SQL Scripts When working with SQL Management Studio, you should keep saving your scripts as a.sql file to somewhere.
Chapter 12 Subqueries and Merge Statements
Displaying Data from Multiple Tables (SQL99 Syntax with examples)
SQL Select Statement IST359.
SQL-5 In-Class Exercise Answer IST 210 Organization of Data IST2101.
Structured Query Language
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
+ Complex SQL Week 9. + Today’s Objectives TOP GROUP BY JOIN Inner vs. Outer Right vs. Left.
Chapter 4: Combining Tables Vertically using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
Chapter 7 Subqueries. Chapter Objectives  Determine when it is appropriate to use a subquery  Identify which clauses can contain subqueries  Distinguish.
Structured Query Language SQL-II IST 210 Organization of Data IST2101.
1 Introduction to Database Systems, CS420 SQL JOIN, Group-by and Sub-query Clauses.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
IFS180 Intro. to Data Management Chapter 10 - Unions.
Structured Query Language
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
AND.
3d. Structured Query Language – JOIN Operations
SQL – Subqueries.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Structured Query Language
Trainer: Bach Ngoc Toan– TEDU Website:
Presentation transcript:

SQL – Part 2 Multiple Tables CIS 324 – Chapter 5

Sub-Queries Example – We need the names of all employees (not their employee number) for all employees who worked more than 40 hours on an assignment. Sub-Queries – Nested Queries (Effective when the results ultimately comes from ONE table) SELECTName FROMEMPLOYEE WHEREEmployeeNumber IN (SELECTDISTINCT EmployeeNum FROMASSIGNMENT WHEREHoursWorked > 40);

Sub Queries (cont) Multiple nesting is allowed: What if we only need Accounting projects from the above example: SELECTName FROMEMPLOYEE WHEREEmployeeNumber IN (SELECTDISTINCT EmployeeNum FROMASSIGNMENT WHEREProjectID IN ( SELECTProjectID FROMPROJECT WHEREDepartment = Accounting));

Joins When we need to display data from 2 or more tables SELECTName, HoursWorked FROMEMPLOYEE, ASSIGNMENT WHEREEmployeeNumber = EmployeNum; This creates a new table with Name from the EMPLOYEE table and HoursWorked from the ASSIGNMENT table when the condition of matching employee numbers occurs. A join is just another table so all earlier SQL statements are available for use. (Group BY, WHERE, etc.)

Multiple Table Joins We can join more than 2 tables together: SELECTProject.Name, HoursWorked, EMPLOYEE.Name FROMPROJECT, ASSIGNMENT, EMPLOYEE WHEREPROJECT.ProjectID = ASSIGNMENT.ProjectID ANDEMPLOYEE.EmployeeNumber = ASSIGNMENT.EmployeeNum;

Joins (cont) If two columns in separate tables have the same column name you will need to indicate the table name and column name in the WHERE statement: SELECT Name, HoursWorked FROMPROJECT, ASSIGNMENT WHEREPROJECT.ProjectID = ASSIGNMENT.ProjectID; If not all rows in both tables have a match in the join condition – these rows will not appear in the join table.

Outer Joins OUTER JOIN – Not part of SQL Standard but supported by most DBMS – Solution to dropping data in standard join An outer join appends the rows in the select statement onto the existing table on either the left or right side: SELECTName, HoursWorked FROMPROJECT LEFT JOIN ASSIGNMENT WHEREPROJECT.ProjectID = ASSIGNMENT.ProjectID;

Sample Outer Join This appends the name of the project to the left side of the assignment table. The unmatched rows will receive a null value: Q3 Portfolio Analysis17.50 Q3 Portfolio Analysis12.50 Q3 Portfolio Analysis8.00 Q3 Portfolio Analysis20.25 Q3 Tax Prep45.75 Q3 Tax Prep70.50 Q3 Tax Prep40.50 Q4 Product Plan75.00 Q4 Product Plan20.25 Q4 Product Plan25.25 Q4 Portfolio Analysisnull