Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Using two.

Slides:



Advertisements
Similar presentations
Database Fundamentals
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.
MULTIPLE-TABLE QUERIES
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Database Fundamentals Physical.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Self-joins,
Microsoft Enterprise Consortium Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Database Fundamentals Defining.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Create Student-Team.
SharePoint ® Training SharePoint slide libraries II: Use slides in the slide library Sharjah Higher Colleges of Technology presents:
SharePoint slide libraries II: Use slides in the slide library MICROSOFT ® OFFICE SHAREPOINT ® SERVER 2007 TRAINING ADVANTAGE TALENT, INC. “Professionals.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Aggregate.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Database Fundamentals The Data.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Database Fundamentals Data.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Evaluating.
Microsoft ® Office Excel ® 2007 Training Get started with PivotTable ® reports [Your company name] presents:
Northwind Sample database (also supplied with MS Access)
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Advanced SQL Greenhouse Database.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Advanced SQL Type II (Correlated)
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Introduction.
PHP and SQL Server: Queries IST2101. Project Report 4 SQL Queries Due Sunday, 4/5 at 11:59pm Instructions on how to access team webspace and SQL database.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals SQL Server.
Mail merge I: Use mail merge for mass mailings Perform a complete mail merge Now you’ll walk through the process of performing a mail merge by using the.
1 Data Bound Controls II Chapter Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Database Fundamentals Test.
SQL 1: GETTING INFORMATION OUT OF A DATABASE MIS2502 Data Analytics.
Database Systems Microsoft Access Practical #1 Creating Tables Nos 215.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals SELECT … FROM.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Advanced SQL Nested aggregate.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Aggregating.
MIS2502: Data Analytics SQL – Getting Information Out of a Database David Schuff
Views Lesson 7.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals SELECT … FROM.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Database Fundamentals Physical.
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.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Sorting output.
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.
Database Applications – Microsoft Access Lesson 7 Designing Custom Reports Updated 11/13 27 Slides in Presentation.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Logical operators.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Advanced SQL In-Line Subquery.
IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida Additional Data Manipulation Statements INSERT.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Advanced SQL Equi-Join One-sided.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Aliya Farheen October 29,2015.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Set Operators:
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals WHERE clause.
MIS2502: Data Analytics SQL – Getting Information Out of a Database.
1 Connecting to a Database Server. 2 We all have accounts, with a single database each, on a Microsoft SQL Server on the USF network: allman.forest.usf.edu.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Advanced SQL The relational.
SSMS SQL Server Management System. SQL Server Microsoft SQL Server is a Relational Database Management System (RDBMS) Relational Database Management System.
Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Database Fundamentals Data.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
1 BCS 4 th Semester. Step 1: Download SQL Server 2005 Express Edition Version Feature SQL Server 2005 Express Edition SP1 SQL Server 2005 Express Edition.
LEC-8 SQL. Indexes The CREATE INDEX statement is used to create indexes in tables. Indexes allow the database application to find data fast; without reading.
Advanced SQL Advanced Database Dr. AlaaEddin Almabhouh.
Perform a complete mail merge Lesson 14 By the end of this lesson you will be able to complete the following: Use the Mail Merge Wizard to perform a basic.
IFS180 Intro. to Data Management Chapter 10 - Unions.
Chapter 12 Introducing Databases. Objectives What a database is and which databases are typically used with ASP.NET pages What SQL is, how it looks, and.
Data Virtualization Community Edition
SQL Query Joins ASP.Net 2.0 – Visual Studio 2005
Database Applications – Microsoft Access
CHAPTER 7: ADVANCED SQL.
Microsoft Enterprise Consortium Type I Subquery
MIS2502: Data Analytics SQL – Getting Information Out of a Database
Database Fundamentals
MIS2502: Data Analytics SQL – Getting Information Out of a Database Part 2: Advanced Queries Aaron Zhi Cheng
Access: SQL Participation Project
Access/SQL Server Eliminate Duplicates with SELECT DISTINCT
MIS2502: Data Analytics SQL – Getting Information Out of a Database Part 2: Advanced Queries Zhe (Joe) Deng
Presentation transcript:

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Using two or more tables in a query 1 Microsoft Enterprise Consortium Microsoft Enterprise Consortium: Microsoft Faculty Connection/Faculty Resource Center

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium What you’ll need … Log in to MEC for this lesson and into MSSMS (Microsoft SQL Server Management Studio). ◦Be sure to select your account ID under Database in the Object Explorer pane, similar to the example shown here. If there is something besides the topics for this lesson that you’re not familiar with in this presentation, please review earlier lessons in this SQL Fundamental series. 2

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Using more than one table in a query A relational database has many tables. Usually, you will want to combine information from two or more tables in a query. For example, if you create a query to list sales orders for today, you may want to include customer information and product information along with the sales order data itself. 3 Based on this example data model, 4 tables would be used in the query to list sales orders for today if query includes customer names and product names.

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Joining tables in a query 4 Tables are joined or connected in a query based on a column they have in common. For example, the CUSTOMER table is joined with the ORDER table through the CustomerID column each table has. Order is joined to Order_Item through OrderNumber. Order_Item is joined to Product through ProductID. Customer.CustomerID = Order.CustomerID Order.OrderNumber = Order_Item.OrderNumber Order_Item.ProductID = Product.ProductID

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Using 2 tables in a query Show team names and students on each team. /* Show the full team name and students on each team. Sort by team. */ select team_name, teams.teamid, stdfname, stdlname from team, students where teams.teamid = students.std_teamID order by teamID; 5 The 2 tables must be joined in the query on column they have in common. When you display a column name that appears in more than one table, you may need to add the table name to the column name in the SELECT clause. You have to do this if the column names are identical in each table. If you don’t put the table name first, you’ll get an error that the column is ambiguously defined.

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Two way to join tables: WHERE clause and JOIN operator. There are 2 ways to join tables in a query. WHERE Clause select team_name, teams. teamID, stdfname, stdlname from team, students WHERE teams.teamid = students.std_teamID order by teamID; JOIN Operator in FROM clause select team_name, teams. teamID, stdfname, stdlname from teams INNER JOIN students ON teamID = std_teamID order by teamID; 6

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Which to use? WHERE clause or JOIN operator? In many situations you can decide which method of joining tables you want to use. There are a few situations in which the method of joining tables is not just a matter of choice. (The Advanced SQL series presents some examples of this.) 7

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Using more than 2 tables in a query. Show students and their evaluations as evaluatee. Use the JOIN operator in the FROM clause. INNER JOIN … INNER is optional and often omitted. /* Show team name, teamd ID, student ID and name, semester, year and evaluation ID for each student who was evaluated (evaluatee). Sort by team name and student ID. */ /* Join with the JOIN operator in the FROM clause. */ select team_name, teams.teamID, stdid, stdfname, stdlname, evalsemester, evalyear, eval_ID from teams join students on teams.teamID = students.std_teamid join evaluations on students.stdid = evaluations.evaluateeID order by teamID, stdid; 8 Take a look at using 3 tables in a query. We’ll try both methods to join the tables.

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Each JOIN has a temporary data set /* Show team name, teamd ID, student ID and name, semester, year and evaluation ID for each student who was evaluated (evaluatee). */ select team_name, teams.teamID, stdid, stdfname, stdlname, evalsemester, evalyear, eval_ID from teams join students on teams.teamID = students.std_teamid join evaluations on students.stdid = evaluations.evaluateeID order by teamID, stdid; 9 Each JOIN creates a temporary data set and this data set is what is joined with the next JOIN operator. TeamStudents Team-Students Team-studentsEvaluations Team-Students-Evaluations

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Joining 3 tables in the WHERE clause Show students and their evaluations as evaluatee. /* Join in the WHERE clause */ select team_name, teams.teamID, stdid, stdfname, stdlname, evalsemester, evalyear, eval_ID from teams, students, evaluations where teams.teamID = students.std_teamID and students.stdid = evaluations.evaluateeID order by teamID, stdid; 10

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium What was covered … How to use more than one table in a query. Joining tables ◦WHERE clause ◦JOIN operator in the FROM clause 11

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Resources Microsoft Faculty Connection—Faculty Resource Center Microsoft Transact-SQL Reference AdventureWorks Sample Database 12