CS 338 Nested SQL Queries

Similar documents
CS 338 Basic SQL Part II

NESTED QUERIES AND AGGREGATION CHAPTER 5 (6/E) CHAPTER 8 (5/E)

NESTED QUERIES AND AGGREGATION CHAPTER 5 (6/E) CHAPTER 8 (5/E)

SQL: Advanced Queries, Assertions, Triggers, and Views. Copyright 2012 Ramez Elmasri and Shamkant B. Navathe

Introduction to SQL. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

Simple SQL Queries (contd.)

CSC 742 Database Management Systems

SQL STRUCTURED QUERY LANGUAGE

Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries

Key Points. COSC 122 Computer Fluency. Databases. What is a database? Databases in the Real-World DBMS. Database System Approach

COSC 122 Computer Fluency. Databases. Dr. Ramon Lawrence University of British Columbia Okanagan

ECE 650 Systems Programming & Engineering. Spring 2018

Slides by: Ms. Shree Jaswal

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

SQL-99: Schema Definition, Basic Constraints, and Queries. Create, drop, alter Features Added in SQL2 and SQL-99

SQL. Copyright 2013 Ramez Elmasri and Shamkant B. Navathe

COSC 304 Introduction to Database Systems SQL. Dr. Ramon Lawrence University of British Columbia Okanagan

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

SQL Queries. COSC 304 Introduction to Database Systems SQL. Example Relations. SQL and Relational Algebra. Example Relation Instances

Relational Model History. COSC 304 Introduction to Database Systems. Relational Model and Algebra. Relational Model Definitions.

SQL. SQL DDL Statements

Relational Model History. COSC 416 NoSQL Databases. Relational Model (Review) Relation Example. Relational Model Definitions. Relational Integrity

SQL: A COMMERCIAL DATABASE LANGUAGE. Complex Constraints

Database design process

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan

COSC344 Database Theory and Applications. Lecture 6 SQL Data Manipulation Language (1)

Database Technology. Topic 3: SQL. Olaf Hartig.

CS 348 Introduction to Database Management Assignment 2

Database Technology. Topic 2: Relational Databases and SQL. Olaf Hartig.

Chapter 8: Relational Algebra

More SQL: Complex Queries, Triggers, Views, and Schema Modification

Outline. Note 1. CSIE30600 Database Systems More SQL 2

COSC344 Database Theory and Applications. σ a= c (P) S. Lecture 4 Relational algebra. π A, P X Q. COSC344 Lecture 4 1

The Relational Algebra

Relational Algebra 1

Database Systems External Sorting and Query Optimization. A.R. Hurson 323 CS Building

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

Relational Algebra. Ron McFadyen ACS

Assertions, Views, and Programming. CS157A Chris Pollett Oct. 31, 2005.

ECE 650 Systems Programming & Engineering. Spring 2018

SQL Structured Query Language Introduction

Chapter 8. Joined Relations. Joined Relations. SQL-99: Schema Definition, Basic Constraints, and Queries

Hand in: the database picture (png or jpeg or ) for question 2, the queries (as SQL statements) for question 4

Ref1 for STUDENT RECORD DB: Ref2 for COMPANY DB:

Algorithms for Query Processing and Optimization. 0. Introduction to Query Processing (1)

More SQL: Complex Queries, Triggers, Views, and Schema Modification

Overview Relational data model

Relational Model. CS 377: Database Systems

Outline. Textbook Chapter 6. Note 1. CSIE30600/CSIEB0290 Database Systems Basic SQL 2

Mobile and Heterogeneous databases Distributed Database System Query Processing. A.R. Hurson Computer Science Missouri Science & Technology

CSIE30600 Database Systems Basic SQL 2. Outline

Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Chapter 6 Outline. Unary Relational Operations: SELECT and

QUERY PROCESSING & OPTIMIZATION CHAPTER 19 (6/E) CHAPTER 15 (5/E)

SQL (Structured Query Language) Truong Tuan Anh CSE-HCMUT

Chapter 6: RELATIONAL DATA MODEL AND RELATIONAL ALGEBRA

Query Processing SL03

What is Data? Volatile vs. persistent data Our concern is primarily with persistent data

