INF 280 Database Systems 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

© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
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.
Relational Database Operators
Chapter 4 Hotel (hotelno, hotelname, city)
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
1 Assignment 2 Relational Algebra Which tables? What operations? Common attributes? What result (attributes)? Syntax (Standard Notations and Symbols) –Product:
CS 3630 Database Design and Implementation. SQL Query Clause Select and From Select * From booking; select hotel_no, guest_no, room_no from booking; select.
Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database.
Chapter 6 SQL Homework.
Introduction to Oracle9i: SQL1 Basic SQL SELECT Statements.
Chapter 5 SQL Homework.
Using SQL to create tables Ways of using Databases.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database.
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.
Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database.
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.
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.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
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.
1 The Relational Database Model. 2 Learning Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical.
9/7/2012ISC329 Isabelle Bichindaritz1 The Relational Database Model.
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.
劉 志 俊 (Chih-Chin Liu) 中華大學 資訊工程系 October 2001 Chap 9 SQL (III): Advanced Queries.
SQL queries ordering and grouping and joins
Relational Algebra – Part 2
1 Chapter 10 Joins and Subqueries. 2 Joins & Subqueries Joins – Methods to combine data from multiple tables – Optimizer information can be limited based.
From Relational Algebra to SQL CS 157B Enrique Tang.
SQL - DML. Data Manipulation Language(DML) Are used for managing data: –SELECT retrieve data from the a database –INSERT insert data into a table –UPDATE.
Chapter 5 SQL: Data Manipulation Thomas Connolly, Carolyn Begg, Database System, A Practical Approach to Design Implementation and Management, 4 th Edition,
SE305 Database System Technology 23/10/2014 Quiz-2.
SQL introduction 2013.
SQL LANGUAGE and Relational Data Model TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Relational Algebra MBAD 613 R. Nakatsu. Relational Data Manipulation Language Query-by-Example; Query-by-Form Transform-Oriented Languages Relational.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
CS 3630 Database Design and Implementation. Joins -- For each booking, display the booking -- details with the room type and price Select B.*, rtype,
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.
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
Populating and Querying tables Insert, Update, Delete and View (DML)
IST 210 More SQL Todd Bacastow IST 210: Organization of Data.
Query Processing – Implementing Set Operations and Joins Chap. 19.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 3 The Relational Database Model.
CS 3630 Database Design and Implementation. Base Table and View Base Table Stored on disk View Virtual table Records are not stored on disk Query is stored.
CS 3630 Database Design and Implementation. Where Clause and Aggregate Functions -- List all rooms whose price is greater than the -- average room price.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
April 20022CS3X1 Database Design Relational algebra John Wordsworth Department of Computer Science The University of Reading Room.
CS 3630 Database Design and Implementation. Joins -- For each booking, display the booking -- details with the room type and price Select B.*, rtype,
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
SQL: Single Table Queries GROUP BY HAVING D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 1.
Advanced SQL Advanced Database Dr. AlaaEddin Almabhouh.
IFS180 Intro. to Data Management Chapter 10 - Unions.
CS 3630 Database Design and Implementation
More SQL: Complex Queries,
Assignment 2 Relational Algebra Which tables? What operations?
CS 3630 Database Design and Implementation
References: Text Chapters 8 and 9
Assignment 2.
CS 3630 Database Design and Implementation
JOINS (Joinining multiple tables)
SQL set operators and modifiers.
JOINS (Joinining multiple tables)
Presentation transcript:

INF 280 Database Systems SQL:Join D. Christozov INF 280 Database Systems SQL:Join

The two NULL values do not match! SQL: join Used for the following purposes: to combine data from two or more tables to select data from one table based on a search condition involving columns from from an other table; to reintegrate data, separated during normalization to combine information about separate entities to explore relationships between tables. Matching the related values in columns of the two tables is based on the foreign key mechanism or the rule of having columns from the two tables with the same names. The two NULL values do not match! D. Christozov INF 280 Database Systems SQL:Join

