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



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

Oracle Database 12c: Introduction to SQL Ed 1.1

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

Oracle SQL. Course Summary. Duration. Objectives

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

MySQL for Beginners Ed 3

A basic create statement for a simple student table would look like the following.

Oracle Database: SQL and PL/SQL Fundamentals NEW

Oracle Database: SQL and PL/SQL Fundamentals

SQL Server for developers. murach's TRAINING & REFERENCE. Bryan Syverson. Mike Murach & Associates, Inc. Joel Murach

Oracle Database 10g: Introduction to SQL

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

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

Oracle 10g PL/SQL Training

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

Oracle Database: SQL and PL/SQL Fundamentals

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

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

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

MOC 20461C: Querying Microsoft SQL Server. Course Overview

Effective Use of SQL in SAS Programming

Structured Query Language (SQL)

Querying Microsoft SQL Server

1 Structured Query Language: Again. 2 Joining Tables

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

Oracle Database 11g SQL

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

Information Systems SQL. Nikolaj Popov

More on SQL. Juliana Freire. Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan

Displaying Data from Multiple Tables

Course ID#: W 35 Hrs. Course Content

Where? Originating Table Employees Departments

Join Example. Join Example Cart Prod Comprehensive Consulting Solutions, Inc.All rights reserved.

Querying Microsoft SQL Server 20461C; 5 days

Database Programming with PL/SQL: Learning Objectives

Querying Microsoft SQL Server Course M Day(s) 30:00 Hours

SUBQUERIES AND VIEWS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 6

MOC QUERYING MICROSOFT SQL SERVER

DBMS / Business Intelligence, SQL Server

Advance DBMS. Structured Query Language (SQL)

Introducing Microsoft SQL Server 2012 Getting Started with SQL Server Management Studio

Introduction to Microsoft Jet SQL

Course 20461C: Querying Microsoft SQL Server Duration: 35 hours

Database Design. Marta Jakubowska-Sobczak IT/ADC based on slides prepared by Paula Figueiredo, IT/DB

SQL Databases Course. by Applied Technology Research Center. This course provides training for MySQL, Oracle, SQL Server and PostgreSQL databases.

14 Databases. Source: Foundations of Computer Science Cengage Learning. Objectives After studying this chapter, the student should be able to:

Querying Microsoft SQL Server 2012

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

Course 10774A: Querying Microsoft SQL Server 2012

Course 10774A: Querying Microsoft SQL Server 2012 Length: 5 Days Published: May 25, 2012 Language(s): English Audience(s): IT Professionals

SQL Server. 1. What is RDBMS?

Business Systems Analysis Certificate Program. Millennium Communications & Training Inc. 2013, All rights reserved

Oracle Database 11g: SQL Tuning Workshop Release 2

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

Programming with SQL

There are five fields or columns, with names and types as shown above.

Using Temporary Tables to Improve Performance for SQL Data Services

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

Course -Oracle 10g SQL (Exam Code IZ0-047) Session number Module Topics 1 Retrieving Data Using the SQL SELECT Statement

Relational Database: Additional Operations on Relations; SQL

Advanced Query for Query Developers

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

SQL SELECT Query: Intermediate

Outline. SAS-seminar Proc SQL, the pass-through facility. What is SQL? What is a database? What is Proc SQL? What is SQL and what is a database

Relational Databases


Useful Business Analytics SQL operators and more Ajaykumar Gupte IBM

The process of database development. Logical model: relational DBMS. Relation

Discovering SQL. Wiley Publishing, Inc. A HANDS-ON GUIDE FOR BEGINNERS. Alex Kriegel WILEY

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

Oracle Database 10g Express

Instant SQL Programming

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

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT. COURSE CURRICULUM COURSE TITLE: DATABASE MANAGEMENT (Code: ) Information Technology

Data Tool Platform SQL Development Tools

- Eliminating redundant data - Ensuring data dependencies makes sense. ie:- data is stored logically

Relational Division and SQL

Oracle Database: SQL and PL/SQL Fundamentals NEW

1. SELECT DISTINCT f.fname FROM Faculty f, Class c WHERE f.fid = c.fid AND c.room = LWSN1106

Unit 3. Retrieving Data from Multiple Tables

Overview. Introduction to Database Systems. Motivation... Motivation: how do we store lots of data?

Introduction to SQL C H A P T E R3. Exercises

SQL Tables, Keys, Views, Indexes

Oracle Database: Introduction to SQL

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

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

Oracle Database 11g: SQL Tuning Workshop

1. INTRODUCTION TO RDBMS

Oracle Database: Introduction to SQL

The Relational Model. Why Study the Relational Model?

Displaying Data from Multiple Tables. Chapter 4

GET DATA FROM MULTIPLE TABLES QUESTIONS

Introduction to Querying & Reporting with SQL Server

WRITING EFFICIENT SQL. By Selene Bainum

