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



Similar documents
Information Systems SQL. Nikolaj Popov

Introduction to Microsoft Jet SQL

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

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

Introduction to SQL: Data Retrieving

System R: Relational Approach to Database Management

Structured Query Language (SQL)

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

Aggregating Data Using Group Functions

Overview of Database Management Systems

RDBMS Using Oracle. Lecture Week 7 Introduction to Oracle 9i SQL Last Lecture. kamran.munir@gmail.com. Joining Tables

Database Design. Marta Jakubowska-Sobczak IT/ADC based on slides prepared by Paula Figueiredo, IT/DB

Mini User's Guide for SQL*Plus T. J. Teorey

CS 338 Join, Aggregate and Group SQL Queries

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

Figure 4.12.Insurancedatabase.

3. Relational Model and Relational Algebra

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

Part 4: Database Language - SQL

Access Path Selection in a Relational Database Management System

The Relational Algebra

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

Subqueries Chapter 6

Oracle/SQL Tutorial 1

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

Oracle SQL. Course Summary. Duration. Objectives

Summary on Chapter 4 Basic SQL

SQL Introduction Chapter 7, sections 1 & 4. Introduction to SQL. Introduction to SQL. Introduction to SQL

Oracle Database 10g: Introduction to SQL

MOC 20461C: Querying Microsoft SQL Server. Course Overview

Elena Baralis, Silvia Chiusano Politecnico di Torino. Pag. 1. Physical Design. Phases of database design. Physical design: Inputs.

IT2305 Database Systems I (Compulsory)

Relational Database: Additional Operations on Relations; SQL

- Eliminating redundant data - Ensuring data dependencies makes sense. ie:- data is stored logically

Oracle 10g PL/SQL Training

Oracle Database: SQL and PL/SQL Fundamentals

Introduction to tuple calculus Tore Risch

Elena Baralis, Silvia Chiusano Politecnico di Torino. Pag. 1. Query optimization. DBMS Architecture. Query optimizer. Query optimizer.

Oracle Database 11g SQL

University of Massachusetts Amherst Department of Computer Science Prof. Yanlei Diao


Chapter 1. Writing Basic. SQL Statements

SQL Nested & Complex Queries. CS 377: Database Systems

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

IT2304: Database Systems 1 (DBS 1)

Database Query 1: SQL Basics

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

Objectives of SQL. Terminology for Relational Model. Introduction to SQL

Oracle Database: SQL and PL/SQL Fundamentals NEW

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


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

Chapter 1 Overview of the SQL Procedure

TYPICAL QUESTIONS & ANSWERS

Oracle Database: SQL and PL/SQL Fundamentals

CSC 443 Data Base Management Systems. Basic SQL

Displaying Data from Multiple Tables. Chapter 4

Relational Databases

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

SQL (structured query language)

VARRAY AND NESTED TABLE

VIEWS virtual relation data duplication consistency problems

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

Instant SQL Programming

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING DR.NAVALAR NEDUNCHEZHIAYN COLLEGE OF ENGINEERING, THOLUDUR , CUDDALORE DIST.

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

Scheme G. Sample Test Paper-I

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

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

Oracle Database 12c: Introduction to SQL Ed 1.1

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

From Databases to Natural Language: The Unusual Direction

Data Models and Database Management Systems (DBMSs) Dr. Philip Cannata

Producing Readable Output with SQL*Plus

Querying Microsoft SQL Server

10CS54: DATABASE MANAGEMENT SYSTEM

Querying Microsoft SQL Server (20461) H8N61S

Using SQL in RPG Programs: An Introduction

Displaying Data from Multiple Tables

Course ID#: W 35 Hrs. Course Content

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

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

Querying Microsoft SQL Server 20461C; 5 days

Performing Queries Using PROC SQL (1)

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

1. INTRODUCTION TO RDBMS

Chapter 5. SQL: Queries, Constraints, Triggers

Elena Baralis, Silvia Chiusano Politecnico di Torino. Pag. 1. Active database systems. Triggers. Triggers. Active database systems.

Access Path Selection in a Relational Database Management System. P. Griffiths Selinger M. M. Astrahan D. D. Chamberlin,: It. A. Lorie.

Lecture 6. SQL, Logical DB Design

14 Triggers / Embedded SQL

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

1 Structured Query Language: Again. 2 Joining Tables

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

SQL, PL/SQL FALL Semester 2013

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

Oracle Database: SQL and PL/SQL Fundamentals NEW

Appendix A Practices and Solutions

Review of Business Information Systems Third Quarter 2013 Volume 17, Number 3

D B M G Data Base and Data Mining Group of Politecnico di Torino

Transcription:

Query Languages Review Relational Algebra SQL Set operators Union Intersection Difference Cartesian product Relational Algebra Operators Relational operators Selection Projection Join Division Douglas S. Kerr CIS 671 Query Languages 1 CIS 671 Query Languages 2 Select (σ), Project (π), Union ( ), Difference (-), Join: Natural (*) and Theta ( ) Natural join (*) Q R 1 * (list1), (list2) R 2 list i has attributes from relation R i list 1 names go in Q Theta-join (θ-join) θ ε {, <, =, >,, } Theta (θ) join ( ) Q R 1 <join condition> R 2 where <join condition> is of the form <condition> AND <condition> AND AND <condition> where each condition is of the form A i θ B j, A i is an attribute of R 1, B j is an attribute of R 2, θ (theta) ε (<,, >,, =, ) e.g. For each employee, those making higher salary. EMP1 EMP1.SAL < EMP2.SAL EMP2 CIS 671 Query Languages 3 EMP( EMPNO, NAME, DNO, JOB, MGR, SAL, COMMISSION) DEPT( DNO, DNAME, LOC) CIS 671 Query Languages 4 Answer: D1 and D2 (exists, ) Answer: D1 and D2 π DNO ( π JOB (σ DNO = D3 (EMP)) * EMP) What about department D3? CIS 671 Query Languages 5 CIS 671 Query Languages 6 1

CIS 671 Query Languages 7 Divide ( ), for all ( ) EMP( EMPNO, DNO, JOB,...) D3JOBS π JOB (σ DNO = D3 (EMP)) DEPT_JOBS π DNO, JOB (EMP) GOOD_DEPTS DEPT_JOBS D3JOBS (π DNO, JOB (EMP)) (π JOB (σ DNO = D3 (EMP)) ) CIS 671 Query Languages 8 Functions and groups continued Q14. List the departments (DNO) and the average salary of each. EMP( EMPNO, DNO, SAL,...) 100 D3 66,000 200 D3 55,000 300 D3 66,000 400 D1 66,000 500 D1 55,000 600 D1 60,000 700 D2 66,000 800 D2 60,000 900 D2 66,000 DNO DNO AVG SAL D3 62,333 D1 60,333 D2 64,000 AVERAGE SAL ( EMP) CIS 671 Query Languages 9 Outer joins P(P#, PNAME, CITY) S(S#, SNAME, CITY) P1 Nut London S1 Smith London P3 Screw Rome S5 Adams Athens Q15a. For each part list all suppliers in the same city. ( P#, PNAME, S#, SNAME, CITY) P1 Nut S1 Smith London Q15b. For each part list all suppliers in the same city. For a part with no supplier in the city, list null. P P.CITY = S.CITY S natural join (inner join) ( P#, PNAME, S#, SNAME, CITY) P P.CITY = S.CITY S P1 Nut S1 Smith London P3 Screw?? Rome left outer join CIS 671 Query Languages 10 Outer joins continued P(P#, PNAME, CITY) P1 Nut London P3 Screw Rome Q15c. For each part list all suppliers in the same city. For a supplier with no part in the city, list null. ( P#, PNAME, S#, SNAME, CITY) P P.CITY = S.CITY P1 Nut S1 Smith London S?? S5 Adams Athens right outer join Q15d. For each part list all suppliers in the same city. For a part with no supplier in the city, list null. For a supplier with no part in the city, list null. S(S#, SNAME, CITY) S1 Smith London S5 Adams Athens ( P#, PNAME, S#, SNAME, CITY) P1 Nut S1 Smith London P P.CITY = S.CITY S P3 Screw?? Rome?? S5 Adams Athens outer join CIS 671 Query Languages 11 Recursive closure EMP( EMPNO, MGR,...) 100 700 200 700 300 700 400 800 500 800 600 800 700 900 800 900 900 900 Q 16. List all the superiors of EMPNO 100. 700 900 Q 17. List all those supervised by EMPNO 800. 600 Can t express these queries? WHY? CIS 671 Query Languages 12 2

SEQUEL Structured English QUEry Language D. D. Chamberlin, et al., SEQUEL 2 A Unified Approach to Data Definition, Manipulation, and Control, IBM J. Res. Develop., Nov. 1976, pp. 560-575. SQL Structured Query Language SQL - Parts of the Language Data Definition Language (DDL) create table create index Data Manipulation Language (DML) select (retrieve) update insert delete CIS 671 Query Languages 13 CIS 671 Query Languages 14 Select - Basic Form (select from where) Cartesian product followed by select and project. select project-list from Cartesian-product-list where select-condition(s) Abstract example: Given tables R(A,B) and S(B,C) select R.A, R.B, S.C from R, S where R.A > 10 BUT - Duplicates NOT eliminated. Bag vs. Set. CIS 671 Query Languages 15 Select as a JOIN Cartesian product followed by select ( join & select conditions) and project. select project-list from Cartesian-product-list where join-condition and select-condition Abstract example: Given tables R(A,B) and S(B,C) select R.A, R.B, S.C from R, S where R.B = S.B /* join condition */ and R.A > 10 /* select condition */ CIS 671 Query Languages 16 Using EMP and DEPT From Relational Algebra to SQL EMP( EMPNO, NAME, DNO, JOB, MGR, SAL, COMMISSION) DEPT( DNO, DNAME, LOC) List the names, employee numbers, department numbers and locations for all clerks. select NAME, EMPNO, E.DNO, LOC E, DEPT D where E.DNO = D.DNO /* join condition */ and JOB = Clerk /* select condition */ Note use of alias in from clause. CIS 671 Query Languages 17 Duplicates in project - must use explicit distinct List the different department numbers in the EMP table (eliminate duplicates). select distinct DNO Specify sort order List employee number, name, and salary of employees in department 50. select EMPNO, NAME, SAL where DNO = 50 order by EMPNO CIS 671 Query Languages 18 3

Union List the numbers of those departments which have an employee named Smith or are located in Columbus. where ENAME = Smith union where LOC = Columbus Functions and Groups List the departments (DNO) and the average salary of each., avg(sal) E, DEPT D where E.DNO = D.DNO group by DNO Duplicates ARE eliminated by default. union all - leaves duplicates CIS 671 Query Languages 19 CIS 671 Query Languages 20 SQL is (was) NOT an Algebra List the departments (DNO, DNAME) in which the average employee salary < $25,000., DNAME E, DEPT D where E.DNO = D.DNO group by DNO, DNAME having avg(sal) < 25000 CIS 671 Query Languages 21 Nested Select: No analog in Relational Algebra List names of employees in departments 25, 47 and 53. select NAME where DNO in (25, 47, 53) List names of employees who work in departments in Ann Arbor. select NAME where DNO in ( where LOC = Ann Arbor ) CIS 671 Query Languages 22 Big Summary For all departments in Columbus with average salary > $25,000, list the department s number, name, and average salary ordered by average salary in descending order., DNAME, avg(sal), DEPT where EMP.DNO = DEPT.DNO and LOC = Columbus group by DNO, DNAME having avg(sal) > 25000 order by 3 desc CIS 671 Query Languages 23 Null Values All of the following conditions are always false. null > 25 null < 25 null = 25 null <> 25 null >= 25 null <= 25 null = null null <> null However we can use the following: select NAME where SAL < 35000 or SAL is null CIS 671 Query Languages 24 4

Views In SQL A named, derived table (a virtual table). Derived from base tables and/or other views. In Three-schema Architecture (pp. 27-29) External View A collection of several tables, some views, other base tables. Views: Changing Attribute Names Create a view called PROGS consisting of the EMPNO, name and salary of all programmers. Include the locations of their departments. create view PROG(EMPNO, NAME, SALARY, HOMEBASE) as select EMPNO, NAME, SAL, LOC, DEPT where EMP.DNO = DEPT.DNO and EMP.JOB = Programmer CIS 671 Query Languages 25 CIS 671 Query Languages 26 Exists ( ) and For All ( ) EMP( EMPNO, NAME, DNO, JOB, MGR, SAL, COMMISSION) DEPT( DNO, DNAME, LOC) CIS 671 Query Languages 27 Answer: D1 and D2 exists, The order of the two where exists selects does not matter. and exists EY Still gets D3. and EY.DNO = DEPT.DNO)) CIS 671 Query Languages 28 for all, However no for all exists in SQL. where for all and exists EY and EY.DNO = DEPT.DNO)) CIS 671 Query Languages 29 CIS 671 Query Languages 30 5