What is Data? ANSI definition: Volatile vs. persistent data. Data. Our concern is primarily with persistent data

Database Systems CSE 303. Outline. Lecture 06: SQL. What is Sub-query? Sub-query in WHERE clause Subquery

More SQL: Complex Queries, Triggers, Views, and Schema Modification

Query Decomposition and Data Localization

Note that it works well to answer questions on computer instead of on the board.

CONSTRAINTS AND UPDATES CHAPTER 3 (6/E) CHAPTER 5 (5/E)

CS430 Final March 14, 2005

Database Management System (15ECSC208) UNIT I: Chapter 2: Relational Data Model and Relational Algebra

Query Processing & Optimization. CS 377: Database Systems

Why Relational Databases? Relational databases allow for the storage and analysis of large amounts of data.

SQL (Structured Query Language) Truong Tuan Anh CSE-HCMUT

CS5300 Database Systems

PES Institute of Technology Bangalore South Campus (1 K.M before Electronic City,Bangalore ) Department of MCA. Solution Set - Test-II

BASIC SQL CHAPTER 4 (6/E) CHAPTER 8 (5/E)

Part 1 on Table Function

School of Computing and Information Technology Session: Spring CSCI835 Database Systems (Bridging Subject) Sample class test 23 July 2018

SQL: A COMMERCIAL DATABASE LANGUAGE. Data Change Statements,

L130 - DATABASE MANAGEMENT SYSTEMS LAB CYCLE-1 1) Create a table STUDENT with appropriate data types and perform the following queries.

Chapter 3. Algorithms for Query Processing and Optimization

CMP-3440 Database Systems

Chapter 9. MORE SQL: Assertions, Views, and Programming Techniques. Constraints as Assertions. Assertions: An Example

COSC344 Database Theory and Applications. Lecture 5 SQL - Data Definition Language. COSC344 Lecture 5 1

SQL Introduction. CS 377: Database Systems

CS 377 Database Systems

Query 2: Pnumber Dnum Lname Address Bdate 10 4 Wallace 291 Berry, Bellaire, TX Wallace 291 Berry, Bellaire, TX

Chapter 8: The Relational Algebra and The Relational Calculus

Class Information. Textbooks. Grading. Final Project. Grading. CS 440: Database Management Systems. Instructor:

DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E)

Intermediate SQL ( )

A taxonomy of SQL queries Learning Plan

BASIC SQL CHAPTER 4 (6/E) CHAPTER 8 (5/E)

Chapter 18 Strategies for Query Processing. We focus this discussion w.r.t RDBMS, however, they are applicable to OODBS.

Introduction to SQL Part 2 by Michael Hahsler Based on slides for CS145 Introduction to Databases (Stanford)

Session Active Databases (2+3 of 3)

LAB 3 Part 1 ADBC - Interactive SQL Queries-Advanced

SQL:Union, Intersection, Difference. SQL: Natural Join

COSC344 Database Theory and Applications. COSC344 Lecture 15 1

SQL- Updates, Asser0ons and Views

Querying Data with Transact SQL

CS 338 Functional Dependencies

Overview of Data Management

HW1 is due tonight HW2 groups are assigned. Outline today: - nested queries and witnesses - We start with a detailed example! - outer joins, nulls?

Transcription:

CS 338 Nested SQL Queries Bojana Bislimovska Spring 2017

Exercises 2. A database for an organization that shelters animals, and people can go and adopt animals that they shelter, has the following set of relations: Animals(ID: integer, Name: string, Owner: integer, DateAdmitted: date, Type:string) Adopter(PID: integer, Name: string, Address: string) Adoption(AnimalID:integer, PID:integer, AdoptDate: date, chipno: integer) where the type of each attribute is given following the attribute (e.g., ID: integer), and the primary keys are underlined. Furthermore, (a) Animals stores information about the animals. Each is given an ID, and their names (attribute Name) together with the PID of their owners (attribute Owner) if they have been adopted, and their date of admission is recorded (so Owner is a foreign key to Adopter.PID). Type refers to the type of animal (dog, cat, etc). Note that this is a historical table, so it keeps track of all admitted animals, even if some of them have been adopted. (b) Adopter is the relation that holds information about animal adopters. The PID of the person who has adopted an animal is recorded as well as his/her Name and Address. (c) Adoption.AnimalID refers to Animals.ID and has the same type. Similarly, Adoption.PID refers to Adopter. PID and has the same type. Attribute chipno stores the number on the microchip that is implanted on the animal for tracking.