INF 280 Database Systems SQL:Join Syntax: FROM A [type] [JOIN] B [condition] Equijoins: the join condition is based on comparison for equality and the join columns are included in the final result Natural join – only one of the corresponding join columns is included in the result. FROM A [type] B ON A.b = B.d (the corresponding columns have different names) FROM A [type] B USING b (the corresponding columns have equal names  b) D. Christozov INF 280 Database Systems SQL:Join

Using foreign key constrains SQL: join Using foreign key constrains FROM A [type] B (the corresponding columns are defined as foreign key reference) Theta joins any join in which the condition is given in the form: qualifier_1.column_1 relational_operator qualifier_2.column_2 where relational_operator can be any of the following: =, <>, <, >, <=, >= Equijoin is a special case of theta join D. Christozov INF 280 Database Systems SQL:Join

SQL: join – categories of natural/equijoins Built in specified in FROM clause CROSS a Cartesian product: all combinations of rows INNER the default join type: only matched rows are retained LEFT(OUTER) all rows from table A, plus the matched values from table B RIGHT(OUTER) all rows from table B, plus the matched values from table A FULL(OUTER) all rows from both tables, merged where matches were found,or filled with NULLs in the columns from other table UNION opposite of the INNER: retains only no matched rows Defined specified as filter-condition in WHERE clause D. Christozov INF 280 Database Systems SQL:Join

INF 280 Database Systems SQL:Join SQL: cross join A A cross join B a b 1334 CCC 1399 GGG 1208 MMM A.a A.b B.b B.c 1334 CCC 8554 1399 GGG 1208 MMM 5223 9111 WWW 7894 B b c CCC 8554 GGG 5223 9111 WWW 7894 D. Christozov INF 280 Database Systems SQL:Join

INF 280 Database Systems SQL:Join SQL: inner join A a b 1334 CCC 1399 GGG 1208 MMM A inner join B (A.b = B.b) A.a A.b B.c 1334 CCC 8554 1399 GGG 5223 9111 B b c CCC 8554 GGG 5223 9111 WWW 7894 D. Christozov INF 280 Database Systems SQL:Join

INF 280 Database Systems SQL:Join SQL: left outer join A a b 1334 CCC 1399 GGG 1208 MMM A left outer join B (A.b = B.b) A.a B.b B.c 1334 CCC 8554 1399 GGG 5223 9111 1208 MMM NULL B b c CCC 8554 GGG 5223 9111 WWW 7894 D. Christozov INF 280 Database Systems SQL:Join

INF 280 Database Systems SQL:Join SQL: right outer join A a b 1334 CCC 1399 GGG 1208 MMM A right outer join B (A.b = B.b) A.a A.b B.c 1334 CCC 8554 1399 GGG 5223 9111 NULL WWW 7894 B b c CCC 8554 GGG 5223 9111 WWW 7894 D. Christozov INF 280 Database Systems SQL:Join

INF 280 Database Systems SQL:Join SQL: full outer join A a b 1334 CCC 1399 GGG 1208 MMM A full outer join B (A.b = B.b) A.a A.b B.c 1334 CCC 8554 1399 GGG 5223 9111 NULL WWW 7894 1208 MMM B b c CCC 8554 GGG 5223 9111 WWW 7894 D. Christozov INF 280 Database Systems SQL:Join

INF 280 Database Systems SQL:Join SQL: union join A a b 1334 CCC 1399 GGG 1208 MMM A union join B (A.b = B.b) A.a A.b B.c 1208 MMM NULL WWW 7894 B b c CCC 8554 GGG 5223 9111 WWW 7894 D. Christozov INF 280 Database Systems SQL:Join

INF 280 Database Systems SQL:Join Exercises D. Christozov INF 280 Database Systems SQL:Join

