SQL JOINs and VIEWs. Chapter 3.3 V3.0. Copyright @ Napier University Dr Gordon Russell



Similar documents
SQL Simple Queries. Chapter 3.1 V3.0. Napier University Dr Gordon Russell

Chapter 9 Joining Data from Multiple Tables. Oracle 10g: SQL

SQL. by Steven Holzner, Ph.D. ALPHA. A member of Penguin Group (USA) Inc.

Relational Algebra 2. Chapter 5.2 V3.0. Napier University Dr Gordon Russell

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

Relational Database: Additional Operations on Relations; SQL

Displaying Data from Multiple Tables. Copyright 2006, Oracle. All rights reserved.

Lab # 5. Retreiving Data from Multiple Tables. Eng. Alaa O Shama

Oracle Database 12c: Introduction to SQL Ed 1.1

Displaying Data from Multiple Tables. Copyright 2004, Oracle. All rights reserved.

D61830GC30. MySQL for Developers. Summary. Introduction. Prerequisites. At Course completion After completing this course, students will be able to:

Lecture Slides 4. SQL Summary. presented by Timothy Heron. Indexes. Creating an index. Mathematical functions, for single values and groups of values.

MySQL for Beginners Ed 3

PSU SQL: Introduction. SQL: Introduction. Relational Databases. Activity 1 Examining Tables and Diagrams

Introduction to database design

Displaying Data from Multiple Tables

Displaying Data from Multiple Tables. Copyright 2004, Oracle. All rights reserved.

Where? Originating Table Employees Departments

Displaying Data from Multiple Tables

RDBMS Using Oracle. Lecture Week 7 Introduction to Oracle 9i SQL Last Lecture. kamran.munir@gmail.com. Joining Tables

Using SQL Queries to Insert, Update, Delete, and View Data: Joining Multiple Tables. Lesson C Objectives. Joining Multiple Tables

Define terms Write single and multiple table SQL queries Write noncorrelated and correlated subqueries Define and use three types of joins

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Displaying Data from Multiple Tables. Chapter 4

Database CIS 340. lab#6. I.Arwa Najdi

Part A: Data Definition Language (DDL) Schema and Catalog CREAT TABLE. Referential Triggered Actions. CSC 742 Database Management Systems

In this Lecture SQL SELECT. Example Tables. SQL SELECT Overview. WHERE Clauses. DISTINCT and ALL SQL SELECT. For more information

Toad for Data Analysts, Tips n Tricks

Databases What the Specification Says

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries

Review: Participation Constraints

Oracle Database 10g: Introduction to SQL

Select the Crow s Foot entity relationship diagram (ERD) option. Create the entities and define their components.

LiTH, Tekniska högskolan vid Linköpings universitet 1(7) IDA, Institutionen för datavetenskap Juha Takkinen

Lecture 4: More SQL and Relational Algebra

The Query Builder: The Swiss Army Knife of SAS Enterprise Guide

Paper TU_09. Proc SQL Tips and Techniques - How to get the most out of your queries

5.5. Solving linear systems by the elimination method


GET DATA FROM MULTIPLE TABLES QUESTIONS

TIM 50 - Business Information Systems

Introduction to tuple calculus Tore Risch

Access Tutorial 1 Creating a Database

DATABASE DESIGN AND IMPLEMENTATION II SAULT COLLEGE OF APPLIED ARTS AND TECHNOLOGY SAULT STE. MARIE, ONTARIO. Sault College

2Creating Reports: Basic Techniques. Chapter

Excel 2010 Sorting and Filtering

Unit 3. Retrieving Data from Multiple Tables

Relational Algebra. Module 3, Lecture 1. Database Management Systems, R. Ramakrishnan 1

Relational Databases

Oracle Database 11g: SQL Tuning Workshop

Word 2007 WOWS of Word Office 2007 brings a whole new basket of bells and whistles for our enjoyment. The whistles turn to wows.

Examples of DIVISION RELATIONAL ALGEBRA and SQL. r s is used when we wish to express queries with all :

Database Administration with MySQL

CATIA Basic Concepts TABLE OF CONTENTS

Beginning C# 5.0. Databases. Vidya Vrat Agarwal. Second Edition

Row Echelon Form and Reduced Row Echelon Form

Improving Productivity using IT - Level 3 Scenario Assignment Sample Test 4 Version SampleMQTB/1.0/IP3/v1.0. Part 1 Performance

Oracle SQL. Course Summary. Duration. Objectives

ICAB4136B Use structured query language to create database structures and manipulate data

Oracle Database: SQL and PL/SQL Fundamentals

Personal Geodatabase 101

Oracle 10g PL/SQL Training

Microsoft Access 3: Understanding and Creating Queries

and what does it have to do with accounting software? connecting people and business

OA3-10 Patterns in Addition Tables

Microsoft Excel 2007 Consolidate Data & Analyze with Pivot Table Windows XP

LSP 121. LSP 121 Math and Tech Literacy II. Simple Databases. Today s Topics. Database Class Schedule. Simple Databases

