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.

Slides:



Advertisements
Similar presentations
Advanced SQL (part 1) CS263 Lecture 7.
Advertisements

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
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.
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
Stored procedures and views You can see definitions for stored procedures and views in the demo databases but you can’t change them. For views, expand.
Data Bits Many to Many Subkeys JoinsQueries Attributes $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 Final DataBit.
Microsoft SQL Server 2008 From the Program menu choose: Microsoft SQL Server 2008 R2  SQL Server Management Studio. You may see a window indicating the.
NMED 3850 A Advanced Online Design February 25, 2010 V. Mahadevan.
1 Merging with SQL HRP223 – 2011 October 31, 2011 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation.
1 Lab 2 HRP223 – 2010 October 18, 2010 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation is protected.
Introduction to Oracle9i: SQL1 Subqueries. Introduction to Oracle9i: SQL2 Chapter Objectives Determine when it is appropriate to use a subquery Identify.
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.
® IBM Software Group © 2006 IBM Corporation The Eclipse Data Perspective and Database Explorer This section describes how to use the Eclipse Data Perspective,
Learningcomputer.com SQL Server 2008 – Entity Relationships in a Database.
PHP meets MySQL.
IE 423 – Design of Decision Support Systems Database development – Relationships and Queries.
Lecture 2 of Advanced Databases Advanced SQL Instructor: Mr.Ahmed Al Astal.
Chapter 9 Joining Data from Multiple Tables
Banner and the SQL Select Statement: Part Three (Joins) Mark Holliday Department of Mathematics and Computer Science Western Carolina University 4 November.
Programming using C# Joins SQL Injection Stored Procedures
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.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
© 2008 The McGraw-Hill Companies, Inc. All rights reserved. ACCESS 2007 M I C R O S O F T ® THE PROFESSIONAL APPROACH S E R I E S Lesson 9 – Building Links,
Relationships and Advanced Query Concepts Using Multiple Tables Please use speaker notes for additional information!
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.
Introduction to SQL Server JOINS © Meganadha Reddy K., Meganadha Reddy K. Technical Trainer | NetCom Learning
ADVANCED SQL SELECT QUERIES CS 260 Database Systems.
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.
ACCESS CHAPTER 4 Tables and Queries Learning Objectives: Define table structure Enter data into a table Alter table structure Set a table’s field properties.
1 Database Design and Development: A Visual Approach © 2006 Prentice Hall Chapter 8 DATABASE DESIGN AND DEVELOPMENT: A VISUAL APPROACH Chapter 8 Creating.
CS146 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman
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.
CSC 240 (Blum)1 Introduction to Data Entry, Queries and Reports.
Relational Algebra – Part 2
Relational Databases.  In week 1 we looked at the concept of a key, the primary key is a column/attribute that uniquely identifies the rest of the data.
SQL 101 – Class 1 Lee Turner. Agenda 1. This is your life – SQL A brief history of SQL What SQL is and what it is not Normalization 2. Some Super Simple.
1 Chapter 10 Joins and Subqueries. 2 Joins & Subqueries Joins – Methods to combine data from multiple tables – Optimizer information can be limited based.
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.
When I want to work with SQL, I start off as if I am doing a regular query.
WDM3304 week 6 Table Joins. Primary keys, foreign keys Primary key: A field of a table designated to provide a unique identifier for a specific row of.
Multiple Table Queries (Inner Joins, Equijoins) Week 3.
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.
IS201 Agenda: 09/19  Modify contents of the database.  Discuss queries: Turning data stored in a database into information for decision making.  Create.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Aliya Farheen October 29,2015.
Computer Science & Engineering 2111 Lecture 13 Outer Joins 1.
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)
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.
IMS 4212: Constraints & Triggers 1 Dr. Lawrence West, Management Dept., University of Central Florida Stored Procedures in SQL Server.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 5: SQL I Rob Gleasure robgleasure.com.
Quiz Which of the following is not a mandatory characteristic of a relation? Rows are not ordered (Not required) Each row is a unique There is a.
 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.
