D B M G. SQL language: basics. Set operators. The UNION operator The INTERSECT operator The EXCEPT operator. SQL language: set operators

Similar documents
Overview of Database Management Systems

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

CSE132A Solutions HW 1

Introduction to Microsoft Jet SQL

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

Information Systems SQL. Nikolaj Popov

SQL: QUERIES, CONSTRAINTS, TRIGGERS

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

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

LABSHEET 1: creating a table, primary keys and data types

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

Figure 4.12.Insurancedatabase.

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

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

Normalisation. Why normalise? To improve (simplify) database design in order to. Avoid update problems Avoid redundancy Simplify update operations

SQL: Queries, Programming, Triggers

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

SQL: Queries, Programming, Triggers

3. Relational Model and Relational Algebra

Relational Algebra and SQL

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

Chapter 5. SQL: Queries, Constraints, Triggers

Database Management Systems. Chapter 1

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

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

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

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

Lecture 6. SQL, Logical DB Design

Chapter 9: Normalization

Introduction to SQL: Data Retrieving

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

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

Proposed solutions Project Assignment #1 (SQL)

Advance DBMS. Structured Query Language (SQL)

Lecture 4: More SQL and Relational Algebra

6 QUERY-BY-EXAMPLE (QBE)

Green = 0,255,0 (Target Color for E.L. Gray Construction) CIELAB RGB Simulation Result for E.L. Gray Match (43,215,35) Equal Luminance Gray for Green

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

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

Database Sample Examination

CGS X X X X X

Querying Microsoft SQL Server 2012

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

Databases and BigData

The Relational Model. Ramakrishnan&Gehrke, Chapter 3 CS4320 1

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

UNIFORM AND DRESS CODE COMPLIANCE

Graphing Equations. with Color Activity

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

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

The Relational Model. Why Study the Relational Model? Relational Database: Definitions. Chapter 3

Set operations and Venn Diagrams. COPYRIGHT 2006 by LAVON B. PAGE

SQL Nested & Complex Queries. CS 377: Database Systems

Oracle SQL. Course Summary. Duration. Objectives

The Relational Model. Why Study the Relational Model?

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

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

Θεµελίωση Βάσεων εδοµένων

The SQL Query Language. Creating Relations in SQL. Referential Integrity in SQL. Basic SQL Query. Primary and Candidate Keys in SQL

Summary on Chapter 4 Basic SQL

Comp 3311 Database Management Systems. 2. Relational Model Exercises

Exercise 1: Relational Model


Database Access from a Programming Language: Database Access from a Programming Language

Database Access from a Programming Language:

CSC 443 Data Base Management Systems. Basic SQL

Useful Business Analytics SQL operators and more Ajaykumar Gupte IBM

1. Write the query of Exercise 6.19 using TRC and DRC: Find the names of all brokers who have made money in all accounts assigned to them.

Oracle Database 12c: Introduction to SQL Ed 1.1

The Relational Algebra

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

SQL Simple Queries. Chapter 3.1 V3.0. Napier University Dr Gordon Russell

Relational Databases

MOC 20461C: Querying Microsoft SQL Server. Course Overview

1 Structured Query Language: Again. 2 Joining Tables

LiTH, Tekniska högskolan vid Linköpings universitet 1(7) IDA, Institutionen för datavetenskap Juha Takkinen

Effective Use of SQL in SAS Programming

How To Improve Performance In A Database

Introduction to database design

Oracle Database 10g: Introduction to SQL

Oracle Database 11g SQL

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

Data Warehousing and Decision Support. Introduction. Three Complementary Trends. Chapter 23, Part A

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

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

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

There are five fields or columns, with names and types as shown above.

DATA CUBES E Jayant Haritsa Computer Science and Automation Indian Institute of Science. JAN 2014 Slide 1 DATA CUBES

SQL (structured query language)

Pantone Matching System Color Chart PMS Colors Used For Printing

Introduction to SQL ( )

Oracle Database: SQL and PL/SQL Fundamentals

Quiz 2: Database Systems I Instructor: Hassan Khosravi Spring 2012 CMPT 354

Chapter 13: Query Optimization

How To Color Print

The World. Engineering And Robotics Learned Young. EARLY Robotics Competition. Mission Field Details

1.204 Lecture 3. SQL: Basics, Joins SQL

MIDLAND INDEPENDENT SCHOOL DISTRICT UNIFORM REQUIREMENTS

SECURE UNIVERSES USING RESTRICTION SETS

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

Transcription:

SQL language: basics Set operators Set operators The UNION operator The INTERSECT operator The EXCEPT operator 2013 Politecnico di Torino 1

Set operators The UNION operator The UNION operator Set union operator A UNION B It performs the union of the two relational expressions A and B relational expressions A and B may be generated by SELECT statements it requires schema compatibility between A and B removal of duplicates UNION removes duplicates UNION ALL does not remove duplicates 2013 Politecnico di Torino 2

P UNION: example Find the codes of products that are either red or supplied by supplier S2 (or both) PName Color Size Jumper Red 40 P2 Jeans Green 48 P3 Blouse Blue 48 Rome P4 Blouse Red 44 P5 Skirt Blue 40 P6 Shorts Red 42 SP SId Qty S1 300 S1 P2 200 S1 P3 400 S1 P4 200 S1 P5 100 S1 P6 100 S2 300 S2 P2 400 S3 P2 200 S4 P3 200 S4 P4 300 S4 P5 400 UNION: example Find the codes of products that are either red or supplied by supplier S2 (or both) WHERE Color= Red P PName Color Size Jumper Red 40 P2 Jeans Green 48 P3 Blouse Blue 48 Rome P4 Blouse Red 44 P5 Skirt Blue 40 P6 Shorts Red 42 P6 2013 Politecnico di Torino 3

