SQL Nested & Complex Queries. CS 377: Database Systems



Similar documents
Introduction to SQL: Data Retrieving

Chapter 8. SQL-99: SchemaDefinition, Constraints, and Queries and Views

Part 4: Database Language - SQL

CS 338 Join, Aggregate and Group SQL Queries

Part A: Data Definition Language (DDL) Schema and Catalog CREAT TABLE. Referential Triggered Actions. CSC 742 Database Management Systems

CSC 443 Data Base Management Systems. Basic SQL

Lab Assignment Creating a Relational Database Schema from ER Diagram, Populating the Database and Querying over the database with SQL

Relational Schema. CS 4700/6700 A Sample of Small Database Design Using Microsoft Access

The Relational Algebra

CHAPTER 8: SQL-99: SCHEMA DEFINITION, BASIC CONSTRAINTS, AND QUERIES

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

Structured Query Language (SQL)

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

New York University Computer Science Department Courant Institute of Mathematical Sciences

VIEWS virtual relation data duplication consistency problems

4. SQL. Contents. Example Database. CUSTOMERS(FName, LName, CAddress, Account) PRODUCTS(Prodname, Category) SUPPLIERS(SName, SAddress, Chain)

Summary on Chapter 4 Basic SQL

Figure 14.1 Simplified version of the

{ PreviousEducation ( CollegeName, StartDate, EndDate, { Degree (DegreeName, Month, Year) }, { Transcript (CourseName, Semester, Year, Grade) } ) }

How To Create A Table In Sql (Ahem)

Using Multiple Operations. Implementing Table Operations Using Structured Query Language (SQL)

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries

Introduction to SQL and SQL in R. LISA Short Courses Xinran Hu

Information Systems SQL. Nikolaj Popov

Week 4 & 5: SQL. SQL as a Query Language

More SQL: Assertions, Views, and Programming Techniques

Chapter 9, More SQL: Assertions, Views, and Programming Techniques

ER & EER to Relational Mapping. Chapter 9 1

1 Structured Query Language: Again. 2 Joining Tables

Chapter 1 Overview of the SQL Procedure

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA

SUBQUERIES AND VIEWS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 6

Relational Database: Additional Operations on Relations; SQL

CIS 631 Database Management Systems Sample Final Exam

Relational Algebra. Query Languages Review. Operators. Select (σ), Project (π), Union ( ), Difference (-), Join: Natural (*) and Theta ( )

Oracle SQL. Course Summary. Duration. Objectives

More on SQL. Juliana Freire. Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan

10CS54: DATABASE MANAGEMENT SYSTEM

Databases 2011 The Relational Model and SQL

AN INTRODUCTION TO THE SQL PROCEDURE Chris Yindra, C. Y. Associates

Lab Manual. Database Systems COT-313 & Database Management Systems Lab IT-216

UNIT 6. Structured Query Language (SQL) Text: Chapter 5

Examine the structure of the EMPLOYEES table: EMPLOYEE_ID NUMBER Primary Key FIRST_NAME VARCHAR2(25) LAST_NAME VARCHAR2(25)

Introduction to database design

b. Examine the following histories. Draw their serialization graph and identify which of them is serializable given reasons.

Performing Queries Using PROC SQL (1)

Introduction to SQL C H A P T E R3. Exercises

SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

Lecture 25: Database Notes

Financial Data Access with SQL, Excel & VBA

1. SELECT DISTINCT f.fname FROM Faculty f, Class c WHERE f.fid = c.fid AND c.room = LWSN1106

SQL Query Evaluation. Winter Lecture 23

Introduction to Microsoft Jet SQL

Boats bid bname color 101 Interlake blue 102 Interlake red 103 Clipper green 104 Marine red. Figure 1: Instances of Sailors, Boats and Reserves

Example Instances. SQL: Queries, Programming, Triggers. Conceptual Evaluation Strategy. Basic SQL Query. A Note on Range Variables

