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

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

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.
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.
Maintenance Modifying the data –Add records –Delete records –Update records Modifying the design –Add fields into tables –Remove fields from a table –Change.
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)
SQL Views Chapter 3A DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 5 th Edition.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 2: Single-Table Selections.
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.
SQL for Data Retrieval. Running Example IST2102 Data Preparation Login to SQL server using your account Download three SQL script files from wiki page.
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.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
DAY 15: ACCESS CHAPTER 2 Larry Reaves October 7,
SQL Views Appendix E DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition.
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.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Computer Science & Engineering 2111 Lecture 11 Querying a Database 1.
Using Special Operators (LIKE and IN)
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.
1 Agenda – 10/24/2013 Answer questions from lab on 10/22. Present SQL View database object. Present SQL UNION statement.
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.
Join, Subqueries and set operators. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
Computer Science & Engineering 2111 Querying a Database 1CSE 2111 Lecture- Querying a Database.
SQL for Data Retrieval. Running Example IST2102 Data Preparation Login to SQL server using your account Select your database – Your database name is.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
STRUCTURED QUERY LANGUAGE SQL-II IST 210 Organization of Data IST210 1.
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.
Quiz questions. 1 A data structure that is made up of fields and records? Table.
SQL Views Chapter 3A. Appendix Objectives Learn basic SQL statements for creating views Learn basic SQL statements for using views Understand the reasons.
You can sort Access data so you can view records in the order you want to view them, and you can filter data so you only see the records you want to see.
2a. What and Why Database? Lingma Acheson Department of Computer and Information Science IUPUI CSCI N207 Data Analysis with Spreadsheets 1.
Introduction to Access Chapter 13 pages 1-4. What is a database??? Related information is stored in databases  All SC student information is stored in.
CPSC 203 Introduction to Computers Tutorial 03 & 29 By Jie (Jeff) Gao.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
Planning & Creating a Database By Ms. Naira Microsoft Access.
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.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
LINQ to DATABASE-2.  Creating the BooksDataContext  The code combines data from the three tables in the Books database and displays the relationships.
Drill Consider the following tables with the following fields: Student: FName, LName, StudentID, Age, Yr, Course Grades: ID, P1, P2, P3 1.Display the.
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.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Join Queries CS 146. Introduction: Join Queries  So far, our SELECT queries have retrieved data from a single table  Usually queries combine data from.
5a – While Loops Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
Structured Query Language SQL-II IST 210 Organization of Data IST2101.
Computer Science & Engineering 2111 Inner Joins and Advanced Queries 1CSE 2111 Lecture-Advanced Queries.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
2b. Create an Access Database Lingma Acheson Department of Computer and Information Science IUPUI CSCI N207 Data Analysis with Spreadsheets 1.
IFS180 Intro. to Data Management Chapter 10 - Unions.
Prepared By: Bobby Wan Microsoft Access Prepared By: Bobby Wan
3d. Structured Query Language – JOIN Operations
Structured Query Language (SQL) William Klingelsmith
Relational Queries (query 12) Display vendor contact info (contact person and phone number) for inventory products (relationship query) Query: Inventory.
Structured Query Language
How to create query table with computation
Lingma Acheson Department of Computer and Information Science IUPUI
2g – ComboBox Lingma Acheson CSCI N331 VB .NET Programming
Presentation transcript:

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

Joining Multiple Tables What if we also want to see the Hours Worked for the previous query? If we need to display columns from different tables, subqueries wont work. 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; 2

Joining Multiple Tables Join Operation – –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; 3

Joining Multiple Tables Notice that the records that dont match in two tables wont 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 FROM EMPLOYEE, ASSIGNMENT WHERE EMPLOYEE.EmployeeNumber = ASSIGNMENT.EmployeeNumber AND Department = Accounting; 4

Joining Multiple Tables 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; 5

Joining Multiple Tables Practice: –Show projects whose maximum hours are smaller than 140. Need to see all the project information, as well as the budget code for departments. –An list of employees, need to see their names, s, department name, and department phone number. Sorted by last name. –How many total hours have been worked on a certain project? Show the project name, and the total hours. (hint: a combination of SUM, WHERE and GROUP BY) 6

Joining Multiple Tables 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 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 FROM EMPLOYEE AS E, ASSIGNMENT AS A WHERE E.EmployeeNumber = A.EmployeeNumber AND Department = Accounting; 7

Joining Multiple 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 8

Joining Multiple Tables Using the JOIN … ON key words 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: SELECT FirstName, LastName, HoursWorked FROM EMPLOYEE AS E INNER JOIN ASSIGNMENT AS A ON E.EmployeeNumber = A.EmployeeNumber; 9

Joining Multiple Tables Often use the graphic interface to add the tables to be joined. The table will be joined automatically based on the relationships defined. Its handy to create and save a query that shows all the joined data and later create queries based on this grand query, thus no need to join all the time. E.g. Query Saved - ProjectAndAssignment: SELECT * FROM PROJECT AS A INNER JOIN ASSIGNMENT AS A ON P.ProjectID = A.ProjectID; Query - ProjectAndHoursAccounting SELECT ProjectName, HoursWorked FROM ProjectAndAssignment WHERE Department = Accounting; 10