SQL - III Reading: C&B, Chap 6, 7, 8 & 9. Dept. of Computing Science, University of Aberdeen 2 In this lecture you will learn the concept of joining tables.

Slides:



Advertisements
Similar presentations
Query Processing Chapter 21 in Textbook.
Advertisements

0 - 0.
Introduction to Relational Database Systems 1 Lecture 4.
Logical Database Design Reading: C&B, Chap 17. Dept. of Computer Science, University of Aberdeen2 In this lecture you will learn What is logical database.
Relational Model Reading: C&B, Chap 2, 3 & 4. Dept. of Computing Science, University of Aberdeen 2 In this lecture you will learn The concept of Model.
Access Control & Views Reading: C&B, Chap 7. Dept of Computing Science, University of Aberdeen2 In this lecture you will learn the principles of object.
Database Design: ER Modelling
Query Processing Reading: CB, Chaps 5 & 23. Dept of Computing Science, University of Aberdeen2 In this lecture you will learn the basic concepts of Query.
Data Definition and Integrity Constraints
SQL-II Reading: C&B, Chap 6, 7, 8 & 9. Dept. of Computing Science, University of Aberdeen2 In this lecture you will learn how to sort and group query.
© Abdou Illia MIS Spring 2014
Chapter 4 Joining Multiple Tables
Chapter 12 Joining Tables Part C. SQL Copyright 2005 Radian Publishing Co.
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.
Displaying Data from Multiple Tables
Relational Algebra and Relational Calculus
Computer Concepts BASICS 4th Edition
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
CSC271 Database Systems Lecture # 11.
RELATIONAL ALGEBRA Lecture Relational Algebra Operations to manipulate relations. Used to specify retrieval requests (queries). Query results in.
CSC271 Database Systems Lecture # 13. Summary: Previous Lecture  Grouping through GROUP BY clause  Restricted groupings  Subqueries  Multi-Table queries.
1 Minggu 4, Pertemuan 8 SQL: Data Manipulation (Cont.) Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
1 Minggu 3, Pertemuan 5 Relational Algebra (Cont.) Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
Database Systems: A Practical Approach to Design, Implementation and Management International Computer Science S. Carolyn Begg, Thomas Connolly Lecture.
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.
1-1 Thomas Connolly and Carolyn Begg’s Database Systems: A Practical Approach to Design, Implementation, and Management Chapter 4 Part One: Relational.
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.
Relational Database Management System A type of database in which records are stored in relational form is called relational database management system.
RELATIONAL ALGEBRA Objectives
Relational Model & Relational Algebra. 2 Relational Model u Terminology of relational model. u How tables are used to represent data. u Connection between.
Lecture 2 of Advanced Databases Advanced SQL Instructor: Mr.Ahmed Al Astal.
Chapter 9 Joining Data from Multiple Tables
SQL: Overview SQL Overview © Pearson Education Limited 1995, 2005.
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.
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)
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
Chapter 6 SQL: Data Manipulation (Advanced Commands) Pearson Education © 2009.
SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.
JOI/1 Data Manipulation - Joins Objectives –To learn how to join several tables together to produce output Contents –Extending a Select to retrieve data.
1 General Form of the SELECT Statement SELECT [DISTINCT | ALL {* | col_expr [AS new_name] [,…]} FROMtable_name [,…] [,…] [WHERE condition] [GROUP BY col_list]
Bayu Adhi Tama, ST., MTI. Introduction Relational algebra and relational calculus are formal languages associated with the relational.
Advanced Database Systems
CS 3630 Database Design and Implementation. 2 DreamHome Branch (branchNo, street, city, state, zipcode, phone1, phone2, phone3) Staff (staffNo, firstName,
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.
1 Pertemuan > > Matakuliah: >/ > Tahun: > Versi: >
Chapter 5 Relational Algebra and Relational Calculus Pearson Education © 2009.
Chapter 5 Relational Algebra Pearson Education © 2014.
1 Geog 357 – Introduction to GIS The Relational Language.
CSC271 Database Systems Lecture # 8. Summary: Previous Lecture  Relation algebra and operations  Selection (Restriction), projection  Union, set difference,
SQL : Data Manipulation Pertemuan 09 s/d 12 Matakuliah: M0564 /Pengantar Sistem Basis Data Tahun : 2008.
1 Pertemuan > > Matakuliah: >/ > Tahun: > Versi: >
CSC271 Database Systems Lecture # 7. Summary: Previous Lecture  Relational keys  Integrity constraints  Views.
SQL: Additional Notes. 2 Example 5.3 Use of DISTINCT List the property numbers of all properties that have been viewed. SELECT propertyNo FROM Viewing;
LECTURE THREE RELATIONAL ALGEBRA 11. Objectives  Meaning of the term relational completeness.  How to form queries in relational algebra. 22Relational.
Lecture 11 SQL. Agenda SQL Select statement WHERE clause BindingSource filtering.
Chapter 4 Relational Algebra Chapter 4 in Textbook.
Teacher Workshop Database Design Pearson Education © 2014.
Chapter 11 SQL: Data Manipulation
Relational Algebra Lecture 2.
CS 3630 Database Design and Implementation
Relational Algebra and Relational Calculus
Querying Multiple Tables
The Relational Database Model
Chapter Name SQL: Data Manipulation
JOINS (Joinining multiple tables)
The Relational Algebra
Chapter Name SQL: Data Manipulation Transparencies JOINS
JOINS (Joinining multiple tables)
Presentation transcript:

SQL - III Reading: C&B, Chap 6, 7, 8 & 9

Dept. of Computing Science, University of Aberdeen 2 In this lecture you will learn the concept of joining tables why joins are central to relational database systems how to specify joins in SQL the different ways of joining tables using table aliases & full column names in queries

Dept. of Computing Science, University of Aberdeen 3 Querying Multiple Tables PropertyForRent PropertyNoStreetCityPostcodeTypeRoomsRentOwnerNoStaffNoBranchNo PA1416 HolheadAberdeenAB7 5SUHouse6650CO46SA9B007 PG165 Novar DrGlasgowG12 9AXFlat4450CO93SG14B003 PG2118 Dale RdGlasgowG12House5600CO87SG37B003 PG362 Manor RdGlasgowG32 4QXFlat3375CO93SG37B003 PG46 Lawrence StGlasgowG11 9QXFlat3350CO40B003 PL946 Argyll StLondonNW2Flat4400CO87SL41B005 Viewing ClientNoPropertyNoViewDateComment CR56PA1424-May-01too small CR56PG3628-Apr-01 CR56PG426-May-01 CR62PA1414-May-01no dining room CR76PG420-Apr-01too remote Client ClientNoFnameLnameTelNoPrefTypeMaxRent CR56AlineStewart Flat350 CR62MaryTregear Flat600 CR74MikeRitchie House750 CR76JohnKay Flat425 How do we list all the properties that a given client has viewed? Could start with an example - e.g. client CR56...

Dept. of Computing Science, University of Aberdeen 4 Property Query - First Attempt First attempt: List the property numbers viewed by client number CR56: SELECT PropertyNo FROM Viewing WHERE ClientNo = 'CR56'; But we'd like to see the client name & property details So we'll need to access Client and PropertyForRent for names etc...

Dept. of Computing Science, University of Aberdeen 5 Property Query - Second Version SELECT Viewing.PropertyNo, Street, City, ViewDate FROM Viewing, PropertyForRent WHERE ClientNo = 'CR56' AND Viewing.PropertyNo = PropertyForRent.PropertyNo; We now have two table names in the FROM clause Note use of Table.ColumnName" to avoid ambiguity in column names

Dept. of Computing Science, University of Aberdeen 6 Property Query - Third Version SELECT Fname, Lname, Street, City, ViewDate FROM Viewing, PropertyForRent, Client WHERE Viewing.ClientNo = 'CR56' AND Viewing.PropertyNo = PropertyForRent.PropertyNo; AND Viewing.ClientNo = Client.ClientNo; The two AND" clauses are called join criteria

Dept. of Computing Science, University of Aberdeen 7 Property Query - Fourth Version Users shouldn't have to know about internal keys... SELECT Fname, Lname, Street, City, ViewDate FROM Viewing, PropertyForRent, Client WHERE Fname = 'Aline' AND Lname = 'Stewart' AND Viewing.PropertyNo = PropertyForRent.PropertyNo; AND Viewing.ClientNo = Client.ClientNo;

Dept. of Computing Science, University of Aberdeen 8 Using Table Aliases Table aliases can help reduce amount of typing The following is identical to the previous query: SELECT C.Fname, C.Lname, P.Street, P.City, V.ViewDate FROM Viewing V, PropertyForRent P, Client C WHERE C.Fname = 'Aline' AND C.Lname = 'Stewart' AND V.PropertyNo = P.PropertyNo AND V.ClientNo = C.ClientNo; Table aliases help reduce the risk of typing mistakes But shouldn't the DBMS know how to match keys for us?

Dept. of Computing Science, University of Aberdeen 9 Natural Joins Natural joins implement relationships in the Relational model The DBMS will know which columns are key columns The following is equivalent to the previous query: SELECT C.Fname, C.Lname, P.Street, P.City, V.ViewDate FROM Client C NATURAL JOIN (Viewing V NATURAL JOIN PropertyForRent P) WHERE C.Fname = 'Aline' AND C.Lname = 'Stewart'; Most DBMSs support 4-table joins, or more...

Dept. of Computing Science, University of Aberdeen 10 ANSI SQL Syntax for Joins ANSI SQL allows joins to be written in different ways: Natural joins: SELECT * FROM Left NATURAL JOIN Right; SELECT * FROM Left JOIN Right USING ColNam; If not joining on keys, give join condition explicitly: SELECT * FROM Left JOIN Right ON Left.ColNam = Right.ColNam; Which is best ? - Make up your own mind !!

Dept. of Computing Science, University of Aberdeen 11 Cross Joins (Cartesian Products) Cartesian Product: a join with no WHERE clause SELECT * FROM Left, Right; ANSI SQL allows: SELECT * FROM Left CROSS JOIN Right; Useful for: –query optimisation –data mining

Dept. of Computing Science, University of Aberdeen 12 Theta Joins Theta joins" have a more general WHERE predicate: SELECT L.*, R.*,... FROM Left L, Right R WHERE L.LeftCol Θ R.Rightcol; Θ may be one of =; ! =; <>; ; = If Θ is =, its an equi join If Θ is = and columns are key columns its a natural join If all output columns are from one table, its a semi join

Dept. of Computing Science, University of Aberdeen 13 Example of a Theta Join For all clients, list the properties that each client can afford to rent: SELECT C.ClientNo, P.PropertyNo FROM Client C, PropertyForRent P WHERE C.MaxRent >= P.rent; The DBMS could implement theta joins by: –First forming a cross join to give... –An intermediate (Cartesian product) table.. –Then applying WHERE clause to find matching rows –...but there are more efficient ways...

Dept. of Computing Science, University of Aberdeen 14 Self-Joins Sometimes it is useful to join a table to itself (must use aliases) ALTER TABLE Staff ADD COLUMN BossNo CHAR(5); SELECT S.Lname AS Person, B.Lname as Boss FROM Staff S, Staff B WHERE S.BossNo = B.StaffNo;

Dept. of Computing Science, University of Aberdeen 15 Outer Joins (Selecting unmatched rows) Example: List the branch offices and properties in the same city, along with any unmatched branches: SELECT B.BrNo, P.PropNo FROM Branch B LEFT JOIN PropForRent P ON B.City = P.City; NB. Not all DBMSs (e.g. MS Access) support outer joins

Dept. of Computing Science, University of Aberdeen 16 Right Outer Joins & Full Outer Joins In a similar manner, can use RIGHT JOIN and FULL JOIN (meaning full outer join): SELECT B.BrNo, P.PropNo FROM Branch B FULL JOIN PropertyForRent P ON B.City = P.City;

Dept. of Computing Science, University of Aberdeen 17 Why So Many Types of Join ? Theta join - a join using a simple WHERE predicate Equi join - a special case of theta join (= predicate) Natural join - special case of equi join (match keys) Semi join - theta join that outputs from just one table Self join - joining a table to itself Outer join - a join that may include unmatched rows cross join - Cartesian products (no predicates) Question: Why do we need to distinguish so many different types of join ? Answer: Queries with different joins are often optimised differently...