Joining Tables Steve Perry

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
Beginning SQL Tutorial Author Jay Mussan-Levy. What is SQL?  Structured Query Language  Communicate with databases  Used to created and edit databases.
SQL Exercises1 Revising RDB and SQL CTEC2902 Advanced Programming.
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.
1 Section 5 - Grouping Data u The GROUP BY clause allows the grouping of data u Aggregate functions are most often used with the GROUP BY clause u GROUP.
CHAPTER 7 Database: SQL, MySQL. Topics  Introduction  Relational Database Model  Relational Database Overview: Books.mdb Database  SQL (Structured.
Dr. Chen, Oracle Database System (Oracle) 1 Database Development DDL DML DCL JL_D.B. ORACLE (SQL Components) (Retrieve Data and Produce Information from.
Database. Basic Definitions Database: A collection of related data. Database Management System (DBMS): A software package/ system to facilitate the creation.
Learningcomputer.com SQL Server 2008 – Entity Relationships in a Database.
SQL/lesson 2/Slide 1 of 45 Retrieving Result Sets Objectives In this lesson, you will learn to: * Use wildcards * Use the IS NULL and IS NOT NULL keywords.
Using Relational Databases and SQL John Hurley Department of Computer Science California State University, Los Angeles Lecture 3: Joins Part I.
Chapter 9 Joining Data from Multiple Tables
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.
Database: SQL and MySQL
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:
Grouping Data. GROUP BY clause Groups results by column name used with aggregate functions must come first when used with ORDER BY.
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining Multiple Tables.
SQL by Example By convention SQL keywords are written in uppercase. SELECT * FROM Books –This query returns all rows in the Books table. –SQL statements.
1 The Information School of the University of Washington Dec 1fit advdatabases © 2006 University of Washington Advanced Database Concepts INFO/CSE.
Subqueries Steve Perry 1.
Grouping Data Steve Perry
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
LINQ to DATABASE-2.  Creating the BooksDataContext  The code combines data from the three tables in the Books database and displays the relationships.
Fall Lab 131 CS105 Lab 13 – Logical Operator Precedence and Joining Tables Announcements: MP 3 released Friday, 11/20 Honors project due: Tuesday,
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
1 Section 10 - Embedded SQL u Many computer languages allow you to embed SQL statements within the code (e.g. COBOL, PowerBuilder, C++, PL/SQL, etc.) u.
1 Section 8 - Manipulating Data u The INSERT statement adds rows of data to the database u The UPDATE statement changes columns of existing data u The.
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.
Dr. Chen, Oracle Database System (Oracle) 1 Basic Nested Queries and Views Jason C. H. Chen, Ph.D. Professor of MIS School of Business Gonzaga University.
1 Section 9 - Views, etc. u Part 1: Views u Part 2:Security Issues u Part 3:Transaction Management u Part 4:Set Operations u Part 5:Triggers and Stored.
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
ORDER BY Clause The result of a query can be sorted in ascending or descending order using the optional ORDER BY clause. The simplest form of.
Databases.
Connect to SQL Server and run select statements
Writing Basic SQL SELECT Statements
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
3d. Structured Query Language – JOIN Operations
Oracle Join Syntax.
 2012 Pearson Education, Inc. All rights reserved.
CS311 Database Management system
LINQ to DATABASE-2.
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Constraints AND Examples
SQL.
SQL – Column constraints
Writing Basic SQL SELECT Statements
INFO/CSE 100, Spring 2005 Fluency in Information Technology
David M. Kroenke and David J
Structured Query Language (SQL) William Klingelsmith
Insert, Update, Delete Manipulating Data.
Restricting and Sorting Data
Chapter 22 - SQL, MySQL, DBI and ADO
LINQ to DATABASE-2.
Oracle Join Syntax.
Introduction To Structured Query Language (SQL)
Writing Basic SQL SELECT Statements
Lecture 3 Finishing SQL
Introduction To Structured Query Language (SQL)
Advanced Database Concepts: Reports & Views
Section 4 - Sorting/Functions
Oracle Join Syntax.
Indexes and more Table Creation
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Constraints AND Examples
Restricting and Sorting Data
Presentation transcript:

Joining Tables Steve Perry

Joining Tables Explicit Join in FROM Joins are implicitly expressed in the WHERE clause 2

Two Tables at a Time Can Join Only Two Tables at a Time –Use 'JOIN' keyword in the FROM clause –Use 'Connecting' Columns in the WHERE clause datatypes must match or convert correctly More than one Join operation can be specified in a Single SELECT statement 3

Join Syntax – FROM Clause SELECT select_list FROM table_1 JOIN table_2 {USING (col_1) | ON (col_1 operator col_2) } 4

Examples SELECT title, name FROM book JOIN publisher USING (pub_id); SELECT title, name FROM book JOIN publisher ON (book.pub_id = publisher.pub_id); Qualify the column names with the table names with there is ambiguity 5

Aliases Remember you can use short names as aliases for the table names SELECT title, name FROM book t JOIN publisher p ON (t.pub_id = p.pub_id); 6

Example Find the author(s) of the Secrets of Silicon Valley SELECT lastname, firstname, title FROM author JOIN bookauthor USING (ssn) JOIN book USING (isbn) WHERE title = ‘Secrets of Silicon Valley’; 7

Unplanned Joins Joins may be planned –Primary Keys –Foreign Keys Joins do not have to be planned –e.g. you may join the "city" column on the authors and editors table 8

Database Diagram 9

Join Operators >greater than >=greater than or equal to <less than <=less than or equal to not equal to 10

Column Compatibility Join columns need not have the same name They should be of the same datatype (or convert to the same datatype) The join should be meaningful NULLs found in connecting columns will never join. 11

Example Show a list of all authors that live in the same city where the ‘Algodata Infosystems’ publisher is based SELECT lastname, firstname, city FROM author JOIN publisher USING (city) WHERE name = ‘Algodata Infosystems’; 12

Data Replication Rows are returned for every successful join Returned rows may replicate data for display. (This does not change the data.) 13

Exercise Show a list of the authors whose last name is ‘Ringer’ and the city they live in, but only if they have written any books. Put in last name order. 14

Answer SELECT lastname, firstname, city FROM author JOIN bookauthor USING (ssn) WHERE lastname = ‘Ringer’; If no join is made from an author (ssn) to the bookauthor table then the author has not written any books. 15

Joining More than Two Tables We can only join two tables at a time… … but we can do more than one ‘two-table’ join in a Select statement. 16

Exercise Show a list of books and the authors who’ve written them. –Remember: Many-to-Many relationships are modeled with an association/bridging table –Need to join three tables to get information from both associated tables. 17

Database Diagram 18

Answer SELECT lastname, firstname, title FROM authors JOIN bookauthor USING (ssn) JOIN book USING (isbn) ORDER BY 1, 2, 3 19

Another Exercise Show a list of books (by book title) and the number of authors that have written each book, if more than one author wrote the book. 20

Answer SELECT title, count(ssn) FROM book JOIN bookauthor USING (isbn) GROUP BY title HAVING count(ssn) > 1 ORDER BY title 21

Last Slide 22