SQL: Set Operations & Nested Queries. Presented by: Dr. Samir Tartir

Slides:



Advertisements
Similar presentations
SQL DESIGN AND IMPLEMENTATION CONTENT SOURCES: ELAMSARI AND NAVATHE, FUNDAMENTALS OF DATABASE MANAGEMENT SYSTEMSELAMSARI AND NAVATHE, FUNDAMENTALS OF.
Advertisements

The Relational Calculus
Copyright © 2004 Ramez Elmasri and Shamkant Navathe Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 15-1 Query Processing and.
Chapter 15 Algorithms for Query Processing and Optimization Copyright © 2004 Pearson Education, Inc.
COMPANY schema EMPLOYEE
The Relational Algebra
Basic Queries. 2 Retrieval Queries in SQL SQL has one basic statement for retrieving information from a database; the SELECT statement This is not the.
SQL Query Slides Sharif University Of Technology Database Systems CE 384 Prepared By: Babak Bagheri Hariri
Your Logo Fundamentals of Database Systems Fourth Edition El Masri & Navathe Instructor: Mr. Ahmed Al Astal Chapter 8 (Cont.) SQL-99: Schema Definition,
More SQL: Complex Queries, Triggers, Views, and Schema Modification 1.
관계 연산자 & SQL. Selection SELECT * FROM r WHERE A=B AND D>5.
SQL The relational DB Standard CS-450 Dr. Ali Obaidi.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1 CREATE/DROP/ALTER TABLE Data types : char, varchar, decimal, date CREATE TABLE DEPARTMENT.
Al-Imam University Girls Education Center Collage of Computer Science 1 ST Semester, 1432/1433H Chapter 8 Part 4 SQL-99 Schema Definition, Constraints,
Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
1 CSE 480: Database Systems Lecture 11: SQL. 2 SQL Query SELECT FROM WHERE –In MySQL, FROM and WHERE clauses are optional –Example:
SQL: Schema Definition, Constraints, & Queries and Views.
Relational Algebra - Chapter (7th ed )
CS 380 Introduction to Database Systems Chapter 7: The Relational Algebra and Relational Calculus.
 Employee (fname, minit, lname, ssn, bdate, address, sex, salary, superssn, dno)  Department (dname, dnumber, mgrssn, mgrstartdate) 
Database Management Systems. NESTING OF QUERIES  Some queries require that existing values in the database be retrieved and then used in a comparison.
Announcements Written Homework 1 due Friday –If you have fourth edition make sure you do the right problems Program 3 out today, due next Friday Nov 10.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)
Structured Query Language
Structured Query Language (2) The main reference of this presentation is the textbook and PPT from : Elmasri & Navathe, Fundamental of Database Systems,
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
DatabaseIM ISU1 Fundamentals of Database Systems Chapter 6 The Relational Algebra.
Advanced Relational Algebra & SQL (Part1 )
Announcements Written Homework 1 due Nov 2 –See course web page –Exercises 5.12, 5.15, 6.17, 6.20, 6.22 (a,c,f only). Today –SQL (chapter 8) –Instructional.
Slide 6- 1 Additional Relational Operations Aggregate Functions and Grouping A type of request that cannot be expressed in the basic relational algebra.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Query Processing – Implementing Set Operations and Joins Chap. 19.
Chapter 6 The Relational Algebra Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
ISC321 Database Systems I Chapter 5: SQL: Advanced Queries, Assertions, Triggers, and Views Fall 2015 Dr. Abdullah Almutairi.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Lecture # 10 July 10,2012 More SQL: Complex Queries, Triggers,
CS580 Advanced Database Topics Chapter 8 SQL Irena Pevac.
Chapter (6) The Relational Algebra and Relational Calculus Objectives
More SQL: Complex Queries,
CS580 Advanced Database Topics
SQL: SchemaDefinition, Constraints, and Queries and Views
6/22/2018.
Normalization Functional Dependencies Presented by: Dr. Samir Tartir
CS580 Advanced Database Topics
The Relational Algebra and Relational Calculus
Retrieval Queries in SQL(DML)
SQL: Structured Query Language DML- Queries Lecturer: Dr Pavle Mogin
Subject Name: DATA BASE MANAGEMENT SYSTEM Subject Code: 10cs54
More SQL: Complex Queries, Triggers, Views, and Schema Modification
11/9/2018.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Company Requirements.
Session - 6 Sequence - 2 SQL: The Structured Query Language:
SQL (more).
Structured Query Language (3)
SQL-99: Schema Definition, Constraints, and Queries and Views
Session - 6 Sequence - 5 SQL Updating Database Contents
ISC321 Database Systems I Chapter 4: SQL: Data definition, Constraints, and Basic Queries and Updates Fall 2015 Dr. Abdullah Almutairi.
SQL Updating Database Contents Presented by: Dr. Samir Tartir
SQL Stored Procedures and Functions Presented by: Dr. Samir Tartir
Session - 6 Sequence - 1 SQL: The Structured Query Language:
SQL Views Presented by: Dr. Samir Tartir
SQL Grouping, Ordering & Arithmetics Presented by: Dr. Samir Tartir
Presentation transcript:

SQL: Set Operations & Nested Queries. Presented by: Dr. Samir Tartir Session - 6 Sequence - 3 SQL: Set Operations & Nested Queries. Presented by: Dr. Samir Tartir

Outline Sets Nested queries Correlated nested queries Contains Exists Handling NULLs

SET OPERATIONS SQL has directly incorporated some set operations UNION MINUS INTERSECT The resulting relations of these set operations are sets of tuples duplicate tuples are eliminated from the result The set operations apply only to union compatible relations The two relations must have the same attributes and the attributes must appear in the same order