UNION: example Find the codes of the products that are either red or supplied by supplier S2 (or both) SP SId Qty S1 300 S1 P2 200 S1 P3 400 S1 P4 200 S1 P5 100 S1 P6 100 S2 300 S2 P2 400 S3 P2 200 S4 P3 200 S4 P4 300 S4 P5 400 FROM SP WHERE SId= S2 P2 UNION: example Find the codes of products that are either red or supplied by supplier S2 (or both) WHERE Color= Red UNION FROM SP WHERE SId= S2 ; P6 P2 R P2 P6 2013 Politecnico di Torino 4

UNION: example Find the codes of products that are either red or supplied by supplier S2 (or both) WHERE Color= Red UNION FROM SP WHERE SId= S2 ; Duplicate removal R P2 P6 UNION: example Find the codes of products that are either red or supplied by supplier S2 (or both) Schema compatibility WHERE Color= Red UNION FROM SP WHERE SId= S2 ; 2013 Politecnico di Torino 5

UNION ALL: example Find the codes of products that are either red or supplied by supplier S2 (or both) WHERE Color= Red UNION ALL FROM SP WHERE SId= S2 ; P6 P2 R P2 P6 Set operators The INTERSECT operator 2013 Politecnico di Torino 6

The INTERSECT operator Set intersection operator A INTERSECT B It performs the intersection of the two relational expressions A and B relational expressions A and B may be generated by SELECT statements it requires schema compatibility between A and B INTERSECT: example Find the cities where both one or more suppliers and one or more stores are based P S PName Color Size Jumper Red 40 P2 Jeans Green 48 P3 Blouse Blue 48 Rome P4 Blouse Red 44 P5 Skirt Blue 40 P6 Shorts Red 42 SId SName #Employees City S1 Smith 20 S2 Jones 10 S3 Blake 30 S4 Clark 20 S5 Adams 30 Athens 2013 Politecnico di Torino 7

INTERSECT: example Find the cities where both one or more suppliers and one or more stores are based SELECT City FROM S S SId SName #Employees City S1 Smith 20 S2 Jones 10 S3 Blake 30 S4 Clark 20 S5 Adams 30 Athens City Athens INTERSECT: example Find the cities where both one or more suppliers and one or more stores are based SELECT P PName Color Size Jumper Red 40 P2 Jeans Green 48 P3 Blouse Blue 48 Rome P4 Blouse Red 44 P5 Skirt Blue 40 P6 Shorts Red 42 Rome 2013 Politecnico di Torino 8

INTERSECT: example Find the cities where both one or more suppliers and one or more stores are based SELECT City FROM S INTERSECT SELECT ; City Athens Rome R Equivalence with other operators The intersection operation may also be performed by means of a join the IN operator 2013 Politecnico di Torino 9

Equivalence with join The FROM clause contains the relations involved in the intersection The WHERE clause contains join conditions between the attributes listed in the SELECT clauses of relational expressions A and B Equivalence with join: example Find the cities where both one or more suppliers and one or more stores are based SELECT City FROM S, P WHERE S.City=P.; 2013 Politecnico di Torino 10

Equivalence with the IN operator One of the two relational expressions is turned into a nested query using operator IN The attributes in the outer SELECT clause, joined together by a tuple constructor, make up the lefthand side of the IN operator Equivalence with IN: example Find the cities where both one or more suppliers and one or more stores are based SELECT WHERE IN (SELECT City FROM S); 2013 Politecnico di Torino 11

Set operators The EXCEPT operator The EXCEPT operator Set difference operator A EXCEPT B It subtracts relational expression B from relational expression A it requires schema compatibility between A and B 2013 Politecnico di Torino 12

EXCEPT: example Find the cities where one or more suppliers, but no stores are based P S PName Color Size Jumper Red 40 P2 Jeans Green 48 P3 Blouse Blue 48 Rome P4 Blouse Red 44 P5 Skirt Blue 40 P6 Shorts Red 42 SId SName #Employees City S1 Smith 20 S2 Jones 10 S3 Blake 30 S4 Clark 20 S5 Adams 30 Athens EXCEPT: example Find the cities where one or more suppliers, but no stores are based SELECT City FROM S S SId SName #Employees City S1 Smith 20 S2 Jones 10 S3 Blake 30 S4 Clark 20 S5 Adams 30 Athens City Athens 2013 Politecnico di Torino 13

EXCEPT: example Find the cities where one or more suppliers, but no stores are based SELECT P PName Color Size Jumper Red 40 P2 Jeans Green 48 P3 Blouse Blue 48 Rome P4 Blouse Red 44 P5 Skirt Blue 40 P6 Shorts Red 42 Rome EXCEPT: example Find the cities where one or more suppliers, but no stores are based SELECT City FROM S EXCEPT SELECT ; City Athens Rome R Athens 2013 Politecnico di Torino 14

Equivalence with the NOT IN operator The difference operation may also be performed by means of the NOT IN operator relational expression B is nested within the NOT IN operator the attributes in the SELECT clause of relational expression A, joined together by a tuple constructor, make up the left-hand side of the NOT IN operator Equivalence with the NOT IN operator: example Find the cities where one or more suppliers, but no stores are based SELECT City FROM S WHERE City NOT IN (SELECT ); 2013 Politecnico di Torino 15