1.264 Lecture 10. Data normalization

Netezza Workbench Documentation

Supporting Data Set Joins in BIRT

Relational model. Relational model - practice. Relational Database Definitions 9/27/11. Relational model. Relational Database: Terminology

Querying Microsoft SQL Server (20461) H8N61S

Using Multiple Operations. Implementing Table Operations Using Structured Query Language (SQL)

Guide to SQL Programming: SQL:1999 and Oracle Rdb V7.1

DIPLOMA IN WEBDEVELOPMENT

Querying Microsoft SQL Server

CSC 443 Data Base Management Systems. Basic SQL

Presentation of database relational schema (schema 1) Create Universe step by step

3. Relational Model and Relational Algebra

Oracle Database 10g Express

Creating Charts in Microsoft Excel A supplement to Chapter 5 of Quantitative Approaches in Business Studies

Foundations & Fundamentals. A PROC SQL Primer. Matt Taylor, Carolina Analytical Consulting, LLC, Charlotte, NC

Keymaker_for_ML. Connect adapter to USB port of your PC and install drivers. Find it on CD.

Pizza. Pizza with Everything Identifying & Estimating Fractions up to 1. Serves 2-4 players / Grades 3+ (Includes a variation for Grades 1+)

Oracle Database: SQL and PL/SQL Fundamentals NEW

SpatiaLite Using SQL Views is really simple and easy a very short introduction

Typical Linear Equation Set and Corresponding Matrices

Storing SpamAssassin User Data in a SQL Database Michael Parker. [ Start Slide ] Welcome, thanks for coming out today.

Database Assistant. Once Database Assistant is installed you must login to gain access to the database. Copyright 2009

INTRODUCTION TO DESKTOP PUBLISHING

CHAPTER 12. SQL Joins. Exam Objectives

Relational Algebra and SQL

The Relational Model. Why Study the Relational Model? Relational Database: Definitions. Chapter 3

The Relational Model. Why Study the Relational Model? Relational Database: Definitions

Creating tables of contents and figures in Word 2013

Introducing the hard disc recorder and Beo4, Daily use, Advanced operation, Preference settings, Set up the hard disc recorder, On-screen menus,

Check out our website!

8.1. Cramer s Rule for Solving Simultaneous Linear Equations. Introduction. Prerequisites. Learning Outcomes. Learning Style

ICE for Eclipse. Release 9.0.1

Transcription:

SQL JOINs and VIEWs Chapter 3.3 V3.0 Copyright @ Napier University Dr Gordon Russell

Multiple source tables Using more than a single table of a database is usually essential. The basic form is just to list all the needed tables in the from line. You do have to explain to the DBMS how the tables should be joined together

JOIN conditions SELECT * from driver; NAME DOB

SELECT * from car; REGNO F611 AAA J111 BBB A155 BDE K555 GHT SC04 BFE MAKE FORD SKODA MERCED ES FIAT SMART COLOUR RED GREEN PRICE 12000 11000 22000 6000 13000 OWNER

SELECT * FROM car,driver REGNO MAKE COLOUR PRICE OWNER NAME DOB F611 AAA FORD RED 12000 J111 BBB SKODA 11000 A155 BDE MERCEDES 22000 K555 GHT FIAT GREEN 6000 SC04 BFE SMART 13000 F611 AAA FORD RED 12000 J111 BBB SKODA 11000 A155 BDE MERCEDES 22000 K555 GHT FIAT GREEN 6000 SC04 BFE SMART 13000 F611 AAA FORD RED 12000 J111 BBB SKODA 11000 A155 BDE MERCEDES 22000 K555 GHT FIAT GREEN 6000 SC04 BFE SMART 13000

FOREIGN KEY link REGNO MAKE COLOUR PRICE OWNER NAME DOB F611 AAA FORD RED 12000 J111 BBB SKODA 11000 A155 BDE MERCEDES 22000 K555 GHT FIAT GREEN 6000 SC04 BFE SMART 13000 F611 AAA FORD RED 12000 J111 BBB SKODA 11000 A155 BDE MERCEDES 22000 K555 GHT FIAT GREEN 6000 SC04 BFE SMART 13000 F611 AAA FORD RED 12000 J111 BBB SKODA 11000 A155 BDE MERCEDES 22000 K555 GHT FIAT GREEN 6000 SC04 BFE SMART 13000

Traditional JOIN SELECT * FROM car,driver WHERE owner = name; REGNO MAKE COLOUR PRICE OWNER NAME DOB F611 AAA FORD RED 12000 J111 BBB SKODA 11000 A155 BDE MERCEDES 22000 K555 GHT FIAT GREEN 6000

Modern JOIN SELECT * FROM car JOIN driver ON ( owner = name ) ; REGNO MAKE COLOUR PRICE OWNER NAME DOB F611 AAA FORD RED 12000 J111 BBB SKODA 11000 A155 BDE MERCEDES 22000 K555 GHT FIAT GREEN 6000

