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

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

Database Technology. Topic 3: SQL. Olaf Hartig.

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

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

Slides by: Ms. Shree Jaswal

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

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

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

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

SQL STRUCTURED QUERY LANGUAGE

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

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

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

Database design process

ECE 650 Systems Programming & Engineering. Spring 2018

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

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

The Relational Algebra

CS 338 Nested SQL Queries

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

Database Technology. Topic 3: SQL. Structured Query Language. Creating Tables SQL DDL. Creating Tables (Example) Modifying Table Definitions

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

A taxonomy of SQL queries Learning Plan

SQL. Copyright 2013 Ramez Elmasri and Shamkant B. Navathe

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

Basic SQL II. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

CS 338 Basic SQL Part II

Introduction to Query Processing and Query Optimization Techniques. Copyright 2011 Ramez Elmasri and Shamkant Navathe

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

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

Chapter 3. Algorithms for Query Processing and Optimization

CSC Web Programming. Introduction to SQL

Insertions, Deletions, and Updates

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

Chapter 6 The Relational Algebra and Calculus

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

What happens. 376a. Database Design. Execution strategy. Query conversion. Next. Two types of techniques

Overview Relational data model

Relational Algebra. Relational Algebra Overview. Relational Algebra Overview. Unary Relational Operations 8/19/2014. Relational Algebra Overview

CS 582 Database Management Systems II

SQL BASICS WITH THE SMALLBANKDB STEFANO GRAZIOLI & MIKE MORRIS

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

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

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

RELATIONAL DATA MODEL: Relational Algebra

CSEN 501 CSEN501 - Databases I

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.

Chapter 3: Introduction to SQL

The SQL data-definition language (DDL) allows defining :

Relational Database Management Systems for Epidemiologists: SQL Part I

SQL Data Manipulation Language. Lecture 5. Introduction to SQL language. Last updated: December 10, 2014

Lecture 6 - More SQL

Chapter 19 Query Optimization

Chapter 6 - Part II The Relational Algebra and Calculus

STRUCTURED QUERY LANGUAGE (SQL)

CS 2451 Database Systems: SQL

Chapter 6 Part I The Relational Algebra and Calculus

Chapter 3: Introduction to SQL

Relational Database Management Systems for Epidemiologists: SQL Part II

Chapter 6 5/2/2008. Chapter Outline. Database State for COMPANY. The Relational Algebra and Calculus

Intermediate SQL: Aggregated Data, Joins and Set Operators

SQL Data Query Language

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

SQL (Structured Query Language)

SQL - Lecture 3 (Aggregation, etc.)

QQ Group

The Relational Algebra and Calculus. Copyright 2013 Ramez Elmasri and Shamkant B. Navathe

Data Manipulation Language (DML)

CS 464/564 Introduction to Database Management System Instructor: Abdullah Mueen

CMP-3440 Database Systems

Based on the following Table(s), Write down the queries as indicated: 1. Write an SQL query to insert a new row in table Dept with values: 4, Prog, MO

SQL: Data Manipulation Language. csc343, Introduction to Databases Diane Horton Winter 2017

In This Lecture. Yet More SQL SELECT ORDER BY. SQL SELECT Overview. ORDER BY Example. ORDER BY Example. Yet more SQL

SQL: csc343, Introduction to Databases Renée J. Miller and Fatemeh Nargesian and Sina Sina Meraji. Winter 2018

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

CSIE30600 Database Systems Basic SQL 2. Outline

12. MS Access Tables, Relationships, and Queries

Wentworth Institute of Technology COMP570 Database Applications Fall 2014 Derbinsky. Physical Tuning. Lecture 10. Physical Tuning

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

Information Systems Engineering. SQL Structured Query Language DML Data Manipulation (sub)language

Chapter 8: The Relational Algebra and The Relational Calculus

CS3DB3/SE4DB3/SE6M03 TUTORIAL

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