Chapter 5. SQL: Queries, Constraints, Triggers

3. Relational Model and Relational Algebra

FHE DEFINITIVE GUIDE. ^phihri^^lv JEFFREY GARBUS. Joe Celko. Alvin Chang. PLAMEN ratchev JONES & BARTLETT LEARN IN G. y ti rvrrtuttnrr i t i r

Demystified CONTENTS Acknowledgments xvii Introduction xix CHAPTER 1 Database Fundamentals CHAPTER 2 Exploring Relational Database Components

C HAPTER 4 INTRODUCTION. Relational Databases FILE VS. DATABASES FILE VS. DATABASES

Performing Queries Using PROC SQL (1)

Transcription:

Using SQL Queries to Insert, Update, Delete, and View Data: Joining Multiple Tables Wednesay 9/24/2014 Abdou Illia MIS 4200 - Fall 2014 Lesson C Objectives After completing this lesson, you should be able to: Create SQL queries that join multiple tables Create nested SQL queries Combine query results using SET operators Create and use database views 2 Joining Multiple Tables a Join Combines data from multiple tables using foreign key references Syntax SELECT column1, column2, FROM table1, table2 WHERE table1.joincolumn = table2.joincolumn AND search_condition(s); SELECT s_id, s_last, f_last FROM student, faculty WHERE student.f_id = faculty.f_id AND f_last IN ( Marx, Zhulin ); 3 1

Joining Multiple Tables (continued) Must qualify column name in SELECT clause Specify name of table that contains column followed by period then column name Example: SELECT s_id, s_last, student.f_id Join condition Specifies table names to be joined and column names on which to join tables Example: WHERE student.f_id = faculty.f_id 4 Inner Joins Simplest type of join Also called: Equality join, Equijoin, Natural join VALUES in one table equal to values in other table Query design diagram helps get the query right SELECT s_id, s_last, s_first, student.f_id, f_last FROM student, faculty WHERE student.f_id = faculty.f_id; Could be replaced by: FROM Student NATURAL JOIN faculty; 5 Display column, search column, join column Display columns: appear in SELECT clause Search columns: appear in search condition Join columns: primary key and foreign key column on which you join the tables. Linkage table: contains join column to link other tables through foreign key values. SELECT f_last FROM faculty, course_section, term WHERE faculty.f_id = course_section.f_id AND course_section.term_id = term.term_id 6 AND term_desc = 'Summer 2007'; 2

Deriving a SQL Query From a Query Design Diagram 4 tables, 3 links All 4 tables must be named in the FROM clause Query must have 3 join conditions because there are 3 links Always 1 fewer join condition than number of tables that query joins. If you omit one join condition, the query creates a Cartesian product (every row in one table is joined with every row in other table) with more row than expected. SELECT course_name, grade FROM student, enrollment, course_section, course WHERE student.s_id = enrollment.s_id AND enrollment.c_sec_id = course_section.c_sec_id AND course_section.course_no = course.course_no AND s_last = 'Jones' AND s_first = 'Tammy'; Search conditions 7 Outer Joins Inner joins return row only if values exist in all joined tables Outer joins return all rows from one table (called inner table) and only matching rows from second table (outer table) Syntax: inner_table.join_col = outer_table.join_col(+) (+) operator signals Oracle to insert NULL for columns from the outer table with no matching rows in the inner table. 8 Self-join Query that joins table to itself Must create table alias Alternate name assigned to table in query s FROM clause Syntax: FROM table1 alias1, table1 alias2 9 3

Creating Nested Queries Nested query Consists of a main query and one or more subqueries Main query First query that appears in SELECT command Subquery Retrieves values that main query s search condition must match Subquery is evaluated first. Then, DBMS substitute subquery s output into main query. 10 Creating Nested Queries Q: What would happen if a subquery generated more values than the main query is expecting? 11 Creating subqueries that return multiple values 12 4

Using Multiple Subqueries Within a Nested Query Use AND and OR operators To join search conditions associated with subqueries 13 Using SET operators to combine Query Results UNION Queries must have same number of display column in their SELECT clause Corresponding display columns must have same data type Note: S_LAST, S_FIRST, S_PHONE used as display title even though there are faculty members names displayed along with students. 14 Using SET operators to combine Query Results INTERSECT Queries must have same number of display column in their SELECT clause Corresponding display columns must have same data type Suppresses duplicates 15 5

Using SET operators to combine Query Results MINUS Queries must have same number of display column in their SELECT clause Corresponding display columns must have same data type Suppresses duplicates Finds difference between two query results 16 Creating and Using Database Views Source query Used to create view Specify subset of single table s columns or rows or join multiple tables Updatable views Can be used to update database Syntax CREATE VIEW view_name AS source_query; Or CREATE OR REPLACE VIEW view_name AS source_query; 17 Removing Views DROP VIEW command Remove view from user schema Syntax DROP VIEW view_name; 18 6