Example Make a list of all project names for projects that involve an employee whose last name is 'Smith' as a worker or as a manager of the department that controls the project. SELECT PNAME FROM PROJECT, WORKS_ON, EMPLOYEE WHERE PNUMBER = PNO AND ESSN = SSN AND LNAME = 'Smith' UNION SELECT PNAME FROM PROJECT, DEPARTMENT, EMPLOYEE WHERE DNUM = DNUMBER AND MGRSSN = SSN AND LNAME = 'Smith'

EXPLICIT SETS It is possible to use a static explicit (enumerated) set of values in the WHERE-clause. Example: Retrieve the social security numbers of all employees who work on project number 1, 2, or 3. SELECT DISTINCT ESSN FROM WORKS_ON WHERE PNO IN (1, 2, 3)

NESTED QUERIES A nested query complete SELECT query specified within the WHERE-clause of another query, called the outer query Example: Retrieve the name and address of all employees who work for the 'Research' department. SELECT FNAME, LNAME, ADDRESS FROM EMPLOYEE WHERE DNO IN (SELECT DNUMBER FROM DEPARTMENT WHERE DNAME='Research') Outer Query Nested Query

NESTED OF QUERIES (contd.) The nested query selects the number of the 'Research' department The outer query select an EMPLOYEE tuple if its DNO value is in the result of the nested query The comparison operator IN compares a value v with a set (or multi-set) of values V, and evaluates to TRUE if v is one of the elements in V

CORRELATED NESTED QUERIES In general, we can have several levels of nested queries A reference to an unqualified attribute refers to the relation declared in the innermost nested query In the previous example, the nested query is not correlated with the outer query

CORRELATED NESTED QUERIES The result of a correlated nested query is different for each tuple (or combination of tuples) of the relation(s) the outer query Example Retrieve the name of each employee who has a dependent with the same first name as the employee. SELECT E.FNAME, E.LNAME FROM EMPLOYEE AS E WHERE E.SSN IN ( SELECT ESSN FROM DEPENDENT WHERE ESSN = E.SSN AND E.FNAME = DEPENDENT_NAME)

CORRELATED NESTED QUERIES (contd.) In the last query, the nested query has a different result depending on the current record of the outer query A query written with nested SELECT... FROM... WHERE... blocks and using the = or IN comparison operators can always be expressed as a single block query. SELECT E.FNAME, E.LNAME FROM EMPLOYEE E, DEPENDENT D WHERE E.SSN = D.ESSN AND E.FNAME = D.DEPENDENT_NAME

THE CONTAINS FUNCTION The original SQL as specified for SYSTEM R also had a CONTAINS comparison operator, which is used in conjunction with nested correlated queries This operator was dropped from the language, possibly because of the difficulty in implementing it efficiently Most implementations of SQL do not have this operator We will still explain the operator for comprehensiveness of the course The CONTAINS operator compares two sets of values, and returns TRUE if one set contains all values in the other set Reminiscent of the division operation of algebra

THE CONTAINS FUNCTION (contd.) Retrieve the name of each employee who works on all the projects controlled by department number 5. SELECT FNAME, LNAME FROM EMPLOYEE WHERE ((SELECT PNO FROM WORKS_ON WHERE SSN = ESSN) CONTAINS (SELECT PNUMBER FROM PROJECT WHERE DNUM = 5))

THE CONTAINS FUNCTION (contd.) In the last query, the second nested query, which is not correlated with the outer query, retrieves the project numbers of all projects controlled by department 5 The first nested query, which is correlated, retrieves the project numbers on which the employee works, which is different for each employee tuple because of the correlation

THE EXISTS FUNCTION EXISTS is used to check whether the result of a correlated nested query is empty (contains no tuples) or not

THE EXISTS FUNCTION (contd.) Retrieve the name of each employee who has a dependent with the same first name as the employee. SELECT FNAME, LNAME FROM EMPLOYEE WHERE EXISTS (SELECT * FROM DEPENDENT WHERE SSN = ESSN AND FNAME=DEPENDENT_NAME)

THE EXISTS FUNCTION (contd.) Retrieve the names of employees who have no dependents. SELECT FNAME, LNAME FROM EMPLOYEE WHERE NOT EXISTS (SELECT * FROM DEPENDENT WHERE SSN = ESSN) The correlated nested query retrieves all DEPENDENT tuples related to an EMPLOYEE tuple. If none exist, the EMPLOYEE tuple is selected

NULLS IN SQL QUERIES SQL allows queries that check if a value is NULL (missing or undefined or not applicable) SQL uses IS or IS NOT to compare NULLs because it considers each NULL value distinct from other NULL values Equality (or any other) comparison is not appropriate. A NULL value is not the same as empty string.

NULLS IN SQL QUERIES (contd.) Retrieve the names of all employees who do not have supervisors. SELECT FNAME, LNAME FROM EMPLOYEE WHERE SUPERSSN IS NULL Note: If a join condition is specified, tuples with NULL values for the join attributes are not included in the result

SUMMARY SQL provides natural ways for handling set operations. Complex queries can be built using nested queries. Nested queries can be independent or correlated. CONTAINS allows developers to check if the results of one query contain results of another query. EXISTS allows developers to check if the results of one query is empty or not. SQL has a special way of dealing with NULLs

Resources & References Dr. Samir Tartir Email: startir@philadelphia.edu.jo Website: http://www.philadelphia.edu.jo/academics/startir Fundamentals of Database Systems by El Masri & Navathe. Publisher : Addison-Wesley, 5th edition, 2006.