Joins. Joins In order to maintain normalization in the database design it is necessary to break up data into separate tables. The data can then be re-associated.

Slides:



Advertisements
Similar presentations
Sometimes you need to use data from more than one table. In example1, the report displays data from two separate tables. Employee IDs exist in the EMPLOYEES.
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.
CSC271 Database Systems Lecture # 13. Summary: Previous Lecture  Grouping through GROUP BY clause  Restricted groupings  Subqueries  Multi-Table queries.
Chapter 11 Group Functions
Writing Basic SQL SELECT Statements. Capabilities of SQL SELECT Statements A SELECT statement retrieves information from the database. Using a SELECT.
1 Minggu 4, Pertemuan 8 SQL: Data Manipulation (Cont.) Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
Introduction to Oracle9i: SQL1 Basic SQL SELECT Statements.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Indexing and Joins. Indexes Increase database performanceIncrease database performance must be explicitly definedmust be explicitly defined once defined,
Chapter 6 SQL: Data Manipulation Cont’d. 2 ANY and ALL u ANY and ALL used with subqueries that produce single column of numbers u ALL –Condition only.
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.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
Sorting data and Other selection Techniques Ordering data results Allows us to view our data in a more meaningful way. Rather than just a list of raw.
Displaying Data from Multiple Tables. Obtaining Data from Multiple Tables Sometimes you need to use data from more than one table. In the example, the.
Relational Model & Relational Algebra. 2 Relational Model u Terminology of relational model. u How tables are used to represent data. u Connection between.
Basic SQL. Implementation Schemes: Once we have a set of relation schemes, we can translate them into implementation schemes. We use to express implementation.
Lecture 2 of Advanced Databases Advanced SQL Instructor: Mr.Ahmed Al Astal.
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.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
CSC271 Database Systems Lecture # 12. Summary: Previous Lecture  Row selection using WHERE clause  WHERE clause and search conditions  Sorting results.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
Chapter 6 SQL: Data Manipulation (Advanced Commands) Pearson Education © 2009.
JOI/1 Data Manipulation - Joins Objectives –To learn how to join several tables together to produce output Contents –Extending a Select to retrieve data.
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.
Join, Subqueries and set operators. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
Agenda for 02/16/2006 Answer any questions about SQL project. Do you want to see any of the answers for the queries? Discuss additional formatting options.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Structured Query Language
1 Geog 357 – Introduction to GIS The Relational Language.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Displaying Data from Multiple Tables (SQL99 Syntax with examples)
1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.
Query Processing – Query Trees. Evaluation of SQL Conceptual order of evaluation – Cartesian product of all tables in from clause – Rows not satisfying.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
Session 9 Accessing Data from a Database. RDBMS and Data Management/ Session 9/2 of 34 Session Objectives Describe the SELECT statement, its syntax and.
LINQ to DATABASE-2.  Creating the BooksDataContext  The code combines data from the three tables in the Books database and displays the relationships.
SQL advanced select using Oracle 1 Multiple Tables: Joins and Set Operations Subqueries: Nested Queries.
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.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
Writing Basic SQL SELECT Statements Lecture
 CONACT UC:  Magnific training   
4 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
1 Section 3 - Select Statement u The Select statement allows you to... –Display Data –Specify Selection Criteria –Sort Data –Group Data for reporting –Use.
Advanced SQL Advanced Database Dr. AlaaEddin Almabhouh.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
IFS180 Intro. to Data Management Chapter 10 - Unions.
Joining Tables Steve Perry
CHAPTER 7 DATABASE ACCESS THROUGH WEB
MySQL Subquery Source: Dev.MySql.com
Writing Basic SQL SELECT Statements
Basic select statement
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Querying Multiple Tables
Writing Basic SQL SELECT Statements
JOINS (Joinining multiple tables)
Writing Basic SQL SELECT Statements
Writing Basic SQL SELECT Statements
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Manipulating Data Lesson 3.
JOINS (Joinining multiple tables)
Presentation transcript:

Joins

Joins In order to maintain normalization in the database design it is necessary to break up data into separate tables. The data can then be re-associated through the use of a join.

What to join? Key columns are the best since these were created for the purpose of existing as a reference.Key columns are the best since these were created for the purpose of existing as a reference. Should have similar dataShould have similar data Should be the same datatypeShould be the same datatype nulls will not join since their value is not known.nulls will not join since their value is not known.

Syntax Usually best to put the join conditions first in the WHERE clauseUsually best to put the join conditions first in the WHERE clause Use of aliases greatly simplifies the statement.Use of aliases greatly simplifies the statement. Any logical operator can be used.Any logical operator can be used. A self-join can be performed on the same table by qualifying it twice.A self-join can be performed on the same table by qualifying it twice.

Self Join Which authors in Oakland have the same zip code? Select distinct au1.au_fname, au1.au_lname, au1.zip from authors au1, authors au2 where au1.city = “Oakland” and au1.zip = au2.zip and au1.au_id != au2.au_id

Outer Joins Allow you to look at the results of a SELECT in the context of what rows did not qualify. Select fname, lname, pubname from author, pub where author.city *= pub.city This will return all the rows of the first table in the statement and with a NULL value in the column of anything that did not qualify from the second. Putting the * on the other side of the = will have the same effect on the second table.

Results _au_fname__au_lname_pub_name JohnsonWhiteNULL Marjorie GreenNULL Cheryl CarsonAlgodata Michael O’LearyNULL

How a Join is Processed First the system obtains the Cartesian Product of all tables in join Cartesian Product - the matrix of all possible combinations that could satisfy the joinFirst the system obtains the Cartesian Product of all tables in join Cartesian Product - the matrix of all possible combinations that could satisfy the join The select list is used to restrict the columns returned The select list is used to restrict the columns returned The WHERE clause is then used to restrict the rows return that satisfy the queryThe WHERE clause is then used to restrict the rows return that satisfy the query

Union clause Way of combining data from multiple queriesWay of combining data from multiple queries Uses the column names from the first SELECT statementUses the column names from the first SELECT statement select fname FirstName, lname LastName, city City from authors where city=‘Oakland’ UNION select fname, lname, city from authors where city=‘New York’ select fname FirstName, lname LastName, city City from authors where city=‘Oakland’ UNION select fname, lname, city from authors where city=‘New York’

Union cont. Removes all duplicates by default. Using the ALL keyword prevents this. UNION ALLRemoves all duplicates by default. Using the ALL keyword prevents this. UNION ALL Can use an ORDER BY only in the last SELECT statement which applies to all the outputCan use an ORDER BY only in the last SELECT statement which applies to all the output Can eliminate the need to write numerous SELECT statements.Can eliminate the need to write numerous SELECT statements.