OUTER JOIN Consider the last line of the unconstrained join REGNO MAKE COLOUR PRICE OWNER NAME DOB SC04 BFE SMART 13000 This is a car without an owner. Sometimes we want to see the rows that fail the join condition due to NULL values. To see NULL joins we use OUTER JOIN. The JOIN discussed up to this point is known as INNER JOIN. We will discuss this more in the relational algebra section.

Really outer join means we want to force all the rows in one of the tables to appear in the result. There are some variants to OUTER JOIN, depending on which rows you want to keep.

Consider this: FROM car JOIN driver on (driver = name) To the RIGHT of the JOIN To the LEFT of the JOIN If you want all the rows in CAR to always be in the answer, you need a LEFT OUTER JOIN If you want all the rows in DRIVER to always be in the answer, you need a RIGHT OUTER JOIN

In our example the NULL causing the problem is in OWNER, which is in the table CAR. CAR is to the left of the word JOIN We need a LEFT OUTER JOIN (which is written LEFT JOIN). SELECT * FROM car LEFT JOIN driver ON ( owner = name ) ;

SELECT * FROM car LEFT JOIN driver ON ( owner = name ) ; REGNO MAKE COLOUR PRICE OWNER NAME DOB F611 AAA FORD RED 12000 J111 BBB SKODA 11000 A155 BDE MERCEDES 22000 K555 GHT FIAT GREEN 6000 SC04 BFE SMART 13000

FULL OUTER JOIN With a LEFT OUTER join, you keep all the rows from the left. With a RIGHT OUTER join, you keep all the rows from the right. What if you want to keep all the rows from both sides? You need FULL OUTER join, known as FULL JOIN. Consider a new row, David Davis, to DRIVER. David owns no cars. NAME David Davis DOB 1 Oct 1975

Example: LEFT SELECT * FROM car LEFT JOIN driver ON ( owner = name ) ; REGNO MAKE COLOUR PRICE OWNER NAME DOB F611 AAA FORD RED 12000 J111 BBB SKODA 11000 A155 BDE MERCEDES 22000 K555 GHT FIAT GREEN 6000 SC04 BFE SMART 13000

Example: Right SELECT * FROM car RIGHT JOIN driver ON ( owner = name ) ; REGNO MAKE COLOUR PRICE OWNER NAME DOB F611 AAA FORD RED 12000 J111 BBB SKODA 11000 A155 BDE MERCEDES 22000 K555 GHT FIAT GREEN 6000 David Davis 1 Oct 1975

Example: Full SELECT * FROM car FULL JOIN driver ON ( owner = name ) ; REGNO MAKE COLOUR PRICE OWNER NAME DOB F611 AAA FORD RED 12000 J111 BBB SKODA 11000 A155 BDE MERCEDES 22000 K555 GHT FIAT GREEN 6000 SC04 BFE SMART 13000 David Davis 1 Oct 1975

Naming Sometimes column names can be ambiguous. Take two tables, called ALPHA and BRAVO. Lets assume each table has a column called NAME. SELECT name from ALPHA,BRAVO; This query fails, as since NAME is in both tables the DBMS cannot work out which NAME you want. You can tell the DBMS, by putting the table name in front of the column name, separated by a dot. SELECT alpha.name from ALPHA,BRAVO;

Aliases If you are writing a big query, you can find yourself typing the same long table names in again and again. This can be quite prone to errors. SQL allows us to rename tables for the duration of a query. You put the new name immediately after the table name in FROM, separated by a space. Rather than: SELECT car.owner FROM car; You can say SELECT c.owner FROM car c;

This also works for JOIN. SELECT c.regno, c.owner, d.dob FROM car c JOIN driver d on (c.owner = d.name) It makes it much easier for the user to work out what values are coming from which table.

Selfjoin A selfjoin, or an equijoin, is where the same table is used twice in a FROM line. It indicates a need to use a single table in two different ways simultaneously. Consider the question Who drives a car the same colour as?

SELECT colour FROM car WHERE owner = ; Colour SELECT owner FROM car WHERE colour = AND owner!= AND owner not null; owner

SELECT other.owner FROM car bobsmith, car other WHERE bobsmith.colour = other.colour -- 1 AND bobsmith.owner = -- 2 AND bobsmith.owner!= other.owner -- 3 AND other.owner NOT NULL -- 4 ;

VIEWS VIEWS are to a database what subroutines are to a programming language. A view allows us to store a query in the database, so that we can access it later by name. We treat views in the same way as a normal table when writing queries.

Lets write a query to tell us how many drivers and how many cars are in the database. We could write them separately SELECT count(*) from DRIVER; SELECT count(*) from CAR; However this is two queries, and we want to do it in one query Lets store the two queries in two different VIEWs.

CREATE VIEW count1 (total) AS select count(*) from driver; CREATE VIEW count2 (total) AS select count(*) from car; Select * from count1; Total 3 Select * from count2; Select count1.total,count2.total from count1,count2; Total 5 Total 3 Total 5

Deleting a VIEW When you are finished with a VIEW you have created you can delete it. You do with with the DROP VIEW command. For our example DROP VIEW count1; DROP VIEW count2;