However no for all exists in SQL. where not exists Use two not exists. and not exists EY and EY.DNO = DEPT.DNO)) Eliminate where not exists and not exists EY and EY.DNO = DEPT.DNO) ) and DNO <> D3 CIS 671 Query Languages 31 CIS 671 Query Languages 32 SQL: for all ( ) using count Function First Attempt: SQL: for all ( ) using count Function Second Attempt: D where ( select count(distinct JOB) ) = ( select count(distinct JOB) EY where EY.DNO = D.DNO) D3JOBS: Jobs done DJOBS: Jobs done department D. D where ( select count(distinct JOB) ) = ( select count(distinct EY.JOB) EY, EMP ED3 where EY.DNO = D.DNO and EY.JOB = ED3.JOB and ED3.DNO = D3 ) D3JOBS: Jobs done DJOBS: Jobs done by employees in department D that are also done by employees in department D3. CIS 671 Query Languages 33 CIS 671 Query Languages 34 Why does this approach work? The where clause is applied to a specific department D. D where ( select count(distinct JOB) ) = ( select count(distinct EY.JOB) EY, EMP ED3 where EY.DNO = D.DNO and EY.JOB = ED3.JOB and ED3.DNO = D3 ) Works if DJOBS D3JOBS. Why is this a general rule? Why is the rule satisfied in this case? D3JOBS: Jobs done DJOBS: Jobs done department D that are also done by employees in CIS 671 Query Languages 35 6