Relational Division and SQL



Similar documents
Chapter 9 Joining Data from Multiple Tables. Oracle 10g: SQL

Unit 3. Retrieving Data from Multiple Tables

Relational Database: Additional Operations on Relations; SQL

SQL Query Evaluation. Winter Lecture 23

Introduction to tuple calculus Tore Risch

SQL: Queries, Programming, Triggers

Relational Databases

Advance DBMS. Structured Query Language (SQL)

Advanced SQL - Subqueries and Complex Joins

Querying Microsoft SQL Server 2012

SQL: Queries, Programming, Triggers

Define terms Write single and multiple table SQL queries Write noncorrelated and correlated subqueries Define and use three types of joins

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

Chapter 5. SQL: Queries, Constraints, Triggers

Introducing Microsoft SQL Server 2012 Getting Started with SQL Server Management Studio

Introduction to Querying & Reporting with SQL Server

Chapter 13: Query Optimization

Information Systems SQL. Nikolaj Popov

WRITING EFFICIENT SQL. By Selene Bainum

Using SQL Queries to Insert, Update, Delete, and View Data: Joining Multiple Tables. Lesson C Objectives. Joining Multiple Tables

Oracle Database 12c: Introduction to SQL Ed 1.1

Querying Microsoft SQL Server 2012

Introduction to Microsoft Jet SQL

Chapter 14: Query Optimization

Course 10774A: Querying Microsoft SQL Server 2012 Length: 5 Days Published: May 25, 2012 Language(s): English Audience(s): IT Professionals

MOC 20461C: Querying Microsoft SQL Server. Course Overview

Course 10774A: Querying Microsoft SQL Server 2012

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

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

SQL SELECT Query: Intermediate

Evaluation of Expressions

Examples of DIVISION RELATIONAL ALGEBRA and SQL. r s is used when we wish to express queries with all :

Course 20461C: Querying Microsoft SQL Server Duration: 35 hours

3. Relational Model and Relational Algebra

Module 1: Getting Started with Databases and Transact-SQL in SQL Server 2008

Saskatoon Business College Corporate Training Centre

Relational Algebra. Module 3, Lecture 1. Database Management Systems, R. Ramakrishnan 1

SQLMutation: A tool to generate mutants of SQL database queries

Writing Queries Using Microsoft SQL Server 2008 Transact-SQL

The Relational Algebra

Object Oriented Databases (OODBs) Relational and OO data models. Advantages and Disadvantages of OO as compared with relational

Oracle SQL. Course Summary. Duration. Objectives

Oracle Database 10g: Introduction to SQL

Querying Microsoft SQL Server

Querying Microsoft SQL Server (20461) H8N61S

Writing Queries Using Microsoft SQL Server 2008 Transact-SQL

Database Design and Database Programming with SQL - 5 Day In Class Event Day 1 Activity Start Time Length

SECURE UNIVERSES USING RESTRICTION SETS

Lecture Slides 4. SQL Summary. presented by Timothy Heron. Indexes. Creating an index. Mathematical functions, for single values and groups of values.

Comp 5311 Database Management Systems. 3. Structured Query Language 1

Relational Algebra Programming With Microsoft Access Databases

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

SQL Server for developers. murach's TRAINING & REFERENCE. Bryan Syverson. Mike Murach & Associates, Inc. Joel Murach

Course ID#: W 35 Hrs. Course Content

Simple SQL Queries (3)

Topics. Database Essential Concepts. What s s a Good Database System? Using Database Software. Using Database Software. Types of Database Programs

Querying Microsoft SQL Server 20461C; 5 days

ICAB4136B Use structured query language to create database structures and manipulate data

Join Example. Join Example Cart Prod Comprehensive Consulting Solutions, Inc.All rights reserved.

Schema Mappings and Data Exchange

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

Database Management Systems Comparative Study: Performances of Microsoft SQL Server Versus Oracle

Querying Microsoft SQL Server Course M Day(s) 30:00 Hours

Consulting. Personal Attention, Expert Assistance

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

Advanced SQL Queries for the EMF

Relational Algebra and SQL

Recognizing and resolving Chasm and Fan traps when designing universes

Relational Algebra. Basic Operations Algebra of Bags

Preparing Data Sets for the Data Mining Analysis using the Most Efficient Horizontal Aggregation Method in SQL

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

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

Useful Business Analytics SQL operators and more Ajaykumar Gupte IBM

Netezza SQL Class Outline

Five Little Known, But Highly Valuable, PROC SQL Programming Techniques. a presentation by Kirk Paul Lafler

Microsoft Access Rollup Procedure for Microsoft Office Click on Blank Database and name it something appropriate.

Advanced Query for Query Developers

Querying Microsoft SQL Server 2012

MapReduce examples. CSE 344 section 8 worksheet. May 19, 2011

MOC QUERYING MICROSOFT SQL SERVER

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

Databases in Engineering / Lab-1 (MS-Access/SQL)

DBMS / Business Intelligence, SQL Server

Optimizing Your Database Performance the Easy Way

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

Structured Query Language (SQL)

Oracle Database: SQL and PL/SQL Fundamentals

Execution Strategies for SQL Subqueries

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