SQL: Queries, Programming, Triggers

Lecture 4: More SQL and Relational Algebra

SQL SELECT Query: Intermediate

SQL: Queries, Programming, Triggers

The Structured Query Language. De facto standard used to interact with relational DB management systems Two major branches

Instant SQL Programming

featuring data privacy Andres Avelino Campos Sainz A Project submitted in partial fulfillment of the requirements for the degree of

Appendix A Practices and Solutions

VBScript Database Tutorial Part 1

New York University Computer Science Department Courant Institute of Mathematical Sciences

Introduction to tuple calculus Tore Risch

Oracle Database: SQL and PL/SQL Fundamentals

SQL: QUERIES, CONSTRAINTS, TRIGGERS

Data Structure: Relational Model. Programming Interface: JDBC/ODBC. SQL Queries: The Basic From

SECURITY CHAPTER 24 (6/E) CHAPTER 23 (5/E)

Advanced SQL. Lecture 3. Outline. Unions, intersections, differences Subqueries, Aggregations, NULLs Modifying databases, Indexes, Views

Chapter 5. SQL: Queries, Constraints, Triggers


DBMS / Business Intelligence, SQL Server

Oracle Database: SQL and PL/SQL Fundamentals NEW

Figure 4.12.Insurancedatabase.

Relational Databases

Introduction to SQL for Data Scientists

Oracle Database: SQL and PL/SQL Fundamentals

History of SQL. Relational Database Languages. Tuple relational calculus ALPHA (Codd, 1970s) QUEL (based on ALPHA) Datalog (rule-based, like PROLOG)

COSC344 Database Theory and Applications. Lecture 23 Security and Auditing. COSC344 Lecture 23 1

Relational Algebra 2. Chapter 5.2 V3.0. Napier University Dr Gordon Russell

Natural Language Interfaces (NLI s)

Introduction to Proc SQL Steven First, Systems Seminar Consultants, Madison, WI

Katie Minten Ronk, Steve First, David Beam Systems Seminar Consultants, Inc., Madison, WI

T-SQL STANDARD ELEMENTS

MySQL for Beginners Ed 3

Advance DBMS. Structured Query Language (SQL)

Exercises. a. Find the total number of people who owned cars that were involved in accidents

3.GETTING STARTED WITH ORACLE8i

A Comparison of Database Query Languages: SQL, SPARQL, CQL, DMX

Object-Oriented Query Languages: Object Query Language (OQL)

IT2304: Database Systems 1 (DBS 1)

Fundamentals of Database Systems. Laboratory Manual 1. Rajshekhar Sunderraman. Georgia State University. August 2010

Simple SQL Queries (3)

Functional Dependency and Normalization for Relational Databases

IT2305 Database Systems I (Compulsory)

Relational Normalization: Contents. Relational Database Design: Rationale. Relational Database Design. Motivation

SQL (structured query language)

How To Use The Database In Jdbc.Com On A Microsoft Gdbdns.Com (Amd64) On A Pcode (Amd32) On An Ubuntu (Amd66) On Microsoft

Transcription:

SQL Nested & Complex Queries CS 377: Database Systems

Recap: Basic SQL Retrieval Query A SQL query can consist of several clauses, but only SELECT and FROM are mandatory SELECT <attribute list> FROM <table list> [WHERE <condition on the tables (join or selection)>] [ORDER BY <attribute list>] [LIMIT <number of tuples>]

Subquery Subquery: A parenthesized SELECT-FROM-WHERE statement which results in a relation of tuples Syntax: (SELECT-command) Usage Inside WHERE clause (nested query) Inside FROM clause (temporal relation)

Nested Query Nested query is when a subquery is specified within the WHERE clause of another query, called the outer query Syntax: SELECT FROM WHERE (SELECT FROM WHERE ) Nested Query

