Database Management Systems. NESTING OF QUERIES  Some queries require that existing values in the database be retrieved and then used in a comparison.

Slides:



Advertisements
Similar presentations
COMPANY schema EMPLOYEE
Advertisements

SQL (cont’d) CSE3330 Spring 2014 Chengkai Li. NULL values.
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,
FEN More about SELECT, Nested selects GROUP BY, HAVING, ORDER BY Other joins Aggregate functions Views More about SQL.
Company Database. CREATE TABLE DEPARMENT ( DNAME VARCHAR(10) NOT NULL, DNUMBER INTEGER NOT NULL, MGRSSN CHAR(9), MGRSTARTDATE CHAR(9), PRIMARY KEY (DNUMBER),
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
METU Department of Computer Eng Ceng 302 Introduction to DBMS SQL-99: Schema Definition, Basic Constraints, and Queries by Pinar Senkul resources: mostly.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
Database Systems Chapter 8 ITM 354. Data Definition, Constraints, and Schema Changes Used in SQL to Create, Drop, and Alter the descriptions of the tables.
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,
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
FEN  Queries: SELECT  Data Manipulation: INSERT, UPDATE, DELETE SQL: Structured Query Language – Part 2.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: Constraints, and Queries and Views.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: Advanced Queries.
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.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
 Employee (fname, minit, lname, ssn, bdate, address, sex, salary, superssn, dno)  Department (dname, dnumber, mgrssn, mgrstartdate) 
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.
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 Yuh-Jzer Joung 莊 裕 澤 Dept. of Information Management National Taiwan University February, 2002 資料庫管理與資訊科技.
SQL-99: SchemaDefinition, Constraints, and Queries and Views.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Chapter 8 Part 2 SQL-99 Schema Definition, Constraints, Queries, and Views.
1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)
Structured Query Language
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
Structured Query Language (2) The main reference of this presentation is the textbook and PPT from : Elmasri & Navathe, Fundamental of Database Systems,
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.
Faculty of Information Science and Technology Mahanakorn University of Technology Topic 8 SQL-99: Schema Definition, Basic Constraints, and Queries.
Chapter 11 SQL DML. Retrieval Queries in SQL SQL has one basic statement for retrieving information from a database; the SELECT statement This is not.
1 Chapter 5 The SQL Language Data Definition Language The Data Definition Language (DDL) is used to create and destroy databases and database.
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 –continue with SQL (chapter.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Chapter SQL DML. Retrieval Queries in SQL SQL has one basic statement for retrieving information from a database; the SELECT statement This is not the.
SQL-99: SchemaDefinition, Constraints, and Queries and Views.
Introduction to Database Systems
COMP163 Database Management Systems September 18, 2008 Lecture 8 – Sections SQL Queries.
ISC321 Database Systems I Chapter 5: SQL: Advanced Queries, Assertions, Triggers, and Views Fall 2015 Dr. Abdullah Almutairi.
1 Database Systems SQL: Advanced Queries. 2 Union, Intersection, and Except (2)   Find the names of those people who are either a graduate student or.
Lecture # 25 Chapter 8 SQL-99: Schema Definition, Constraints, and Queries and Views Muhammad Emran Database Systems.
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.
1 Chapter (8) SQL The Relational Database Standard Objectives Presenting the main features of SQL SQL is the Structured Query Language SQL commands SQL.
The SQL Database Grammar
CS580 Advanced Database Topics
6/22/2018.
Retrieval Queries in SQL(DML)
Subject Name: DATA BASE MANAGEMENT SYSTEM Subject Code: 10cs54
11/9/2018.
Company Requirements.
Session - 6 Sequence - 2 SQL: The Structured Query Language:
SQL-99: Schema Definition, Constraints, and Queries and Views
SQL Grouping, Ordering & Arithmetics Presented by: Dr. Samir Tartir
SQL: Set Operations & Nested Queries. Presented by: Dr. Samir Tartir
Presentation transcript:

Database Management Systems

NESTING OF QUERIES  Some queries require that existing values in the database be retrieved and then used in a comparison condition.  A complete SELECT query, called a nested query, can be specified within the WHERE- clause of another query, called the outer query  Many of the previous queries can be specified in an alternative form using nesting

Query 1  Retrieve the name and address of all employees who work for the 'Research' department.  SELECT FNAME, LNAME, ADDRESS FROM EMPLOYEE, DEPARTMENT WHERE DNAME='Research' AND DNUMBER=DNO;  SELECTFNAME, LNAME, ADDRESS FROM EMPLOYEE WHEREDNO IN ( SELECT DNUMBER FROM DEPARTMENT WHERE DNAME='Research' )

NESTING OF QUERIES  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 either 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  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 this example, the nested query is not correlated with the outer query

Query 2  Make a list of all project numbers 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 PNUMBER FROMPROJECT, DEPARTMENT, EMPLOYEE WHERE DNUM=DNUMBER AND MGR_SSN=SSN AND LNAME='Smith') UNION (SELECT PNUMBER FROMPROJECT, WORKS_ON, EMPLOYEE WHEREPNUMBER=PNO AND ESSN=SSN AND LNAME='Smith')