BTM 382 Database Management Chapter 8 Advanced SQL Chitu Okoli Associate Professor in Business Technology Management John Molson School of Business, Concordia.
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.
XP Chapter 1 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Level 2 Objectives: Understanding and Creating Table.
IFS180 Intro. to Data Management Chapter 10 - Unions.
Rob Gleasure robgleasure.com
This shows the user interface and the SQL Select for a situation with two criteria in an AND relationship.
JOINs JOINs can be used to combine tables A CROSS JOIN B
Rob Gleasure robgleasure.com
Rob Gleasure robgleasure.com
Manipulating Data Lesson 3.
Navigating SSMS Primer for Beginners
Presentation transcript:

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 the k: drive in the IS475\ directory. You will see two errors, and then the tables will create and populate. There are two tables created and populated with this file – one has three rows and one has four rows of data.

What is the goal of a SQL query? To produce an accurate result table. To produce an accurate result table that contains meaningful information. To produce an accurate result table that contains meaningful information that will help solve a business problem. To produce an accurate result table that contains meaningful information that will help solve a business problem and is capable of being viewed through a front-end visualization program to make an impact.

Getting 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. SQL programmers need to understand what happens when multiple tables are accessed in a single query.

Time for some new tables!! These two tables above are created and populated with the file called “SQLLab3.sql”. If you were late to class, look back on the first page of this handout to see the location of the file and information about execution.

Questions about design on previous page Does the design indicate whether or not referential integrity is enforced in the database? Does the inclusion of a foreign key to relate tables imply that referential integrity is enforced in the database? What does it mean to say “referential integrity is enforced” vs. “referential integrity is not enforced” in a database? Is it necessary to enforce referential integrity to relate tables in a relational database?

What do we want to accomplish? A simple result table with a combination of data from the order and customer tables, as shown on the next page.

Order IDOrderDateCustIDDueDate 1002/23/ /1/ /24/ /1/ /22/ /15/2014 CustIDCustomerName 1234John Smith 2555Jane Doe 6773Bertie Wooster 8372Martin Cheng ord cust Order IDOrderDateDueDateCustomerName 1002/23/20143/1/2014 John Smith 2002/24/20144/1/2014 Bertie Wooster 3002/22/20143/15/2014 John Smith + Result Table =

SELECT * FROMord, cust Let’s try and create the result table on the previous page. Open a new query window and type the following statement.

Order IDOrderDateCustIDDueDate 1002/23/ /1/ /24/ /1/ /22/ /15/2014 CustIDCustomerName 1234John Smith 2555Jane Doe 6773Bertie Wooster 8372Martin Cheng OrderIDOrderDateCustIDDueDateCustIDCustomerName 1002/23/ /1/ John Smith 1002/23/ /1/ Jane Doe 1002/23/ /1/ Bertie Wooster 1002/23/ /1/ Martin Cheng 2002/24/ /1/ John Smith 2002/24/ /1/ Jane Doe 2002/24/ /1/ Bertie Wooster 2002/24/ /1/ Martin Cheng 3002/22/ /15/ John Smith 3002/22/ /15/ Jane Doe 3002/22/ /15/ Bertie Wooster 3002/22/ /15/ Martin Cheng +

Cartesian Product Cross Join Or

OrderIDOrderDateCustIDDueDateCustIDCustomerName 1002/23/ /1/ John Smith 1002/23/ /1/ Jane Doe 1002/23/ /1/ Bertie Wooster 1002/23/ /1/ Martin Cheng 2002/24/ /1/ John Smith 2002/24/ /1/ Jane Doe 2002/24/ /1/ Bertie Wooster 2002/24/ /1/ Martin Cheng 3002/22/ /15/ John Smith 3002/22/ /15/ Jane Doe 3002/22/ /15/ Bertie Wooster 3002/22/ /15/ Martin Cheng 1002/23/ /1/ John Smith 2002/24/ /1/ Bertie Wooster 3002/22/ /15/ John Smith