Nested Query (2) Forms of nested query: Set membership: IN and NOT IN Set comparison: compareop ANY or compareop ALL Test for empty relation: EXIST In theory, nesting can be arbitrarily deep but in practice the number of levels is limited

Example Query: Nested Query Retrieve the name and address of all employees who work for the Research department Soln #1: SELECT fname, lname FROM employee, department WHERE dno = dnumber AND dname = Research ; Soln #2: SELECT fname, lname FROM EMPLOYEE WHERE dno IN (SELECT pnumber FROM department WHERE dname = Research )

Example Query: Nested Query (2) Find fname, lname of employees that do not have any dependents SELECT fname, lname FROM employee WHERE ssn NOT IN (SELECT essn FROM dependent);

Correlated Nested Queries Correlated: inner query (query in the WHERE clause) uses one or more attributes from relation(s) specified in the outer query Uncorrelated: inner query is a stand-alone query that can be executed independently from the outer query Example Syntax: SELECT FROM R1 WHERE attr1 IN (SELECT attr2 FROM R2 WHERE R2.attr3 = R1.attr4)

Example Query: Correlated Nested Query Retrieve the name of each employee who has a dependent with the same 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 = name);

Correlated Nested Query Execution FOR (each tuple X in the outer query) DO { } Execute inner query using attribute value of tuple X Example: SELECT fname, lname, salary, uno FROM employee a WHERE salary >= ALL (SELECT salary FROM employee a WHERE b.dno = a.dno)

Correlated Nested Query Execution (2) FName LName DNo Salary John Smith 4 50,000 James Bond 4 80,000 Jane Brown 3 60,000 Jennifer Wallace 5 30,000 James Borg 1 55,000 Joyce English 5 25,000 Alicia Wong 4 70,000 Outer tuple a = John Smith 4 50,000 WHERE 50,000 >= ALL (SELECT salary FROM employee b where b.dno = 4) => FALSE

Correlated Nested Query Execution (2) FName LName DNo Salary John Smith 4 50,000 James Bond 4 80,000 Jane Brown 3 60,000 Jennifer Wallace 5 30,000 James Borg 1 55,000 Joyce English 5 25,000 Alicia Wong 4 70,000 Outer tuple a = James Bond 4 80,000 WHERE 80,000 >= ALL (SELECT salary FROM employee b where b.dno = 4) => TRUE (select tuple)

Correlated Nested Query Execution (2) FName LName DNo Salary James Bond 4 80,000 Jane Brown 3 60,000 Jennifer Wallace 5 30,000 James Borg 1 55,000 SELECT fname, lname, salary, uno FROM employee a WHERE salary >= ALL (SELECT salary FROM employee a WHERE b.dno = a.dno) Return name, salary, and department number of employees whose salary is the highest of all employees in his/her department

Correlated Nested Query Scope Scoping rules defines where a name is visible Each nesting level constitutes a new inner scope Names of relations and their attributes in outer query are visible in the inner query but not the converse Attribute name specified inside an inner query is associated with nearest relation

Example: Scoping Nested Queries SELECT <attribute list from R1 and/or R2> FROM R1, R2 WHERE <conditions from R1 and/or R2> AND (SELECT <attribute list from R1, R2, R3 and/or R4> FROM R3, R4 WHERE <conditions from R1, R2, R3, and/or R4>) Attributes of R1 and R2 are visible in the inner query Attributes of R3 and R4 are not visible in the outer query

Example: Scoping Nested Queries (2) SELECT <attribute list from R1 and/or R2> FROM R1, R2 WHERE <conditions from R1 and/or R2> AND (SELECT x FROM R3, R4 WHERE <conditions from R1, R2, R3, and/or R4>) If R3 or R4 contains the attribute name x, then x refers to that attribute in R3 or Rf If R3 and R4 does not contain the attribute name x, then x in the inner query refers to the attribute in R1 or R2

