Joins, views, and subqueries CMSC 461 Michael Wilson.

Slides:



Advertisements
Similar presentations
© Abdou Illia MIS Spring 2014
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Murach's MySQL, C3© 2012, Mike Murach & Associates, Inc.Slide 1.
Introduction to Oracle9i: SQL1 Subqueries. Introduction to Oracle9i: SQL2 Chapter Objectives Determine when it is appropriate to use a subquery Identify.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Database Systems More SQL Database Design -- More SQL1.
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.
Introduction to SQL Structured Query Language Martin Egerhill.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
IFS180 Intro. to Data Management Chapter 9 – Outer Joins.
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.
Oracle Database Administration Lecture 2 SQL language.
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.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
SQL 101 for Web Developers 14 November What is a database and why have one? Tables, relationships, normalization SQL – What SQL is and isn’t – CRUD:
Chapter 6 Additional Database Objects Oracle 10g: SQL.
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.
Week 10 Quiz 9 Answers Group 28 Christine Hallstrom Deena Phadnis.
SQL continued CMSC 461 Michael Wilson. Right back into it  Recap:  Learned how to log in to PostgreSQL  Learned about PostgreSQL data types  Learned.
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.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
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.
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.
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.
Instructor: Craig Duckett Lecture 03: Tuesday, April 14, 2015 SQL Sorting, Aggregates and Joining Tables 1.
Database Programming Section 15 – Oracle Proprietary Join Syntax and Review 1.
SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries.
Chapter 12 Subqueries and Merge Statements
Displaying Data from Multiple Tables (SQL99 Syntax with examples)
SQL/Lesson 7/Slide 1 of 32 Implementing Indexes Objectives In this lesson, you will learn to: * Create a clustered index * Create a nonclustered index.
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.
(SQL - Structured Query Language)
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
+ Complex SQL Week 9. + Today’s Objectives TOP GROUP BY JOIN Inner vs. Outer Right vs. Left.
INFANL01-3 ANALYSE 3 WEEK 3 March 2015 Institute voor Communication, Media en Informatietechnology.
SQL Select Statement IST359 M005 Yang Wang 342 Hinds
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
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.
Chapter 7 Subqueries. Chapter Objectives  Determine when it is appropriate to use a subquery  Identify which clauses can contain subqueries  Distinguish.
 CONACT UC:  Magnific training   
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.
BTM 382 Database Management Chapter 8 Advanced SQL Chitu Okoli Associate Professor in Business Technology Management John Molson School of Business, Concordia.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
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.
IFS180 Intro. to Data Management Chapter 10 - Unions.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
More SQL: Complex Queries,
MySQL Subquery Source: Dev.MySql.com
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
Database Systems: Design, Implementation, and Management Tenth Edition
LESSON Database Administration Fundamentals Inserting Data.
Chapter 4: Intermediate SQL Joins
SQL – Subqueries.
David M. Kroenke and David J
Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Contents Preface I Introduction Lesson Objectives I-2
Subqueries.
Database Systems: Design, Implementation, and Management Tenth Edition
Manipulating Data Lesson 3.
Presentation transcript:

Joins, views, and subqueries CMSC 461 Michael Wilson

Multi table selects  So, we mentioned that we can refer to multiple tables during the course of our selects  How do we do this?  What does that do for us?

Joins  We went over the natural join before  There are actually a billion types of joins you can explicitly state in ANSI SQL  Okay, fine. There are 5 types.  There are a lot of good images on the net about this, but I’m uncomfortable with their licensing terms, so I’m going to re- draw them for you

Joining on  Most joins require you to join “on” a particular statement  The statements are the same kind that are used in WHERE clauses  Table1.id = Table2.AddressBookId  The PostgreSQL refers to this as the join condition

Join types  INNER JOIN  FULL OUTER JOIN  LEFT OUTER JOIN  RIGHT OUTER JOIN  CROSS JOIN

Inner join  This is one of the more frequent types of joins  Finds all rows which meet the join condition  Example  SELECT * FROM AddressBook a INNER JOIN CallList c on a.id = c.addressBookId;  a and c are aliases

Inner join

Left outer join  Contains all records between T1 and T2 that meet the join condition, and then any records in T1 that don’t meet the join condition  Rows for which the join condition is not met, the columns of T2 are padded with nulls

Left outer join

Right outer join  Contains all records between T1 and T2 that meet the join condition, and then any records in T2 that don’t meet the join condition  Rows for which the join condition is not met, the columns of T1 are padded with nulls

Right outer join

Full outer join  Contains all records between T1 and T2 that meet the join condition, and the combination of a left outer join and right outer join are appended onto the results

Full outer join

Cross join  Cross joins have no join condition  Cross joins literally return the Cartesian product (discussed in the relational algebra slides)

Implicit join notation vs. explicit  Inner joins can be accomplished with “implicit” join notation  In other words, we don’t literally use the terms “inner join” in our select statement  We can use where clauses/multi table selects to accomplish the same thing

Explicit join  SELECT * FROM AddressBook a INNER JOIN CallList c on a.id = c.addressBookId;

Implicit join  SELECT * FROM AddressBook a, CallList c WHERE a.id = c.addressBookId;

Which to use?  Which should you use?  It’s up to you, really  My experience  I never really use explicit syntax  Implicit makes much more sense to me, personally

Outer joins, cross joins in the real world?  You can very much create a fully functional database without using left outer joins, right outer joins, etc.  These types of joins allow you to collate data in ways that would be require much more data munging  More queries, more tables  Truth be told, I didn’t realize their power until writing these slides

Examples  Get a full list of employees and determine who has not enrolled in benefits  SELECT * FROM employees LEFT OUTER JOIN benefitsEnrollment ON employees.id = benefitsEnrollment.employeeId;  If an employee hasn’t enrolled in benefits, the benefitsEnrollment columns would come back as NULL

Views  A view is kind of like a virtual table  Views are defined as queries (SELECT statements)  They can be queried like tables  They are read only  Syntax:  CREATE VIEW AS

View example  CREATE VIEW phoneNumbers AS SELECT phoneNumber FROM AddressBook;  This would create a view that only contained phone numbers from our AddressBook

More complex views  CREATE VIEW callsAndInfo AS SELECT * FROM AddressBook a INNER JOIN CallList c on a.id = c.addressBookId;  Views are more useful for more complex queries  Can join these views on other tables as well

View danger!  Depending too heavily on views can cause performance issues if you’re not careful  You can apply constraints to view queries which have a heavy performance impact  If you have views that depend on views that depend on views

Materialized views  PostgreSQL has materialized views, which are views that are stored on disk  Periodically updated and stored  Regular views are not stored, so they always hit dependencies in real time  Syntax:  CREATE MATERIALIZED VIEW AS

Subqueries  You can use selects in your selects!  This is SUPER confusing  You can use them in both the FROM and the WHERE  Used in the from, kind of like in-lining a view  Can use them in the where to grab values from other tables

Subquery  SELECT * FROM Stalkers s, (SELECT * FROM AddressBook a INNER JOIN CallList c on a.id = c.addressBookId) c WHERE s.contactName = c.contactName;

Using subqueries in a from  SELECT * FROM UserProfile WHERE userId = (SELECT userId FROM MostPopularUser ORDER BY popularity LIMIT 1);