Geo-Databases: lecture 4 Complex Queries in SQL

Slides:



Advertisements
Similar presentations
© Abdou Illia MIS Spring 2014
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.
Relational Database Operators
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Relational Algebra Rohit Khokher. Relational Algebra Set Oriented Operations UnionIntersectionDifference Cartesian Product Relation Oriented Operations.
Relational Algebra Ch. 7.4 – 7.6 John Ortiz. Lecture 4Relational Algebra2 Relational Query Languages  Query languages: allow manipulation and retrieval.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
Relational Algebra Relational Calculus. Relational Algebra Operators Relational algebra defines the theoretical way of manipulating table contents using.
--The SQL Query Language DML--1 The SQL Query Language DML The SQL Query Language DML (SELECT)
Nov 24, 2003Murali Mani SQL B term 2004: lecture 12.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Chapter 11.1 and 11.2 Data Manipulation: Relational Algebra and SQL Brian Cobarrubia Introduction to Database Management Systems October 4, 2007.
Lecture 2 The Relational Model. Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical relations.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Relational Query Languages. Languages of DBMS  Data Definition Language DDL  define the schema and storage stored in a Data Dictionary  Data Manipulation.
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)
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
1 The Relational Database Model. 2 Learning Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
9/7/2012ISC329 Isabelle Bichindaritz1 The Relational Database Model.
SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby debby lilly.
Chapter 4 Multiple-Table Queries
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining Multiple Tables.
I NTRODUCTION TO SQL II. T ABLE JOINS A simple SQL select statement is one that selects one or more columns from any single table There are two types.
From Relational Algebra to SQL CS 157B Enrique Tang.
INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012.
Relational Algebra MBAD 613 R. Nakatsu. Relational Data Manipulation Language Query-by-Example; Query-by-Form Transform-Oriented Languages Relational.
Advanced Relational Algebra & SQL (Part1 )
SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
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.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 9 A First Course in Database Systems.
 CONACT UC:  Magnific training   
1 Relational Algebra and SQL. 2 Relational Query Languages Languages for describing queries on a relational database Relational AlgebraRelational Algebra.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
CSE202 Database Management Systems
Chapter (6) The Relational Algebra and Relational Calculus Objectives
More SQL: Complex Queries,
COMP3017 Advanced Databases
Database Systems: Design, Implementation, and Management Tenth Edition
Geo-Databases: lecture 7 Database design
SQL Structured Query Language 11/9/2018 Introduction to Databases.
David M. Kroenke and David J
More SQL Nested and Union queries, and more
Database Applications (15-415) SQL-Part II Lecture 9, February 04, 2018 Mohammad Hammoud.
IST 210: Organization of Data
LECTURE 3: Relational Algebra
CPSC-310 Database Systems
Chapter Name SQL: Data Manipulation
Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
CSC 453 Database Systems Lecture
CS 3630 Database Design and Implementation
More SQL: Complex Queries, Triggers, Views, and Schema Modification
CSCE 315 – Programming Studio Spring 2010 Project 1, Lecture 4
Geo-Databases: lecture 2 The Relational Data Model
SQL: Structured Query Language
Chapter 2: Intro to Relational Model
Contents Preface I Introduction Lesson Objectives I-2
Geo-Databases: lecture 5 Data Manipulation in SQL
Database Systems: Design, Implementation, and Management Tenth Edition
Geo-Databases: lecture 6 Data Integrity
Geo-Databases: lecture 3 Simple Queries in SQL
Unit Relational Algebra 1
Presentation transcript:

Geo-Databases: lecture 4 Complex Queries in SQL Prof. Dr. Thomas H. Kolbe Institute for Geodesy and Geoinformation Science Technische Universität Berlin Credits: This material is mostly an english translation of the course module no. 8 (‘Geo-Datenbanksysteme‘) of the open e-content platform www.geoinformation.net.

Complex queries in SQL 04/05/2019

Motivation In many cases interrelated information is spread across multiple tables, e.g. in order to avoid redundancy. Problem: How can we associate the professors with their lectures? 04/05/2019