SQL Query: EXISTS Checks whether the result of a correlated nested query is empty (contains no tuples) or not Example: Retrieve the names of employees who have no dependents SELECT fname, lname FROM employee WHERE NOT EXISTS (SELECT * FROM dependent WHERE ssn = essn);

SQL Query: Aggregate Functions COUNT, SUM, MAX, MIN, AVG can be used in the SELECT clause Example: Find the sum, maximum, minimum, and average salary among all employees in the Research department SELECT SUM(salary), MAX(salary) MIN(salary), AVG(salary) FROM employee, department WHERE dno = dnumber AND dname = Research

SQL Query: Aggregate Functions (2) Name given to the selected aggregate function attribute is the same as the function call SELECT MAX(salary), MIN(salary), AVG(salary) FROM employee; max(salary) min(salary) avg(salary) Rename selected attributes with AS alias clause inside the SELECT clause SELECT MAX(salary) AS max, MIN(salary) AS min, AVG(salary) AS average FROM employee;

SQL Example: Aggregate Function Retrieve the names of all employees who have two or more dependents

SQL Query: GROUP BY Apply aggregate functions to subgroups of tuples in a relation Corresponds to grouping and aggregate function in RA Grouping attributes: attributes used to group the tuples Function is applied to each subgroup independently Syntax: SELECT <attribute list> FROM <table list> WHERE <condition on the tables> GROUP BY <grouping attributes>

GROUP BY Execution A query with GROUP BY clause is processed as follows: 1. Select the tuples that satisfies the WHERE condition 2. Selected tuples from (1) are grouped based on their value in the grouping attributes 3. One or more set functions is applied to the group

SQL Example: GROUP BY For each department, retrieve the department number, the number of employees in the department, and their average salary SELECT dno, count(*), avg(salary) FROM employee GROUP BY dno

SQL Query: GROUP BY details What happens if we do not include certain grouping attributes in the SELECT clause? What happens if we include an attribute in the SELECT clause that is not in the group by attribute list?

SQL Query: HAVING HAVING clause specifies a selection condition on groups (rather than individual tuples) Filters out groups that do not satisfy the group condition Syntax: SELECT <attribute list> FROM <table list> WHERE <condition on the tables> GROUP BY <grouping attributes> HAVING <group condition>

SQL Query: HAVING Details Group condition is a condition on a set of tuples > must use a grouping attribute inside the HAVING clause Process order: 1. Select tuples that satisfy the WHERE condition 2. Selected tuples from (1) are grouped based on their value in the grouping attributes 3. Filter groups so only those satisfying the condition are left 4. Set functions in the SELECT clause are applied to these groups

SQL Example: HAVING For each project on which more than two employees work, retrieve the project number, project name, and the number of employees who work on that project SELECT pnumber, pname, COUNT(*) FROM project, works_on WHERE pnumber = pno GROUP BY pnumber, pname HAVING COUNT(*) > 2;

SQL Example: HAVING (2) For each department with at least 2 employees, find the department name, and the number of employees in that department that earns greater than $40K SELECT dname, COUNT(ssn) FROM department, employee WHERE dnumber = dno AND salary > 40000 GROUP BY dname HAVING COUNT(ssn) > 2; Is this right? What does it return?

SQL Example: HAVING (2) Previous query only counts the number of departments that have at least 2 employees that earn more then $40K. SELECT dname, COUNT(ssn) FROM employee, department WHERE dno = dnumber AND dno IN ( SELECT dno FROM employee GROUP BY dno HAVING COUNT(ssn) >= 2) AND salary > 40000 GROUP BY dname

Summary of SQL Queries SELECT [DISTINCT] <attribute list> FROM <table list> [WHERE <condition on the tables>] [GROUP BY <grouping attributes>] [HAVING <group condition>] [ORDER BY <attribute list> ASC DESC] [LIMIT <number of tuples>] This has every possible clause of a SQL command included

Query Formulation Techniques: INTERSECT How to compute the intersection of two sets when the system does not support INTERSECT (e.g., MySQL)? x IN (set1 INTERSECT set2) (x IN set1) AND (x IN set2)