SELECT* FROMord INNER JOINcust ONord.custID = cust.custID ORDER BY ord.orderID

OrderIDOrderDateCustIDDueDateCustIDCustomerName 1002/23/ /1/ John Smith 1002/23/ /1/ Jane Doe 1002/23/ /1/ Bertie Wooster 1002/23/ /1/ Martin Cheng 2002/24/ /1/ John Smith 2002/24/ /1/ Jane Doe 2002/24/ /1/ Bertie Wooster 2002/24/ /1/ Martin Cheng 3002/22/ /15/ John Smith 3002/22/ /15/ Jane Doe 3002/22/ /15/ Bertie Wooster 3002/22/ /15/ Martin Cheng 2002/24/ /1/ Bertie Wooster 1002/23/ /1/ John Smith 3002/22/ /15/ John Smith

SELECT* FROMcust INNER JOINord ONord.custID = cust.custID ORDER BY ord.orderID Open a new query window, and type the SQL code below. This code has the customer table placed first in the FROM statement. How do the results contrast with the SQL code on slide #13?

SELECTord.orderid, ord.orderdate, ord.duedate, cust.customername FROMord INNER JOINcust ONOrd.custID = Cust.custID ORDER BYord.orderid Order IDOrderDateDueDateCustomerName 1002/23/20143/1/2014 John Smith 2002/24/20144/1/2014 Bertie Wooster 3002/22/20143/15/2014 John Smith Finalize the query by SELECTing only the required columns

Ord Cust Results of Inner Join

Let’s make a new query!

Order IDOrderDateCustIDDueDate 1002/23/ /1/ /24/ /1/ /22/ /15/2014 CustIDCustomerName 1234John Smith 2555Jane Doe 6773Bertie Wooster 8372Martin Cheng ord cust + CustomerNameOrderIDDueDate Bertie Wooster2004/1/2014 Jane DoeNo order John Smith1003/1/2014 John Smith3003/15/2014 Martin ChengNo order =

SELECTcust.CustomerName, ISNULL(ord.orderID, ‘No Order’) OrderID, ord.DueDate FROMord INNER JOINcust ONOrd.custID = Cust.custID ORDER BYcust.customername CustomerNameOrderIDDueDate Bertie Wooster2004/1/2014 John Smith1003/1/2014 John Smith3003/15/2014

Ord Cust Results of Outer JoinResults of One-Sided Outer Join

SELECTcust.CustomerName, ISNULL(ord.orderID, ‘No Order’) OrderID, ord.DueDate FROMord RIGHT OUTER JOINcust ONOrd.custID = Cust.custID ORDER BYcust.customername CustomerNameOrderIDDueDate Bertie Wooster2004/1/2014 Jane DoeNo order John Smith1003/1/2014 John Smith3003/15/2014 Martin ChengNo order

FROMord RIGHT OUTER JOINcust ord cust + = Result Table Left Side of the join Right Side of the join

Let’s say that referential integrity is not enforced and we have more rows in our tables...

Order IDOrderDateCustIDDueDate 1002/23/ /1/ /24/ /1/ /22/ /15/ /27/ /16/ /12/ /22/ /23/ /27/ /15/ /1/2014 CustIDCustomerName 1234John Smith 2555Jane Doe 6773Bertie Wooster 8372Martin Cheng Execute the script file called: SQLLab3Expand.sql on the k: drive in the IS475\ directory to create a table called “ord1”.

How many rows and columns in the cross join? SELECT * FROMord1, cust

What would the results look like from an inner join? SELECT* FROMord1 INNER JOINcust ONord1.custID = cust.custID