INF 280 Database Systems SQL:Join Booking Guests HotelNo RoomNo GuestNo DateFrom DateTo H01 R01 G01 20050305 20050310 R02 G02 20050328 G03 20050311 20050426 H02 G04 G05 20050601 20050830 R03 G06 20050228 20050502 H03 G07 20050410 20050430 H04 G08 20050412 20050508 GuestNo Name Address G01 Peter USA G02 John Scapto2 G03 Alice UK G04 Barry BG G05 Vern AUBG G06 Simeon Skapto1 G07 Mark Kchuka G08 Julia Sofia Hotel HotelNo Name Address H01 Lessvenor San Francisco H02 Grosvenor New York H03 Alen Mak Blagoevgrad H04 Sheraton Sofia Rooms RoomNo HotelNo Type Price R01 H01 Single 67.00 R02 H02 Double 76.00 R03 H03 Apartment 102.00 H04 72.00 75.00 92.00 88.00 134.00 SELECT Room_No, Type, Price FROM Room INNER JOIN Booking USING (HotelNo, Room_No) UNION JOIN Guest USING GuestNo; D. Christozov INF 280 Database Systems SQL:Join

INF 280 Database Systems SQL:Join Booking Guests HotelNo RoomNo GuestNo DateFrom DateTo H01 R01 G01 20050305 20050310 R02 G02 20050328 G03 20050311 20050426 H02 G04 G05 20050601 20050830 R03 G06 20050228 20050502 H03 G07 20050410 20050430 H04 G08 20050412 20050508 GuestNo Name Address G01 Peter USA G02 John Scapto2 G03 Alice UK G04 Barry BG G05 Vern AUBG G06 Simeon Skapto1 G07 Mark Kchuka G08 Julia Sofia Hotel HotelNo Name Address H01 Lessvenor San Francisco H02 Grosvenor New York H03 Alen Mak Blagoevgrad H04 Sheraton Sofia Rooms RoomNo HotelNo Type Price R01 H01 Single 67.00 R02 H02 Double 76.00 R03 H03 Apartment 102.00 H04 72.00 75.00 92.00 88.00 134.00 SELECT RoomNo, Type, GuestNo FROM Room Left Outer Join Booking Using (RoomNo, HotelNo) WHERE Date_from <= 03/24/2003 AND Date_to >= 03/24/2003; D. Christozov INF 280 Database Systems SQL:Join

INF 280 Database Systems SQL:Join Booking Guests HotelNo RoomNo GuestNo DateFrom DateTo H01 R01 G01 20050305 20050310 R02 G02 20050328 G03 20050311 20050426 H02 G04 G05 20050601 20050830 R03 G06 20050228 20050502 H03 G07 20050410 20050430 H04 G08 20050412 20050508 GuestNo Name Address G01 Peter USA G02 John Scapto2 G03 Alice UK G04 Barry BG G05 Vern AUBG G06 Simeon Skapto1 G07 Mark Kchuka G08 Julia Sofia Hotel Rooms HotelNo Name Address H01 Lessvenor San Francisco H02 Grosvenor New York H03 Alen Mak Blagoevgrad H04 Sheraton Sofia RoomNo HotelNo Type Price R01 H01 Single 67.00 R02 H02 Double 76.00 R03 H03 Apartment 102.00 H04 72.00 75.00 92.00 88.00 134.00 SELECT GuestNo, Guest.Name, Hotel.Name FROM Guest, Booking, Hotel WHERE DateFrom <= ’03/25/2003’ and DateTo >= ‘03/25/2003’; D. Christozov INF 280 Database Systems SQL:Join

INF 280 Database Systems SQL:Join Booking Guests HotelNo RoomNo GuestNo DateFrom DateTo H01 R01 G01 20050305 20050310 R02 G02 20050328 G03 20050311 20050426 H02 G04 G05 20050601 20050830 R03 G06 20050228 20050502 H03 G07 20050410 20050430 H04 G08 20050412 20050508 GuestNo Name Address G01 Peter USA G02 John Scapto2 G03 Alice UK G04 Barry BG G05 Vern AUBG G06 Simeon Skapto1 G07 Mark Kchuka G08 Julia Sofia Hotel Rooms HotelNo Name Address H01 Lessvenor San Francisco H02 Grosvenor New York H03 Alen Mak Blagoevgrad H04 Sheraton Sofia RoomNo HotelNo Type Price R01 H01 Single 67.00 R02 H02 Double 76.00 R03 H03 Apartment 102.00 H04 72.00 75.00 92.00 88.00 134.00 SELECT HotelNo, Name FROM Hotel Left Outer Join Booking Using HotelNo; D. Christozov INF 280 Database Systems SQL:Join