Combination of tables The assignment of professors with their lectures follows a general principle: 1. Create all pairs of tuples of Professoren and Vorlesungen! 2. Filter the valid combinations from this tuple set. 04/05/2019

Cross Join All pairs of tuples are generated using the following query. The result is the so-called cross join (also called cartesian product) of the two tables. Schema of the resulting table: Concatenation of attributes SELECT * FROM Professoren CROSS JOIN Vorlesungen 04/05/2019

General Join Operator The general join allows to constrain the considered tuples using an arbitrary selection condition. The selection condition specifies which tuple pairs are valid. Wanted: Which professors teach which subjects? SELECT * FROM Professoren JOIN Vorlesungen ON Professoren.PersNr = Vorlesungen.PersNr Result: 04/05/2019

Natural Join Tests all columns having the same name from both tables on identity Schema of the resulting table: Like cross join, but attributes with the same name are only given once Example: Show the lecturer for each course! SELECT * FROM Professoren NATURAL JOIN Vorlesungen 04/05/2019

Problem of previous Join Operators In some situations we may be interested in joining two tables, where tuples, that have no corresponding partner should also be included in the result set. Example: Create a table with all professors and their lectures! Problem: The last tuple has no corresponding partner. Therefore it would not appear in the result set using the previously introduced join operators. Previously presented join operators do not fit the problem! 04/05/2019

Outer Join Left outer join: tuples of the left table, that have no corresponding partner are retained. For these tuples the attributes of the right table are set to NULL. Similarly the right outer join retaines all the tuples from the right table. The full outer join combines the functionality of both left and right outer join. Example: SELECT * FROM Professoren LEFT OUTER JOIN Vorlesungen ON Professoren.PersNr=Vorlesungen.PersNr 04/05/2019

Overview of Join Operators type from SQL 92 alternative syntax Cartesian product SELECT * FROM Professoren CROSS JOIN Vorlesungen SELECT * FROM Professoren, Vorlesungen Natural join SELECT * FROM Professoren NATURAL JOIN Vorlesungen SELECT attributelist (see below) FROM Professoren, Vorlesungen WHERE Professoren.PersNr= Vorlesungen.PersNr General SELECT * FROM Professoren JOIN Vorlesungen ON Professoren.PersNr= Vorlesungen.PersNr SELECT * FROM Professoren, Vorlesungen WHERE Professoren.PersNr= Vorlesungen.PersNr Right outer join SELECT * FROM Professoren RIGHT OUTER JOIN Vorlesungen No equivalence attributelist: all attributes from Professoren, non-redundant attributes from Vorlesung 04/05/2019

Nested Queries A nested query (or subquery) “SELECT…FROM…WHERE…“ is allowed: in the from clause (as a substitute for table names) as operand of the operators UNION, INTERSECT, EXCEPT, IN, EXISTS In case the result of the subquery is a scalar (that means a table comprising one row and usually one column) also: in a comparison operation as an attribute within the select clause Example: Formulation of a professors-lectures join with subquery: set operation subquery (=nested SQL-query) 04/05/2019

Existential Quantifier Existential quantifiers offer another way to perform complex queries operates on a table T returns the logical value TRUE if T is not empty Example: All professors that do not have lectures. negation existential quantifiers Remark: A FOR ALL -operator is not provided in SQL! 04/05/2019

Set Operations (1) Alternatively, the previous query can be formulated using set operations. The operators IN and NOT IN are used to test for membership within a set. Wanted: All professors that don‘t have lectures. all professors professors with lectures 04/05/2019

Set Operations (2) SQL provides the classical set operations: Remarks: R UNION S for set union R INTERSECT S for the intersection and R EXCEPT S for the set difference of two tables R and S Remarks: Schema compatibility of R and S is required The set operations eliminate duplicates! If duplicates should be retained: UNION ALL,… union operator all staff members all employees all professors 04/05/2019

References Jim Melton, Alan R. Simon, SQL 1999: Understanding Relational Language Components, Morgan Kaufmann Publishers, 2001 04/05/2019