Query 2 using nested queries SELECT distinct pnumber FROM project WHERE pnumber IN ( SELECT Pnumber FROM project, department, employee WHERE dnum=dnumber and mgr_ssn= ssn and lname='Smith') or pnumber IN ( SELECT pno FROM works_on, employee WHERE essn=ssn and lname='Smith')

Nested Queries  The first nested query selects the project numbers of projects that have a ‘Smith’ involved as manager  The second nested query selects the project numbers of projects that have a ‘Smith’ involved as a worker  In the outer query, we use the OR logical connective to retrieve a project tuple if the pnumber value of the tuple is in the result of either nested query

Nested Queries  SQL allows the use of tuples of values in comparisons by placing them within parentheses. To illustrate this, consider the following query: SELECT DISTINCT ESSN FROM WORKS_ON WHERE (PNO, HOURS) IN (SELECT PNO, HOURS FROM WORKS_ON WHERE ESSN=' ')  This query will select the social security numbers of all employees who work in the same (project, hours) combination on same project that employee 'John Smith' (whose ssn = ‘ ') works on.

Nested Queries  In addition to the IN operator, a number of other comparison operators can be used to compare single value v (typically an attribute name) to a set or multiset V (typically a nested query). The = ANY (or = SOME) operator returns TRUE if the value v is equal to some value in the set V and is hence equivalent to IN. The keywords ANY and SOME have the same meaning. Other operators that can be combined with ANY (or SOME) include >, >=,. The keyword ALL can also be combined with each of these operators MeaningOperator Equal to any member in the listIN Compare value to each value returned by the subqueryANY Compare value to every value returned by the subqueryALL

Query 3  Retrieve the names of employees whose salary is greater than the salary of all the employees in department 5 SELECT LNAME, FNAME FROM EMPLOYEE WHERE SALARY > ALL (SELECT SALARY FROM EMPLOYEE WHERE DNO=5)

Nested Queries  In general, we can have several levels of nested queries. We can once again be faced with possible ambiguity among attribute names if attributes of the same name exist—one in a relation in the FROM clause of the outer query, and another in a relation in the FROM clause of the nested query. The rule is that a reference to an unqualified attribute refers to the relation declared in the innermost nested query.

CORRELATED NESTED QUERIES  If a condition in the WHERE-clause of a nested query references an attribute of a relation declared in the outer query, the two queries are said to be correlated  The result of a correlated nested query is different for each tuple (or combination of tuples) of the relation(s) the outer query

Query 4  Retrieve the name of each employee who has a dependent with the same first name and same sex as the employee. SELECT E.FNAME, E.LNAME FROM EMPLOYEE E WHERE E.SSN IN (SELECT ESSN FROM DEPENDENT WHERE E.FNAME=DEPENDENT_NAME AND E.SEX=SEX)

CORRELATED NESTED QUERIES  Correlated subquery runs once for each row selected by the outer query. It contains a reference to a value from the row selected by the outer query.  Nested subquery runs only once for the entire nesting (outer) query. It does not contain any reference to the outer query row.

THE EXISTS FUNCTION  EXISTS is used to check whether the result of a correlated nested query is empty (contains no tuples) or not. Also we can use NOT EXISTS  Query 5 : Retrieve the name of each employee who has a dependent with the same first name as the employee. SELECT FNAME, LNAME FROMEMPLOYEE WHEREEXISTS (SELECT* FROMDEPENDENT WHERESSN=ESSN AND FNAME=DEPENDENT_NAME)

Query 5  List the names of employees who have no dependents SELECT fname, lname FROM employee WHERE NOT EXISTS ( SELECT * FROM dependent WHERE ssn=ESSN)

Query 6  Retrieve the name of each employee who works on all projects controlled by department number 5 SELECT lname, fname FROM employee where not exists (SELECT * FROM works_on B where (B.PNO in (select Pnumber from project where dnum=5)) AND NOT Exists ( select * from works_on c where c.essn=ssn and c.pno =B.pno))