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.

Slides:



Advertisements
Similar presentations
Chapter 4 Joining Multiple Tables
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.
February 18, 2012 Lesson 3 Standard SQL. Lesson 3 Standard SQL.
What is a Database By: Cristian Dubon.
SUNY Morrisville-Norwich Campus-Week 10 CITA 130 Advanced Computer Applications II Spring 2005 Prof. Tom Smith.
Concepts of Database Management Sixth Edition
Concepts of Database Management Sixth Edition
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.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
Microsoft Access Lesson 3
Introduction –All information systems create, read, update and delete data. This data is stored in files and databases. Files are collections of similar.
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
IFS180 Intro. to Data Management Chapter 9 – Outer Joins.
Learningcomputer.com SQL Server 2008 – Entity Relationships in a Database.
Concepts of Database Management, Fifth Edition Chapter 4: The Relational Model 3: Advanced Topics.
Chapter 4 The Relational Model 3: Advanced Topics Concepts of Database Management Seventh Edition.
HAP 709 – Healthcare Databases SQL Data Manipulation Language (DML) Updated Fall, 2009.
RELATIONSHIPS Generally there are two main database types: flat-file and relational.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
IE 423 – Design of Decision Support Systems Database development – Relationships and Queries.
Lecture 2 of Advanced Databases Advanced SQL Instructor: Mr.Ahmed Al Astal.
SQL Joins Oracle and ANSI Standard SQL Lecture 6.
Chapter 9 Joining Data from Multiple Tables
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
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.
Programming using C# Joins SQL Injection Stored Procedures
1 Normalization Normalization intro Normalization intro First normal form (1NF) First normal form (1NF) Second normal form (2NF) Second normal form (2NF)
XP New Perspectives on Microsoft Office Access 2003 Tutorial 9 1 Microsoft Office Access 2003 Tutorial 9 – Using Action Queries, and Defining Table Relationships.
HSCI 709 SQL Data Definition Language. SQL Standard SQL-92 was developed by the INCITS Technical Committee H2 on Databases. SQL-92 was designed to be.
1 Definition of a subquery Nested subqueries Correlated subqueries The ISNULL function Derived tables The EXISTS operator Mixing data types: CAST & CONVERT.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
JOI/1 Data Manipulation - Joins Objectives –To learn how to join several tables together to produce output Contents –Extending a Select to retrieve data.
Views Lesson 7.
CS146 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman
M1G Introduction to Database Development 5. Doing more with queries.
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.
IST 220 Introduction to Databases Course Wrap-up.
Introduction to Database using Microsoft Access 2013 Part 7 November 19, 2014.
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.
McGraw-Hill/Irwin The O’Leary Series © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft Access 2002 Lab 3 Analyzing Tables and Creating.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
1 SQL III CIS2450 Advanced Programming Concepts. 2 The Join Operation It is one of the most important features of a relational system that it allows you.
1 DBS201: More on SQL Lecture 3. 2 Agenda How to use SQL to update table definitions How to update data in a table How to join tables together.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
Views, Algebra Temporary Tables. Definition of a view A view is a virtual table which does not physically hold data but instead acts like a window into.
Physical Database Design Purpose- translate the logical description of data into the technical specifications for storing and retrieving data Goal - create.
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
SQL server Section 2&3. What are Data Types Character Data Types Number Data Types Date and Time Data Types CAST and CONVERT functions TRY_PARSE and TRY_CONVERT.
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.
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.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
Slide 1 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla In this session, you will learn to: Query data by using joins Query.
 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.
CSC314 DAY 9 Intermediate SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall USING AND DEFINING VIEWS  Views provide users controlled.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Advanced SQL Advanced Database Dr. AlaaEddin Almabhouh.
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.
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Quiz Questions Q.1 An entity set that does not have sufficient attributes to form a primary key is a (A) strong entity set. (B) weak entity set. (C) simple.
David M. Kroenke and David J
Contents Preface I Introduction Lesson Objectives I-2
Relational Database Design
A – Pre Join Indexes.
Manipulating Data Lesson 3.
Trainer: Bach Ngoc Toan– TEDU Website:
Presentation transcript:

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 VIEW + DROP VIEW Creating VIEWs in Management Studio Steen Jensen, autumn 2013

 A JOIN joins together information from two tables into one result set  A JOIN match up one record with one or more other records to make a record, that is a superset created by the combined columns of both records 2 Joined in a one-to-one relationship