Exercises Formulate the following queries in SQL: 1) For each animal type, list the animal type and total number of adoptions on 14 June 2015. 2) For each adopter who has made at least two adoptions, list their names and addresses.

Outline Nested Queries Correlated Nested Queries EXISTS and UNIQUE functions Exercises

Queries that require existing values in the DB to be fetched and then used in a comparison condition Select-from-where block inside another SQL query Example Nested Queries Give biographical information for directors of profitable movies SELECT name, birth, city FROM ( SELECT director FROM Film WHERE gross > budget) AS Profitable, Person WHERE director = name

Nested queries can also appear in the WHERE clause or in the SELECT clause Example Find the film with the highest budget Nested Queries SELECT * FROM Film WHERE budget = (SELECT MAX(budget) FROM Film);

Comparison operator IN Compares a value with a set of values and evaluates to TRUE if the value is one of the elements in the set Example Query 4: Make a list of all project numbers for projects that involve an employee whose last a e is S ith, either as a worker or as a a ager of the depart e t that co trols the project. SELECT DISTINCT Pnumber FROM PROJECT WHERE Pnumber IN (SELECT Pnumber FROM PROJECT, DEPARTMENT, EMPLOYEE WHERE Dnum=Dnumber AND OR Pnumber IN (SELECT Pno FROM WORKS_ON, EMPLOYEE Nested Queries Mgr_ss =Ss AND L a e= S ith ) WHERE Ess =Ss AND L a e= S ith );

Nested Queries Tuples of values can also be used in comparisons with IN Explicit set of values can also be used, instead of a nested query SELECT DISTINCT Essn FROM WORKS_ON WHERE Pno IN (1,2,3)

Nested Queries Other comparison operators can be used ANY (or SOME) operator returns TRUE if the value is equal to some value in the set Equivalent to IN >, >=, <,<=, <> can also be used with ANY (SOME) ALL operator every value in the set

Correlated Nested Queries Major research Correlated query when a nested query references some attribute of a relation declared in the outer query Evaluated once for each tuple Easiest to understand (and write correctly) if all column names are qualified by their relation names

The EXISTS and UNIQUE Functions in SQL Major research Boolean functions that return TRUE or FALSE Used in the WHERE clause condition EXISTS function checks if a result of a nested query is empty or not NOT EXISTS Example Retrieve the names of employees who have no dependents SELECT Fname, Lname FROM EMPLOYEE WHERE NOT EXISTS (SELECT * FROM DEPENDENT WHERE Ssn=Essn); UNIQUE function checks if there are duplicate tuples in the result of a query

Exercises 1. Consider the following relations: Emp(eno, ename, title, city) Proj(pno, pname, budget, city) Works(eno, pno, resp, dur) Pay(title, salary) where the primary keys are underlined, and Emp.title is a foreign key to Pay.title, Works.eno is a foreign key to Emp.eno, and Works.pno is a foreign key to Proj.pno. Write the following SQL query: 1) Retrieve the employee number and responsibility of all persons assigned to any project for which more than 2 persons share the same responsibility for that project.

Exercises 2. A database for an organization that shelters animals, and people can go and adopt animals that they shelter, has the following set of relations: Animals(ID: integer, Name: string, Owner: integer, DateAdmitted: date, Type:string) Adopter(PID: integer, Name: string, Address: string) Adoption(AnimalID:integer, PID:integer, AdoptDate: date, chipno: integer) where the type of each attribute is given following the attribute (e.g., ID: integer), and the primary keys are underlined. Furthermore, Owner is a foreign key to Adopter.PID; Adoption.AnimalID refers to Animals.ID; Adoption.PID refers to Adopter. PID and has the same type. Formulate the following query in SQL: 1) Find the names of adopters who have adopted every type of animal available at the animal shelter.