MTCache: Mid-Tier Database Caching for SQL Server

The Query Builder: The Swiss Army Knife of SAS Enterprise Guide

BRIO QUERY FUNCTIONALITY IN COMPARISION TO CRYSTAL REPORTS

Introduction. Part I: Finding Bottlenecks when Something s Wrong. Chapter 1: Performance Tuning 3

SQL Server Query Tuning

Oracle Database: SQL and PL/SQL Fundamentals NEW

The Basics of Querying in FoxPro with SQL SELECT. (Lesson I Single Table Queries)

Lecture 4: SQL Joins. Morgan C. Wang Department of Statistics University of Central Florida

Transcription:

Relational Division and SQL Soulé 1 Example Relations and Queries As a motivating example, consider the following two relations: Taken(,Course) which contains the courses that each student has completed, and Required(Course), which contains the courses that are required to graduate. The instances for this example are shown below: Taken Course Databases Programming Languages Susie Databases Susie Operating Systems Julie Programming Languages Julie Machine Learning Operating Systems Required Course Databases Programming Languages Suppose we are asked the following two queries: 1. Find all students who have taken a required course. 2. Find all students who can graduate (i.e., who have taken all required courses). 2 Asking About Some We have already seen how to do Query 1 using the standard SELECT... FROM... WHERE clauses. To remove duplicates from our result, we can use 1

the SQL keyword DISTINCT. In terms of relational algebra, we use a selection (σ), to filter rows with the appropriate predicate, and a projection (π) to get the desired columns. SELECT DISTINCT WHERE Course = Databases or Course = Programming Languages ; If we want to be slightly more general, we can use a sub-query: SELECT DISTINCT WHERE Course IN (SELECT Course FROM Required); Informally, we might read this query as give me the set of students who have taken a course that appears in the set of required courses, or the set of students whose courses contain at least one course that is required. 3 Asking About All Query 2 is more difficult. Just by looking at this small instance, it is easy to see that the answer we want is: CanGraduate There is a relational operator that directly gives us this result. The operator is division, written R S. Unfortunately, there is no direct way to express division in SQL. We can write this query, but to do so, we will have to express our query through double negation and existential quantifiers. We will produce this query in stages. Our roadmap is as follows 1. Find all students 2. Find all students and the courses required to graduate 3. Find all students and the required courses that they have not taken 4. Find all students who can not graduate 5. Find all students who can graduate 2

All s First, we create a set of all student that have taken courses. We can express this positively using a selection and projection: CREATE TEMP TABLE All as SELECT ; All Susie Julie All s and Required Classes Next, we will create a set of students and the courses they need to graduate. We can express this as a Cartesian product, creating the pairs of the form (student,course): CREATE TABLE sandrequired AS SELECT DISTINCT All.student, Required.course FROM All, Required ; sandrequired Course Databases Programming Languages Susie Databases Susie Programming Languages Julie Databases Julie Programming Languages Databases Programming Languages All s and Required Classes Not Taken This is where our query starts to get tricky. We want to find the subset of the relation we just produced that includes the students and the required courses that they have not taken. We are doing this as a first step towards finding the students who cannot graduate. The intuition is that we want to find all (student,course) pairs that are in the relation sandrequired, but not in the relation Taken. This should give us the set of students who cannot graduate, with the courses that they still need to take: 3

CREATE TEMP TABLE sandrequirednottaken AS SELECT * FROM sandrequired WHERE NOT EXISTS (SELECT * WHERE sandrequired.student = Taken.student AND sandrequired.course = Taken.Course); sandrequirednottaken Course Susie Programming Languages Julie Databases Databases Programming Languages s Who Can Not Graduate From the previous relation, we can apply a projection to get the set of students who cannot graduate. CREATE TEMP TABLE CannotGraduate AS SELECT FROM sandrequirednottaken; CannotGraduate Susie Julie s Who Can Graduate This is the second tricky part of our query. We find the subset of students who can graduate by looking at the students in Alls who are not in the set of CannotGraduate. Put another way, the set of all students except the students who cannot graduate: CREATE TEMP TABLE CanGraduate AS SELECT * FROM Alls WHERE NOT EXISTS (SELECT * FROM CannotGraduate WHERE CannotGraduate. = Alls.); CanGraduate 4

Re-write As a Single Query We can re-write the above set of queries into a single query that does not use the temporary tables: SELECT DISTINCT x. FROM taken AS x WHERE NOT EXISTS ( SELECT * FROM required AS y WHERE NOT EXISTS ( SELECT * FROM taken AS z WHERE (z.=x.) AND (z.course=y.course))); Re-write Using Except (or Minus) EXCEPT instead of NOT EXISTS: We can re-write the above to use SELECT EXCEPT SELECT FROM ( SELECT,Course FROM (select ), Required EXCEPT SELECT,Course ); Re-write Using Aggregations We can write the query in an (arguably) simpler version, using set membership (i.e., IN), GROUP BY, COUNT aggregations, and HAVING: SELECT From Taken WHERE Course IN (SELECT Course FROM Required) GROUP BY HAVING COUNT(*) = (SELECT COUNT(*) FROM Required); 5