SQL – join.

Slides:



Advertisements
Similar presentations
Database Relationships in Access As you recall, the data in a database is stored in tables. In a relational database like Access, you can have multiple.
Advertisements

SQL/PL SQL Oracle By Rana Umer. Quiz 2 Q1.Create a table called "Persons" that contains five columns: PersonID, LastName, FirstName, Address, and City.
Advanced SQL (part 1) CS263 Lecture 7.
© Abdou Illia MIS Spring 2014
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.
February 18, 2012 Lesson 3 Standard SQL. Lesson 3 Standard SQL.
Displaying Data from Multiple Tables
Introduction to Oracle9i: SQL1 Basic SQL SELECT Statements.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 3: Joins Part I.
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.
02 | Advanced SELECT Statements Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
Introduction to SQL Structured Query Language Martin Egerhill.
IFS180 Intro. to Data Management Chapter 9 – Outer Joins.
Learningcomputer.com SQL Server 2008 – Entity Relationships in a Database.
Using Relational Databases and SQL John Hurley Department of Computer Science California State University, Los Angeles Lecture 3: Joins Part I.
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
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
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
JOI/1 Data Manipulation - Joins Objectives –To learn how to join several tables together to produce output Contents –Extending a Select to retrieve data.
ADVANCED SQL SELECT QUERIES CS 260 Database Systems.
Week 10 Quiz 9 Answers Group 28 Christine Hallstrom Deena Phadnis.
CS146 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining Multiple Tables.
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.
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.
Displaying Data from Multiple Tables (SQL99 Syntax with examples)
SQL Select Statement IST359.
U:/msu/course/cse/103 Day 08, Slide 1 CSE 103 Students: –Review days 7 and 8 if you need to go over relationships and INNER.
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.
Thinking in Sets and SQL Query Logical Processing.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
CS 122: Lecture 3 Joins (Part 1) Tarik Booker CS 122 California State University, Los Angeles October 7, 2014.
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
LEC-8 SQL. Indexes The CREATE INDEX statement is used to create indexes in tables. Indexes allow the database application to find data fast; without reading.
Advanced SQL Advanced Database Dr. AlaaEddin Almabhouh.
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.
SQL – Python and Databases
Rob Gleasure robgleasure.com
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Oracle Join Syntax.
Relational Model By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany)
Indices.
02 | Advanced SELECT Statements
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
03 | Querying Multiple Tables with Joins
Querying Multiple Tables
David M. Kroenke and David J
Displaying Data from Multiple Tables Using Joins
JOINS (Joinining multiple tables)
JOINs JOINs can be used to combine tables A CROSS JOIN B
Oracle Join Syntax.
Rob Gleasure robgleasure.com
Combining Data Sets in the DATA step.
Contents Preface I Introduction Lesson Objectives I-2
Rob Gleasure robgleasure.com
Oracle Join Syntax.
Displaying Data from Multiple Tables
A – Pre Join Indexes.
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Manipulating Data Lesson 3.
Use of SQL – The Patricia database
JOINS (Joinining multiple tables)
Database Systems: Design, Implementation, and Management
Presentation transcript:

SQL – join

Multiple Relation queries Often, queries need to combine (join) rows from multiple tables. Joins specify which rows get merged with rows from a second table. As there are many possible methods to match rows together, there are many types of joins. You will need to know (memorize) the different joins. Annoying, but very useful knowledge.

students id name smithe Eric cancan Can nahumjos Josh samiam Samuel-Hunter thesaint Nicholas mafia Anthony NULL demo-student CREATE TABLE students (id TEXT, name TEXT); INSERT INTO students VALUES('smithe','Eric'); INSERT INTO students VALUES('cancan','Can'); INSERT INTO students VALUES('nahumjos','Josh'); INSERT INTO students VALUES('samiam','Samuel-Hunter'); INSERT INTO students VALUES('thesaint','Nicholas'); INSERT INTO students VALUES('mafia','Anthony'); INSERT INTO students VALUES(NULL, 'demo-student'); associates CREATE TABLE associates (responder_id TEXT, associate_id TEXT); INSERT INTO associates VALUES('cancan','samiam'); INSERT INTO associates VALUES('cancan','smithe'); INSERT INTO associates VALUES('samiam','cancan'); INSERT INTO associates VALUES('samiam','smithe'); INSERT INTO associates VALUES('mafia','thesaint'); responder_id associate_id cancan samiam smithe mafia thesaint

CROSS JOIN Combine every row in one table with every row in the other SELECT * FROM students CROSS JOIN associates; Implicit Cross Join (equivalent) SELECT * FROM students, associates; Also called the Cartesian Product (as it results in every possible combination) Not often used, because you rarely need all possible combinations.

How many Rows should be returned by a cross join between a table with M rows and a table with N rows? 2 * (M * N) N * M M + N M ^ N

Inner Join INNER JOIN is like the CROSS JOIN, except only rows that match a predicate are allowed. Frequently that predicate includes primary keys to match associated data SELECT * FROM students INNER JOIN associates ON id = responder_id; You can equivalently do an implicit inner join and use WHERE to filter rows: SELECT * FROM students, associates WHERE id = responder_id;

INNER Join Retuned rows are those that match both tables (shaded). http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/

Full Outer Join Like an INNER JOIN, the FULL OUTER JOIN includes all rows that match a predicate. In addition however, rows that don't have a match are included with NULL values where their corresponding data would be SELECT * FROM students FULL OUTER JOIN associates ON id = respondent_id; SQLite doesn't implement FULL OUTER JOIN, . But I'll show you how to do it anyways in a later lecture.

FULL OUTER JOIN http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/

LEFT OUTER JOIN LEFT OUTER JOIN includes all the rows to the left of the JOIN keyword, where there is a match, it add the matching column data to rows. If there isn't a match NULL values are used instead. SELECT * FROM students LEFT OUTER JOIN associates ON id = responder_id; Note: order matters!!! SELECT * FROM associates LEFT OUTER JOIN students

LEFT OUTER JOIN http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/

LEFT OUTER JOIN Without Matches If we want to find the rows in the left table that don't have matches, we can do a LEFT OUTER JOIN and filter out the rows with matches. SELECT * FROM students LEFT OUTER JOIN associates ON id = responder_id WHERE responder_id IS NULL;

LEFT OUTER JOIN Without Matches http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/

Which rows should a LEFT INNER Join return? Rows that match the predicate Row that match plus the unmatched rows on left Rows that are unmatched and the matches on the left ???

Self Join You can make a table join on itself. For instance, what it you wanted to find all the mutual associates (where both respondents marked each other as associates). SELECT * FROM associates AS a1 -- We need aliases INNER JOIN associates AS a2 -- Another Alias ON a1.responder_id = a2.associate_id -- first other AND a1.associate_id = a2.responder_id -- Other match first WHERE a1.responder_id < a2.responder_id; -- remove dupes

Natural joins Natural Joins are joins where column names that are shared between two tables in a join are assumed to be keys of each other. The rows returned are the ones that match in the shared column names They are bad practice in general as coincidental column name matches will break the query. SELECT * FROM students NATURAL JOIN associates; This is the same as cross join as there are no column names in common.

Types of Relations Often tables tend to break into two categories (I made this up, so it won't be in any textbook): Entity Tables – they map a primary key to data about an entity Relation Tables – they map relationships between primary keys in tables together If you can break up your tables into these types, it makes for easier joins and less duplication. students (id TEXT, name TEXT) is an Entity Table associates (responder_id TEXT, associate_id TEXT) is a Relation Table These aren't hard a fast rules, just suggestions.