SQL: A COMMERCIAL DATABASE LANGUAGE. Complex Constraints

SQL: Data Querying. B0B36DBS, BD6B36DBS: Database Systems. h p:// Lecture 4

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Lesson 2. Data Manipulation Language

Relational Algebra Part I. CS 377: Database Systems

Querying Data with Transact SQL

SQL Data Querying and Views

Carnegie Mellon Univ. Dept. of Computer Science Database Applications. General Overview - rel. model. Overview - detailed - SQL

Outline. Note 1. CSIE30600 Database Systems More SQL 2

SQL - Data Query language

Full file at

1Z Oracle Database 11g - SQL Fundamentals I Exam Summary Syllabus Questions

Database Management Systems,

T-SQL Training: T-SQL for SQL Server for Developers

Announcements (September 14) SQL: Part I SQL. Creating and dropping tables. Basic queries: SFW statement. Example: reading a table

Aggregation. Lecture 7 Section Robb T. Koether. Hampden-Sydney College. Wed, Jan 29, 2014

Transcription:

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

2 LECTURE OUTLINE More Complex SQL Retrieval Queries Self-Joins Renaming Attributes and Results Grouping, Aggregation, and Group Filtering Ordering Results Nested SPJ Queries

3 REVIEW OF SPJ QUERIES IN SQL SPJ queries (select-project-join) SQL s basic select-from-where queries Equivalent to using only,, and (or ) in Relational Algebra (and possibly, if attributes need to be renamed before joining)

4 RENAMING IN SQL For convenience, include renaming Like relational algebra operation Aliases (or tuple variables) provide alternative names for tables or columns Customer custid name address phone Sale saleid date custid LineItem saleid product quantity price SELECT name, sale_date, product, quantity AS amount FROM Customer C, Sale AS S(id,sale_date,custid), LineItem WHERE C.custid = S.custid AND id = saleid; Keyword AS is optional

5 SELF-JOINS Renaming is mandatory if table is used more than once in a query EMPLOYEE Fname Minit Lname Ssn Bdate Address Sex Salary Super_ssn Dno Example Give the last names and salaries of employees and their managers whenever the employee earns more than the manager. E Think of the EMPLOYEE table as two tables, one for employees and one for managers. Fname Minit Lname Ssn Bdate Address Sex Salary Super_ssn Dno M Fname Minit Lname Ssn Bdate Address Sex Salary Super_ssn Dno SELECT E.Lname, E.Salary, M.Lname, M.Salary FROM EMPLOYEE E, EMPLOYEE M WHERE E.Super_ssn = M.Ssn and E.Salary > M.Salary;

6 AGGREGATE FUNCTIONS Used to accumulate information from multiple tuples, forming a single-tuple summary Built-in aggregate functions: COUNT, SUM, MAX, MIN, and AVG Used in the SELECT clause Example: What is the average budget of all movies? SELECT AVG(budget) FROM Film; Example How many movies were directed by Steven Spielberg? SELECT COUNT(*) FROM Film WHERE director='steven Spielberg'; All tuples in the result are counted, with duplicates! i.e., COUNT(title) or COUNT(director) give same result COUNT(DISTINCT year) would include each year only once Film title genre year director minutes budget gross

7 AGGREGATE FUNCTIONS EXAMPLE What was the total movie profit since 2010, across how many directors? SELECT SUM(gross - budget), COUNT(DISTINCT director) FROM Film WHERE year >= 2010; Film title genre year director minutes budget gross

8 GROUPING BEFORE AGGREGATION How can we answer a query such as How many films were directed by each director after 2001? Need to produce a result with one tuple per director 1. Partition relation into subsets based on grouping column(s) 2. Apply aggregate function to each such group independently 3. Produce one tuple per group GROUP BY clause to specify grouping attributes SELECT director, COUNT(*) FROM Film WHERE year > 2001 GROUP BY director; Important: Every element in SELECT clause must be a grouping column or an aggregation function e.g., SELECT director, year, COUNT(*) would not be allowed (in the query above) unless also grouping by year i.e., GROUP BY director, year