3 Joined in a one-to-many relationship

 There are 4 kinds of joins: INNER JOIN OUTER JOIN (both LEFT and RIGHT) FULL JOIN CROSS JOIN  INNER JOIN is the most used  OUTER JOIN is a bit more rarely used  FULL JOIN is almost never used  CROSS JOIN is seldom used, but could be used for generating test data 4

5  INNER JOIN is the most common kind of JOIN  An INNER JOIN returns only records, where there are matches for the field(s) used in the JOIN NB! Bogart not included

6  The general syntax for an INNER JOIN:  In general avoid using the * operator for both tables in JOINS

7  Before making a JOIN it could be a good idea to draw a diagram showing, how the tables should be connected Person.Person ….. Businessentityid … HumanResources.Employee ….. Businessentityid … Person.BusinessEntity ….. Businessentityid …

8  The INNER JOIN corresponding to the previous slide:  Also see the following links:

9  Where an INNER JOIN is exclusive by nature, an OUTER JOIN is inclusive  The first named table is considered to be the left, and the second named table is considered to be the right  The keyword OUTER is optional, just include either LEFT or RIGHT  A LEFT OUTER JOIN includes all the information from the left table  A RIGHT OUTER JOIN includes all the information from the right table  E.g.: you want to see, what the special offers are for which products

10

11 Also see the following links:

12  A FULL JOIN matches up data on both sides of the JOIN with everything included, no matter which side of the JOIN it is on  FULL JOIN is almost never used  A FULL JOIN is best described, as what you would get, if you could do a LEFT JOIN and a RIGHT JOIN in the same JOIN  You get all records, that match based on the JOIN field(s)  You also get any records that exist only on the left side with NULLs being returned foir columns from the right side and vice versa

13  A CROSS JOIN differs from other JOINs, as there is no ON operator, and it joins every record on one side with every record on the other side of the JOIN

14  An alternative (and maybe older) version of an INNER JOIN:

15  Complex OUTER joins: page  Alternative OUTER / CROSS joins: page 119middle–121  UNION: page

 Experiment making different SQL JOINS for The AdwentureWorks and/or The Amazon database  Try both INNER and OUTER JOIN  Before making the JOIN it might be a good idea to make a diagram with the connections between the primary and the foreign keys (see slide …) 16

 A VIEW is nothing more than a stored query  You can create a simple query, that selects from only bone table and leaves some rows or columns out, or you can create a complex query that joins several tables 17

 The general syntax:  An example:  After this the VIEW can be used just as any normal table in a SELECT 18

 A view is never going to run as fast, as if you had just run the underlying SELECT statement directly  VIEWs exist for a reason: security or simplification for the user – just balance your need against the extra overhead  If you want to restrict some columns for a group of people, one solution might be to make an extra table – this gives some disadvantages: Disk space used twice Synchronization problems, where one table is updated and the other not Double I/O operations, as two tables have to be maintained  In this case a VIEW is a better solution 19

 Just as the WHERE clause on a SELECT can filter the results of a query, so can a WHERE be used on a VIEW 20

21

22

23  DATEADD + CAST: page 361middle-365  WITH CHECK OPTION: page 365–367top

 Just like the layout of a table can be changed with the ALTER TABLE, the SQL command ALTER VIEW can change the layout of a VIEW  Likewise the command DROP VIEW deletes one or more VIEWs 24

 Apart from the SQL way views can also be created the GUI way  Just expand the database node and right-click Views  Follow the instructions on page in the book 25

 Experiment making different views for The AdwentureWorks and/or The Amazon database  Try making views both the SQL way and the GUI way (see previous slide) 26