3d. Structured Query Language – JOIN Operations

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

4d. Structured Query Language – JOIN Operation Lingma Acheson Department of Computer and Information Science IUPUI CSCI N207 Data Analysis with Spreadsheets.
SQL-week5-1 In-Class Exercise Answer IST 210 Organization of Data IST2101.
4c. Structured Query Language - Built-in Functions Lingma Acheson Department of Computer and Information Science IUPUI CSCI N207 Data Analysis with Spreadsheets.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
SQL Basics Based on the relational algebra we just learned. Nonprocedural language – what to be done not how Simple, powerful language Used for both data.
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
SQL Views Chapter 3A DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 5 th Edition.
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.
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.
12a. Regression Analysis, Part 1 CSCI N207 Data Analysis Using Spreadsheet Lingma Acheson Department of Computer and Information Science,
SQL Views Appendix E DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Fundamentals, Design, and Implementation, 9/e CPE 481 Database Processing Chapter 6 Structured Query Language (SQL) Instructor:Suthep Madarasmi, Ph.D.
SQL Views Chapter 3A DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
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.
Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 6/1 Copyright © 2004 Please……. No Food Or Drink in the class.
5. Database Design Lingma Acheson Department of Computer and Information Science IUPUI CSCI N207 Data Analysis Using Spreadsheets 1.
Databases MIS 21. Some database terminology  Database: integrated collection of data  Database Management System (DBMS): environment that provides mechanisms.
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”
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
9c. Line Charts CSCI N207 Data Analysis Using Spreadsheet Department of Computer and Information Science, IUPUI Lingma Acheson
4a. Structured Query Language - SELECT Statement Lingma Acheson Department of Computer and Information Science IUPUI CSCI N207 Data Analysis with Spreadsheets.
STRUCTURED QUERY LANGUAGE SQL-III IST 210 Organization of Data IST210 1.
3. Relational Model Lingma Acheson Department of Computer and Information Science IUPUI CSCI N207 Data Analysis with Spreadsheets 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.
Multiple Table Queries (Inner Joins, Equijoins) Week 3.
SQL Views Chapter 3A. Appendix Objectives Learn basic SQL statements for creating views Learn basic SQL statements for using views Understand the reasons.
SQL-5 In-Class Exercise Answer IST 210 Organization of Data IST2101.
2d – CheckBox Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
2e – RadioButtons Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
7. Data Import Export Lingma Acheson Department of Computer and Information Science IUPUI CSCI N207 Data Analysis Using Spreadsheets 1.
+ Complex SQL Week 9. + Today’s Objectives TOP GROUP BY JOIN Inner vs. Outer Right vs. Left.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
9d. Pie Charts CSCI N207 Data Analysis Using Spreadsheet Department of Computer and Information Science, IUPUI Lingma Acheson
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
Join Queries CS 146. Introduction: Join Queries  So far, our SELECT queries have retrieved data from a single table  Usually queries combine data from.
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
Advanced SQL Advanced Database Dr. AlaaEddin Almabhouh.
2b. Create an Access Database Lingma Acheson Department of Computer and Information Science IUPUI CSCI N207 Data Analysis with Spreadsheets 1.
Prepared By: Bobby Wan Microsoft Access Prepared By: Bobby Wan
CS122 Using Relational Databases and SQL
Multiple Table Queries
Oracle Join Syntax.
 2012 Pearson Education, Inc. All rights reserved.
SQL – Column constraints
Structured Query Language (SQL) William Klingelsmith
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.
Oracle Join Syntax.
CSCI N207 Data Analysis Using Spreadsheet
Structured Query Language
Dots 5 × TABLES MULTIPLICATION.
Dots 5 × TABLES MULTIPLICATION.
Dots 2 × TABLES MULTIPLICATION.
Using CASE Value expression
How to create query table with computation
SQL Views Appendix C DATABASE CONCEPTS, 3rd Edition
Lingma Acheson Department of Computer and Information Science IUPUI
Dots 3 × TABLES MULTIPLICATION.
Dots 6 × TABLES MULTIPLICATION.
Lab 4: Displaying Data from Multiple Tables
Dots 2 × TABLES MULTIPLICATION.
Oracle Join Syntax.
Dots 4 × TABLES MULTIPLICATION.
Displaying Data from Multiple Tables
2b – Labels and Pictures Lingma Acheson CSCI N331 VB .NET Programming
Manipulating Data Lesson 3.
Dots 3 × TABLES MULTIPLICATION.
Presentation transcript:

3d. Structured Query Language – JOIN Operations CSCI N207 Data Analysis with Spreadsheets 3d. Structured Query Language – JOIN Operations Lingma Acheson Department of Computer and Information Science IUPUI

Join Operation What if we also want to see the Hours Worked for each employee but also want to see their first names, last names, instead of the employee numbers? If we need to display columns from different tables, we must use a Join Operation. Two tables can be joined together to form a large table based on the matching of the primary key and the foreign key. E.g. Show the names of all the employees and the hours worked. SELECT FirstName, LastName, HoursWorked FROM EMPLOYEE, ASSIGNMENT WHERE EMPLOYEE.EmployeeNumber = ASSIGNMENT.EmployeeNumber;

Join Operation SELECT FirstName, LastName, HoursWorked FROM EMPLOYEE, ASSIGNMENT WHERE EMPLOYEE.EmployeeNumber = ASSIGNMENT.EmployeeNumber; Primary Key Foreign Key

Join Operation SELECT FirstName, LastName, HoursWorked FROM EMPLOYEE, ASSIGNMENT WHERE EMPLOYEE.EmployeeNumber = ASSIGNMENT.EmployeeNumber;

Join Operation Join Operation – E.g. Must list all the tables in the FROM clause. Must indicate which field in what table is matched with which field in what table. Duplicate field names from different tables must be prefixed by their table names using the dot notation. E.g. SELECT EMPLOYEE.EmployeeNumber, FirstName, LastName, HoursWorked FROM EMPLOYEE, ASSIGNMENT WHERE EMPLOYEE.EmployeeNumber = ASSIGNMENT.EmployeeNumber;

Join Operation

Multiple Conditions in WHERE Clause Notice that the records that don’t match in two tables won’t show in the result. Can apply other WHERE conditions. E.g. Show the names of the employees and their hours worked from the Accounting department. SELECT EMPLOYEE.EmployeeNumber, FirstName, LastName, HoursWorked, Department FROM EMPLOYEE, ASSIGNMENT WHERE EMPLOYEE.EmployeeNumber = ASSIGNMENT.EmployeeNumber AND Department = ‘Accounting’;

Multiple Conditions in WHERE Clause SELECT EMPLOYEE.EmployeeNumber, FirstName, LastName, HoursWorked, Department FROM EMPLOYEE, ASSIGNMENT WHERE EMPLOYEE.EmployeeNumber = ASSIGNMENT.EmployeeNumber AND Department = ‘Accounting’;

Multiple Conditions in WHERE Clause E.g. Show the names of the employees and their hours worked but only interested in hours worked more than 45. SELECT EMPLOYEE.EmployeeNumber, FirstName, LastName, HoursWorked FROM EMPLOYEE, ASSIGNMENT WHERE EMPLOYEE.EmployeeNumber = ASSIGNMENT.EmployeeNumber AND HoursWorked > 45;

Table Aliases Using table aliases to avoid repeatedly typing long table names – in the FROM clause, use AS XX after a table name with XX as the new shortened name you give for that table. E.g. Show the names of the employees and their hours worked from the Accounting department. SELECT E.EmployeeNumber, FirstName, LastName, HoursWorked, Department FROM EMPLOYEE AS E, ASSIGNMENT AS A WHERE E.EmployeeNumber = A.EmployeeNumber AND Department = ‘Accounting’;

Join More Than Two Tables Can join more than two tables. E.g. Show the names of the employees and their hours worked on each project, including the project name. SELECT P.ProjectName, FirstName, LastName, HoursWorked FROM EMPLOYEE AS E, ASSIGNMENT AS A, PROJECT AS P WHERE E.EmployeeNumber = A.EmployeeNumber AND P.ProjectID = A.ProjectID

INNER JOIN … ON Using the JOIN … ON key words Another way to apply a JOIN operation ACCESS syntax: must use INNER JOIN … ON E.g. Show the names of all the employees and the hours worked. Without using the INNERJOIN key word: SELECT FirstName, LastName, HoursWorked FROM EMPLOYEE AS E, ASSIGNMENT AS A WHERE E.EmployeeNumber = A.EmployeeNumber; Using the INNER JOIN key word: FROM EMPLOYEE AS E INNER JOIN ASSIGNMENT AS A ON E.EmployeeNumber = A.EmployeeNumber;