SQL Example: INTERSECT Find fname and lname of employees who work on some project controlled by the Research department and also on some project controlled by the Administration department SELECT fname, lname FROM employee WHERE ssn IN ( SELECT essn FROM works_on, project, department WHERE pno = pnumber AND dnum = dnumber AND dname = Research ) AND ssn IN ( SELECT essn FROM works_on, project, department WHERE pno = pnumber AND dnum = dnumber AND dname = Administration );

Query Formulation Techniques: DIFFERENCE How to compute the difference of two sets when SQL doesn t support set difference? x IN (set1 - set2) (x IN set1) AND (x NOT IN set2)

SQL Example: DIFFERENCE Find SSN of employees in the Research department who has no dependents SELECT ssn FROM employee WHERE ssn IN ( SELECT ssn FROM employee, department WHERE dno = dnumber AND dname = Research ) AND ssn NOT IN ( SELECT essn FROM dependent)

Query Formulation Techniques: Superset How to formulate set1 is a superset (contains) of another set, set2? set1 CONTAINS set2 <=> set2 - set1 = EMPTY SELECT FROM WHERE NOT EXISTS (SELECT * FROM <table> WHERE x IN set2 AND x NOT IN set1)

Query Formulation Techniques: Subset How to formulate set1 is a subset (part of) of another set, set2? set1 SUBSET set2 <=> set1 - set2 = EMPTY SELECT FROM WHERE NOT EXISTS (SELECT * FROM <table> WHERE x IN set1 AND x NOT IN set2)

Query Formulation Techniques: Division How to compute the division between two relations? Example: Find lname of all employees who work on all projects controlled by department number RA: H1 = pnumber (PROJECT./ dnum=dnumber H2 = essn,pno (WORKS ON) H3 = H2 H1 Answer = fname,lname (EMPLOYEE./ ssn = ssn H3) dname= Research (DEPARTMENT)) SQL: Use NOT EXISTS and set difference (can think of it as set of projects worked on employee contains set of projects controlled by department 4)

SQL Example: DIVISION Find lname of all employees who work on all projects controlled by department number SELECT fname, lname FROM employee WHERE NOT EXISTS ( SELECT pnumber FROM project WHERE pnumber IN (SELECT pnumber FROM project WHERE dnum = 4) AND pnumber NOT IN (SELECT dno FROM works_on projects worked on WHERE essn = ssn)); by employee project controlled by Research

Query Formulation Techniques: Only How to compute queries that ask for only? Example: Find the names of projects that are worked on by only employees in the Research department? Formulate the solution using a subset condition: Employees working on project p are a subset of employees in the Research department

SQL Example: Only Find the names of projects that are worked on by only employees in the Research department? SELECT pname FROM project WHERE NOT EXISTS ( SELECT ssn FROM employee WHERE ssn IN (SELECT essn FROM works_on WHERE pno = pnumber) AND ssn NOT IN (SELECT ssn FROM employee, department employees from WHERE dno = dnumber research department AND dname = Research )); employees working on project p

Query Formulation Techniques: Most Number of How to compute queries that ask for the most number of some attribute? Example: Find the name of the departments with most number of employees? Ans: Use nested query with the max function SELECT dname FROM department, employee WHERE dno = dnumber GROUP BY dname HAVING COUNT(ssn) = (SELECT MAX(COUNT(ssn)) FROM employee GROUP BY dno);

SQL Practice (1) Find the name of the departments with 2 or more male employees

SQL Practice (2) Find the name of employees who have more than two dependents and work on more than 2 projects

SQL Practice (3) Find the name of the employees with the most number of dependents

SQL Practice (4) Find fname and lame of employees who works on all projects that are worked on by John Smith

SQL Nested & Complex Queries: Recap Nested Queries Aggregate Functions SQL Grouping GROUP BY HAVING Query Formulation Techniques