Order IDOrderDateCustIDDueDateC.CustIDCustomerName 1002/23/ /1/ John Smith 2002/24/ /1/ Bertie Wooster 3002/22/ /15/ John Smith 4002/27/ /16/ Jane Doe 6002/23/ /27/ Jane Doe 7002/15/ /1/ Jane Doe Why is OrderID 500 missing?

What would the results look like from a right outer join? SELECT* FROMord1 RIGHT OUTER JOINcust ONord1.custID = cust.custID

Order IDOrderDateCustIDDueDateC.CustIDCustomerName 1002/23/ /1/ John Smith 2002/24/ /1/ Bertie Wooster 3002/22/ /15/ John Smith 4002/27/ /16/ Jane Doe 6002/23/ /27/ Jane Doe 7002/15/ /1/ Jane Doe 8372Martin Cheng The row is still missing...

What would the results look like from a left outer join? SELECT* FROMord1 LEFT OUTER JOINcust ONord1.custID = cust.custID

Ord Cust Results of Outer JoinResults of One-Sided Outer Join

Order IDOrderDateCustIDDueDateC.CustIDCustomerName 1002/23/ /1/ John Smith 2002/24/ /1/ Bertie Wooster 3002/22/ /15/ John Smith 4002/27/ /16/ Jane Doe 5002/12/ /22/ /23/ /27/ Jane Doe 7002/15/ /1/ Jane Doe

How do you make the NULL data more meaningful? SELECT ord1.OrderID, ord1.OrderDate, ord1.CustID, ord1.DueDate, ISNULL(cust.CustID, ‘n/a’), ISNULL(cust.CustomerName, ‘Missing Name’) FROMord1 LEFT OUTER JOINcust ON ord1.custID = cust.custID Open a new query window and type the following query:

All rows from both tables! SELECT* FROMord1 FULL OUTER JOINcust ONord1.custID = cust.custID

Ord Cust Results of Outer Join Results of Both-Sided Outer Join

Order IDOrderDateCustIDDueDateC.CustIDCustomerName 1002/23/ /1/ John Smith 2002/24/ /1/ Bertie Wooster 3002/22/ /15/ John Smith 4002/27/ /16/ Jane Doe 5002/12/ /22/ /23/ /27/ Jane Doe 7002/15/ /1/ Jane Doe 8372Martin Cheng

Write a query on your own! Normally, we want to see all the rows in the child table, rather than the parent. Write a query that displays all the orders in the ord1 table, but include only the columns shown below: Order IDOrderDateCustIDCustomerName 1002/23/ John Smith 2002/24/ Bertie Wooster 3002/22/ John Smith 4002/27/ Jane Doe 5002/12/ Missing Name 6002/23/ Jane Doe 7002/15/ Jane Doe

Write a new query Write a query that displays all the orders placed by the customer “Jane Doe”. Assume that you don’t know Jane Doe’s customer ID and have to use her name in the WHERE clause. The join condition is the same, but you are adding a WHERE clause. The goal is to realize that once you have joined tables, you have all fields from all tables available in all parts of the query. The result table should look like the one provided below. Order IDOrderDateDueDate 4002/27/20143/16/ /23/20142/27/ /15/20144/1/2014

Write another query Write a query that displays all the orders that don’t have a valid customer. The result table should look like the one provided below. Order IDOrderDateCustIDDueDateC.CustIDCustomerName 5002/12/ /22/2014n/aMissing Name

Last 2 queries of the day… First, write a query that summarizes order data by customer. The result table should look like the one provided below. Hint: Use the GROUP BY clause. CustIDCustomerNameCountofOrders 1234John Smith2 2555Jane Doe3 6773Bertie Wooster1 Second, change it so that all customers are displayed, whether or not they have an order. The result table should look like the one provided below. CustIDCustomerNameCountofOrders 1234John Smith2 2555Jane Doe3 6773Bertie Wooster1 8372Martin Cheng0