10 HAVING CLAUSE After partitioning into groups, whole partitions can be discarded i.e., HAVING clause specifies a condition on the grouped tuples HAVING clause cannot reference individual tuples within a group Instead, it can reference grouping column(s) and aggregates only Contrast WHERE clause to HAVING clause Note: As for aggregation, no GROUP BY clause means relation treated as one group

11 ORDERING OF QUERY RESULTS Final output of a query can be sorted by one or more column values Use ORDER BY clause Keyword DESC for descending order of values Optionally use keyword ASC for ascending order (default) Example SELECT dept, term, COUNT(DISTINCT instructor) AS num_instructors FROM Course GROUP BY dept, term ORDER BY dept, term DESC; Course dept cnum instructor term Note that the result is sorted ascending by department Within each department, terms are sorted in descending order What if DISTINCT omitted? What if term omitted from SELECT clause? What if dept omitted from GROUP BY clause? What if dept omitted from ORDER BY clause?

12 SUMMARY OF SQL QUERIES 1. Assemble all tables according to From clause (, means to use ) 2. Keep only tuples matching Where clause 3. Group into blocks based on Group By clause 4. Keep only blocks matching Having clause 5. Create one tuple for each block using Select clause 6. Order resulting tuples according to Order By clause

13 NESTED QUERIES Any table can be used in FROM clause select-from-where produces a table Thus, we may nest one query within another Example: Give the biographical information for directors of profitable movies. Film title genre year director minutes budget gross Person name birth city SELECT name, birth, city FROM ( SELECT director FROM Film WHERE gross > budget) AS Profitable, Person WHERE director = name;

15 NESTED QUERIES (CONT D.) Any column can be used in SELECT and WHERE clauses But refers to only one tuple value at a time select-from-where can produce a one-column table that contains only one tuple Thus, queries may also be nested in SELECT and WHERE clauses Example: Which film(s) had the highest budget? SELECT * FROM Film WHERE budget = ( SELECT MAX(budget) FROM Film);

16 USING IN FOR MEMBERSHIP TEST Comparison operator IN Compares value v with a set (or bag) of values V Evaluates to TRUE if v is one of the elements in V Allows any relation in WHERE clause We may omit DISTINCT from this solution. Why?

17 USING IN (CONT D.) Use tuples of values in comparisons Requires parentheses

18 NESTED 1-COLUMN QUERIES Use other comparison operators to compare a single value v = ANY operator (or = SOME) Returns TRUE if the value v is equal to some value in the set V Equivalent to IN Also available for >, >=, <, <=, and <> >= ALL operator Returns TRUE if value v is greater than or equal to every value in V Equivalent to =(SELECT MAX( ) ) Also available for =, >, <, <=, and <>

19 CORRELATED NESTED QUERIES Correlated nested query Inner query refers to a table from the outer query Inner query evaluated once for each tuple in the outer query Such queries are easiest to understand (and write correctly) if all column names are qualified by their relation names Note, inner query can refer to E, but outer query cannot refer to D

20 EXISTS AND UNIQUE FUNCTIONS [NOT] EXISTS function Check whether result of nested query is empty or not EXISTS equivalent to (SELECT COUNT(*) ) <> 0 Customer custid name address phone Sale saleid date custid SELECT name, phone FROM Customer C WHERE NOT EXISTS ( SELECT * FROM Sale S WHERE C.custid = S.custid); Note that columns selected in inner query are irrelevant SQL function UNIQUE(Q) Returns TRUE if no duplicate tuples in result of query Q

22 LECTURE SUMMARY Complex SQL: Self joins Aggregate functions Grouping Sorting Nested queries Relational algebra expressions can handle self joins and nested queries with no additional operators Grouping, aggregations, and sorting require additional operators