SQL - miscellaneous D. Christozov / G.Tuparov INF 280 Database Systems: SQL – miscellaneous 1.

Slides:



Advertisements
Similar presentations
DB glossary (focus on typical SQL RDBMS, not XQuery or SPARQL)
Advertisements

COP4540 Database Management System Midterm Review
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
Complex Integrity Constraints in SQL. Constraints over a Single Table Table Constraint: Create TABLE Sailors (sid INTEGER, sname CHAR(10), rating INTEGER,
The Relational Model and Relational Algebra Nothing is so practical as a good theory Kurt Lewin, 1945.
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
COP-5725 Practice Exercises
Chapter (7): Advanced SQL
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
Review Indra Budi Fakultas Ilmu Komputer UI 2 Database Introduction Database vs File Processing Main purpose of database Database Actors.
Review for Final Test Indra Budi
1 SQL - Select Join / Outer Join Sub queries Join Join Outer join Left outer join Right outer join.
1 Lecture 11: Basic SQL, Integrity constraints
CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.
Cs3431 Constraints Sections 6.1 – 6.5. cs3431 Example CREATE TABLE Student ( sNum int, sName varchar (20), prof int, CONSTRAINT pk PRIMARY KEY (snum),
15/1/20091 Lecture 2 on Relational Algebra This lecture introduces relational data model with relational algebra as its mathematical foundation Relational.
Introduction to Structured Query Language (SQL). Origins & history Early 1970’s – IBM develops Sequel as part of the System R project at its San Hose.
Set operators Union Minus Intersect match ALL COLUMNS must have same ID, column names.
MSc IT UFIE8K-15-M Data Management Prakash Chatterjee Room 3P16
CSE 190: Internet E-Commerce Lecture 10: Data Tier.
Northwind Sample database (also supplied with MS Access)
Summary. Chapter 9 – Triggers Integrity constraints Enforcing IC with different techniques –Keys –Foreign keys –Attribute-based constraints –Schema-based.
Domains, Relations & Base RelVars (Ch
Copyright © 2004 Pearson Education, Inc.. Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries.
CS405G: Introduction to Database Systems Final Review.
The Relational Model Codd (1970): based on set theory Relational model: represents the database as a collection of relations (a table of values --> file)
Relational Model & Relational Algebra. 2 Relational Model u Terminology of relational model. u How tables are used to represent data. u Connection between.
© Pearson Education Limited, Chapter 2 The Relational Model Transparencies.
Relational Algebra II. Semantics - where A WHERE X  Y –  : an operator like =, >, … Result of restriction A by the condition X  Y is A relation –with.
1 Exercises of SQL uAnswer the following questions, based on the database below. wSupplier (SNO, SNAME, STATUS, CITY) wPart(PNO, PNAME, COLOR, WEIGHT)
FEN  Concepts and terminology  Operations (relational algebra)  Integrity constraints The relational model.
1 Structured Query Language (SQL) CIS*2450 Advanced Programming Concepts.
CTFS Workshop Shameema Esufali Suzanne Lao Data coordinators and technical resources for the network
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
Chapter 6 SQL: Data Manipulation (Advanced Commands) Pearson Education © 2009.
FEN Introduction to the database field:  The Relational Model Seminar: Introduction to relational databases.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
Algebra1 After this lecture, you should be able to:  Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
SQL LANGUAGE and Relational Data Model TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries.
1 Algebra of Queries Classical Relational Algebra It is a collection of operations on relations. Each operation takes one or two relations as its operand(s)
COMP3030 Database Management System Final Review
Definition and Properties Constraints and Schemas Update
CTFS Workshop Shameema Esufali Asian data coordinator and technical resource for the network
SQL: Sets UNION INTERSECT MINUS D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Sets 1.
SQL: Sub-queries Single-value sub-queries Single-column sub-queries Sub-queries that produce tables Correlated sub-queries D. Christozov / G.Tuparov INF.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 6-1 David M. Kroenke’s Chapter Six: Transforming Data Models into Database.
Chapter 3 The Relational Model. Objectives u Terminology of relational model. u How tables are used to represent data. u Connection between mathematical.
FEN Introduction to the database field: The development process Seminar: Introduction to relational databases Development process: Analyse.
Quiz Which of the following is not a mandatory characteristic of a relation? Rows are not ordered (Not required) Each row is a unique There is a.
Lab week 10 Aggregates and sub-queries And assignment details.
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Basic SQL Queries.
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
1 CS122A: Introduction to Data Management Lecture 8 Introduction to SQL Instructor: Chen Li.
Structured Query Language IV Asma Ahmad (C. J. Date) Database Systems.
Basic SQL Queries Go over example queries, like 10 > ALL.
COP 4540 Database Management
ECET 450 Innovative Education--snaptutorial.com
Generalization.
David M. Kroenke and David J
پايگاه داده ها.
Φροντιστήριο SQL (από το βιβλίο του Date)
The Relational Model Textbook /7/2018.
Database Dr. Roueida Mohammed.
04 SQL & Relational Algebra
Presentation transcript:

SQL - miscellaneous D. Christozov / G.Tuparov INF 280 Database Systems: SQL – miscellaneous 1

Given the Database SPJ Schema G.Tuparov INF 280 Database Systems: SQL – miscellaneous 2 Notes: 1. 'S' means Supplier; 'P' – Part; 'J' – Project; 'SPJ' – Supplies (cross table of the three) 2. Attribute with name as 'X_ID' is primary key 3. Equally named attributes, with names as “X_ID”, define referential integrity constraints (foreign key).

Given the Database SPJ State S (Supplier) SPJ S_IDSNameStatusCity S_IDP_IDJ_IDQuantity S1Smith20London S1P1J1200 S2Jones10Paris S1P1J4700 S3Blake30Paris S2P3J1400 S4Clark20London S2P3J2200 S5Adams30Athens S2P3J3200 S2P3J4500 P (Part) S2P3J5600 P_IDPNameColourWeightCity S2P3J6400 P1NutRed12London S2P3J7800 P2BoltGreen17Paris S2P5J2100 P3ScrewBlue17Rome S3P3J1200 P4ScrewRed14London S3P4J2500 P5CamBlue12Paris S4P6J3300 P6CogRed19London S4P6J7300 S5P2J2200 J (Project) S5P2J4100 J_IDJNameCity S5P5J5500 J1SorterParis S5P5J7100 J2PunchRome S5P6J2200 J3ReaderAthens S5P1J4100 J4ConsoleAthens S5P3J4200 J5CollatorLondon S5P4J4800 J6TerminalOslo S5P5J4400 J7TapeLondon S5P6J4500 D. Christozov / G.Tuparov INF 280 Database Systems: SQL – miscellaneous 3 Notes: 1. 'S' means Supplier; 'P' – Part; 'J' – Project; 'SPJ' – Supplies (cross table of the three) 2. Attribute with name as 'X_ID' is primary key 3. Equally named attributes, with names as “X_ID”, define referential integrity constraints (foreign key).

Task 1 (Oracle 11g/MySQL 5.5) 1. List suppliers (S.*) with cities, where there are no parts. 'Sub Query Solution SELECT * FROM S WHERE City NOT IN (SELECT DISTINCT City FROM P); OR Result: 'Correlated Query Solution D. Christozov / G.Tuparov INF 280 Database Systems: SQL – miscellaneous 4 SELECT * FROM S WHERE NOT EXISTS (SELECT DISTINCT City FROM P WHERE P.City = S.City);

Task 2 (MySQL 5.5/Oracle 11g) 2. List projects (P.*), which are not supplied with any 'Red' part by any supplier from 'London'. (Note: Inner join, using foreign keys) Result: D. Christozov / G.Tuparov INF 280 Database Systems: SQL – miscellaneous 5 ( SELECT J_ID FROM (P INNER JOIN SPJ USING (P _ID )) INNER JOIN S USING (S _ID ) WHERE P.Colour = ' Red' AND S.City = 'London'); SELECT * FROM J WHERE J_ID NOT IN (SELECT J_ID FROM P, SPJ, S WHERE SPJ.P_ID = P.P_ID AND SPJ.S_ID = S.S_ID AND P.Colour = 'Red' AND S.City = 'London');

Task 3 (Oracle 11g/MySQL 5.5) 3. List all cities, where the company operates (where at least one supplier, part, or project is located). D. Christozov / G.Tuparov INF 280 Database Systems: SQL – miscellaneous 6 Result: Oracle 11g MySQL 5.5 SELECT DISTINCT City FROM J UNION SELECT DISTINCT City FROM P UNION SELECT DISTINCT City FROM S;

Task 4 (Oracle 11g/MySQL 5.5) 4. List projects (J.*) with cities, where there are no suppliers OR D. Christozov / G.Tuparov INF 280 Database Systems: SQL – miscellaneous 7 SELECT * FROM J WHERE City NOT IN (SELECT DISTINCT City FROM S); SELECT * FROM J WHERE NOT EXISTS (SELECT DISTINCT City FROM S WHERE S.City = J.City); Result: Oracle 11g MySQL 5.5

Task 5 (MySQL 5.5/Oracle 11g) 5. List projects (J.*) supplied entirely by supplier 'S2' SELECT * FROM J WHERE NOT EXISTS (SELECT * FROM SPJ WHERE SPJ.J_ID = J.J_ID AND NOT (SPJ.S_ID = 'S2')); D. Christozov / G.Tuparov INF 280 Database Systems: SQL – miscellaneous 8 Result: MySQL 5.5

Task 6 (Oracle 11g/MySQL 5.5) 6. List all supplies (SPJ.*) performed in London (either project or supplier is located in London). SELECT SPJ.S_ID, SPJ.P_ID, SPJ.J_ID, SPJ.Quantity FROM SPJ INNER JOIN S ON SPJ.S_ID=S.S_ID WHERE S.City = 'London' UNION SELECT SPJ.S_ID, SPJ.P_ID, SPJ.J_ID, SPJ.Quantity FROM SPJ INNER JOIN J ON SPJ.J_ID=J.J_ID WHERE J.City = 'London' ORDER BY 1, 2, 3; D. Christozov / G.Tuparov INF 280 Database Systems: SQL – miscellaneous 9 Oracle 11g MySQL 5.5

7. List parts (P.*) with cities, where there are no suppliers OR Task 7 (Oracle 11g/MySQL 5.5) D. Christozov / G.Tuparov INF 280 Database Systems: SQL – miscellaneous 10 Oracle 11g MySQL 5.5 SELECT * FROM P WHERE City NOT IN (SELECT DISTINCT City FROM S); SELECT * FROM P WHERE NOT EXISTS (SELECT DISTINCT City FROM S WHERE P.City = S.City);

Task 8 (Oracle 11g/MySQL 5.5) 8. List parts (P.*) supplied to all projects in London. SELECT * FROM P WHERE NOT EXISTS ( SELECT * FROM J WHERE J.City = 'London' AND NOT EXISTS (SELECT * FROM SPJ WHERE SPJ.P_ID = P.P_ID AND SPJ.J_ID = J.J_ID) ); D. Christozov / G.Tuparov INF 280 Database Systems: SQL – miscellaneous 11 Oracle 11g MySQL 5.5

Task 9 (Oracle 11g/MySQL 5.5) 9. List all cities, where both a supplier and a project are located. D. Christozov / G.Tuparov INF 280 Database Systems: SQL – miscellaneous 12 MySQL 5.5 Solution: SELECT DISTINCT City FROM S INNER JOIN J USING (City) ORDER BY 1; Oracle 11g Solution: SELECT DISTINCT City FROM S INTERSECT SELECT DISTINCT City FROM J;

Task 10 (Oracle 11g/MySQL 5.5) 10. List suppliers (S.*) with cities, where there are no projects Or D. Christozov / G.Tuparov INF 280 Database Systems: SQL – miscellaneous 13 SELECT * FROM S WHERE NOT EXISTS (SELECT DISTINCT City FROM J WHERE J.City = S.City); SELECT * FROM S WHERE City NOT IN (SELECT DISTINCT City FROM J ); No Data Found

Task 12 (Oracle 11g/MySQL 5.5) 12. List all projects (J.*), which are not supplied by any (at least one) part from London. SELECT * FROM J WHERE NOT EXISTS (SELECT * FROM SPJ INNER JOIN P USING (P_ID) WHERE P.City = 'London' ANDSPJ.J_ID = J.J_ID); D. Christozov / G.Tuparov INF 280 Database Systems: SQL – miscellaneous 14