INF 280 Database Systems SQL:Join Booking Guests HotelNo RoomNo GuestNo DateFrom DateTo H01 R01 G01 20050305 20050310 R02 G02 20050328 G03 20050311 20050426 H02 G04 G05 20050601 20050830 R03 G06 20050228 20050502 H03 G07 20050410 20050430 H04 G08 20050412 20050508 GuestNo Name Address G01 Peter USA G02 John Scapto2 G03 Alice UK G04 Barry BG G05 Vern AUBG G06 Simeon Skapto1 G07 Mark Kchuka G08 Julia Sofia Hotel Rooms HotelNo Name Address H01 Lessvenor San Francisco H02 Grosvenor New York H03 Alen Mak Blagoevgrad H04 Sheraton Sofia RoomNo HotelNo Type Price R01 H01 Single 67.00 R02 H02 Double 76.00 R03 H03 Apartment 102.00 H04 72.00 75.00 92.00 88.00 134.00 What is the mistake? SELECT Type, SUM(Price) FROM Room, Booking, Guests WHERE DateFrom <= ’03/25/2003’ and DateTo >= ‘03/25/2003’; D. Christozov INF 280 Database Systems SQL:Join

INF 280 Database Systems SQL:Join Write query Consider ‘Hotel’s Chain Data Base Booking: HotelNo; RoomNo; GuestNo; DateFrom;DateTo Guests: GuestNo; Name; Address Hotel: HotelNo; Name; Address Rooms: RoomNo; HotelNo; Type; Price Write a query to list all guests (name) currently (March 24, 2003) stay in Grosvenor Hotel. (Tip: Grosvenor is the hotel name) Write a query to calculate the today’s (March 24, 2003) income (sum of prices of occupied rooms) of all bookings for Grosvenor Hotel. Write a query to list today’s (March 24, 2003) unoccupied rooms (Room_No, Type, Price) in Grosvenor Hotel. Write a query to count American (Tip: address like ‘%USA%’) guests booked single rooms for May for Grosvenor Hotel. 1. SELECT Guests.* FROM ((Guests Inner Join Bookings Using GuestNo) Inner Join Hotel Using HotelNo) WHERE (DateFrom <= March 24 and DateTo >= March 24) and Hotel.Name = ‘Grosvenor’; Note: Hotel.Name – must qualified 2. SELECT ‘Income for March 23’, SUM(Price) AS ‘Money’ FROM ((Booking Inner Join Rooms ON (Booking.RoomNo=Rooms.RoomNo AND Booking.HotelNo=Rooms.HotelNo) ) Inner Join Hotel ON (Booking.HotelNo = Hotel.HotelNo)) Note: In the second join cannot use ‘USING’ 3. SELECT Rooms.* FROM ((Booking Right Outer Join Rooms ON (Booking.RoomNo=Rooms.RoomNo AND Booking.HotelNo=Rooms.HotelNo) ) Note: all rooms, including those which were never booked Inner Join Hotel ON (Booking.HotelNo = Hotel.HotelNo)) WHERE ((DateFrom >= March 24 OR DateTo <= March 24) OR DateFrom IS Null) and Hotel.Name = ‘Grosvenor’; 4. SELECT ‘Number of USA guests on March 24’, Count(*) FROM FROM ((Guests Inner Join Bookings Using GuestNo) Inner Join Hotel Using HotelNo) WHERE (((DateFrom <= May 31 and DateTo >= May 1) and Hotel.Name = ‘Grosvenor’) and Guest.Address Like ‘USA’; Note: comparing intervals – repeating from ‘group by’ D. Christozov INF 280 Database Systems SQL:Join