Relational Algebra and SQL Exercises

Slides:



Advertisements
Similar presentations
From the Calculus to the Structured Query Language Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 22, 2005.
Advertisements

Relational Algebra and SQL Exercises
TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
1 Advanced SQL Queries. 2 Example Tables Used Reserves sidbidday /10/04 11/12/04 Sailors sidsnameratingage Dustin Lubber Rusty.
IS698: Database Management Min Song IS NJIT. The Relational Data Model.
SQL, RA, Sets, Bags Fundamental difference between theoretical RA and practical application of it in DBMSs and SQL RA  uses sets SQL  uses bags (multisets)
Relational Algebra Tim Kaddoura CS157A. Introduction  Relational query languages are languages for describing queries on a relational database  Three.
 Database is SQL1.mdb ◦ import using MySQL Migration Toolkit 
Relational Algebra and SQL Exercises Dr. Shiyong Lu Department of Computer Science Wayne State University ©copyright 2007, all rights.
Relational Algebra and SQL Exercises
BACS 485 Structured Query Language 2. BACS 485 SQL Practice Problems Assume that a database named COLLEGE exists. It contains the tables defined below.
M ATH IN SQL. 222 A GGREGATION O PERATORS Operators on sets of tuples. Significant extension of relational algebra. SUM ( [DISTINCT] A): the sum of all.
CS411 Database Systems Kazuhiro Minami 06: SQL. Join Expressions.
CREATE VIEW SYNTAX CREATE VIEW name [(view_col [, view_col …])] AS [WITH CHECK OPTION];
INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012.
Employee database: Conceptual Schema in ERD Chapter 3, page 62.
McMillan’s Quizzes Multiple choice, open book, open notes Not open computers, not open classmates Partial credit: if less than half the class gets a question.
Class 8 Team Meeting (at regular class time) Team President will arrange the location Team Secretary will take attendance Everyone MUST attend!
DATABASES AND TRANSACTION PROCESSING: An Application-Oriented Approach Lewis Bernstein Kifer © 2002 Addison Wesley.
1 Chapter 5 Relational Algebra and SQL. 2 Father of Relational Model Edgar F. Codd ( ) PhD from U. of Michigan, Ann Arbor Received Turing Award.
Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Rel. model - SQL part1.
Temple University – CIS Dept. CIS331– Principles of Database Systems V. Megalooikonomou Relational Model – SQL Part I (based on notes by Silberchatz,Korth,
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Relational Algebra 2 Chapter 5.2 V3.0 Napier University Dr Gordon Russell.
Review for Midterm. Problem 2.7 HasPriPhy Patients address name ssn age Doctors exp_year name ssn specialty Pharmacies name address phone PharmCo name.
1 CSE 480: Database Systems Lecture 5: Relational Data Model.
Query Optimization (CB Chapter ) CPSC 356 Database Ellen Walker Hiram College (Includes figures from Database Systems: An Application Oriented.
1 Chapter 5 Relational Algebra and SQL. 2 Father of Relational Model Edgar F. Codd ( ) PhD from U. of Michigan, Ann Arbor Received Turing Award.
$1,000,000 $500,000 $100,000 $50,000 $10,000 $5000 $1000 $500 $200 $100 Is this your Final Answer? YesNo Question 2? Correct Answer Wrong Answer.
1 Chapter 5 Relational Algebra and SQL. 2 Relational Query Languages Languages for describing queries on a relational database Structured Query LanguageStructured.
Dr. Alexandra I. Cristea CS 319: Theory of Databases: Exam preparation.
Main MenuExercise Set 1Exercise Set 2 Interactive Exercises Set 1 Exercise 1.1 Exercise 1.2 Exercise 1.3.
1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)
MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 3 © Akhilesh Bajaj, 2000, 2002, All Rights.
HOP Summer 2011 Peer Counselor:. Academic Standing – page 36 Credit/No Credit – page 44 Grading Symbols – page 45 Graduation – page 46/47.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 10 A First Course in Database Systems.
ORT Braude, CSE 61309, ©2004 Gary Schloss Exam Question #1: ER, RA & SQL Consider a DB schema with the following relations: - Student (s#, sname) - Professor.
1 SY306 Web and Databases for Cyber Operations Set #13: SQL SELECT Grouping and sub-queries.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Grouping These slides are licensed under.
1 Chapter 5 Relational Algebra and SQL. 2 Father of Relational Model Edgar F. Codd ( ) PhD from U. of Michigan, Ann Arbor Received Turing Award.
MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 4 © Akhilesh Bajaj, 2000, 2002, 2004, All.
CSCI 4333 Database Design and Implementation – Exercise (2) Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
Factorization : Difference of Two Squares and Perfect Squares.
QUERY CONSTRUCTION CS1100: Data, Databases, and Queries CS1100Microsoft Access1.
EXAMPLE 4 Solve linear systems with many or no solutions Solve the linear system. a.x – 2y = 4 3x – 6y = 8 b.4x – 10y = 8 – 14x + 35y = – 28 SOLUTION a.
CSCI 4333 Database Design and Implementation – Exercise (1) Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
Which list of numbers is ordered from least to greatest? 10 –3, , 1, 10, , 1, 10, 10 2, 10 – , 10 –3, 1, 10, , 10 –3,
1 CSE 480: Database Systems Lecture 16: Relational Algebra.
Access Test Solutions Fall 2014 Questions 7 and 8 Karl Lieberherr.
1 Schema for Student Registration System Student Student (Id, Name, Addr, Status) Professor Professor (Id, Name, DeptId) Course Course (DeptId, CrsCode,
1 SQL Chapter 9 – 8 th edition With help from Chapter 2 – 10 th edition.
SQL: Interactive Queries (2) Prof. Weining Zhang Cs.utsa.edu.
Introduction to CSCI 242 Compiled by S. Zhang 1. Syllabus Syllabus has the most updated information! –Use the information on the syllabus for the grading.
Retrieve the names of all senior students majoring in 'COSC' (computer science). SELECT Name FROM STUDENT WHERE Major='COSC'
Relational Calculus. Relational calculus query specifies what is to be retrieved rather than how to retrieve it. – No description of how to evaluate a.
CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
1 Relational Algebra & SQL Query Formulation Exercise.
Relational Algebra and SQL
Relational Algebra and SQL
Review Problems Algebra 1 11-R.
Dr. Shiyong Lu Wayne State Univesrity
Rule Exercises Status of the Ball Definitions and Rule 15
Rule Exercises Status of the Ball Definitions and Rule 15
Evaluate the expression by sketching a triangle
SQL: Structured Query Language
Relational Algebra and SQL Examples
Evaluating expressions and Properties of operations
Joins and other advanced Queries
LINQ to SQL Part 3.
CSCI 4333 Database Design and Implementation – Exercise (1)
Presentation transcript:

Relational Algebra and SQL Exercises Revised based on the notes from Dr. Shiyong Lu of Wayne State University Database Systems: An Application Oriented Approach, Edition 2 by Michael Kifer, Arthur Bernstein, Philip M. Lewis ISBN:0321268458 ISBN13:9780321268457 Release Date:March 2005

Relational Algebra and SQL Exercises Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn)

Query 1 Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn) Return those professors who have taught ‘CSCI203’ but never ‘CSCI240’.

Relational Algebra Solution Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn) ssn(crsname=‘CSCI203’(Taught)) -ssn(crsname=‘CSCI240’(Taught))

SQL Solution Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn) SELECT ssn From Taught Where crsname = ‘CSCI203’ EXCEPT Where crsname = ‘CSCI240’;

Query 2 Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn) Return those professors who have taught both ‘CSCI203’ and ‘CSCI204’.

Relational Algebra Solution Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn) ssn(crsname=‘CSCI203’  crsname=‘CSCI204’ (Taught), wrong! ssn(crsname=‘CSCI203’(Taught))  ssn(crsname=‘CSCI204’(Taught)), correct!

SQL Solution Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn) SELECT T1.ssn From Taught T1, Taught T2, Where T1.crsname = ‘CSCI203’ AND T2.crsname=‘CSCI204’ AND T1.ssn=T2.ssn;

Query 3 Return those professors who have never taught ‘CSCI204’. Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn) Return those professors who have never taught ‘CSCI204’.

Relational Algebra Solution Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn) ssn(crsname<>‘CSCI204’(Taught)), wrong answer! ssn(Professor) -ssn(crsname=‘CSCI204’(Taught)), correct answer!

SQL Solution Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn) SELECT ssn From Professor EXCEPT From Taught T Where T.crsname = ‘CSCI204’;

Query 4 Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn) Return those professors who taught ‘CSCI203’ and ‘CSCI204’ in the same semester

Relational Algebra Solution Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn) ssn(crsname1=‘CSCI203’(Taught[crsname1, ssn, semester])  crsname2=‘CSCI204’(Taught[crsname2, ssn, semester]))

SQL Solution Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn) SELECT T1.ssn From Taught T1, Taught T2, Where T1.crsname = ‘CSCI203’ AND T2.crsname=‘CSCI204’ AND T1.ssn=T2.ssn AND T1.semester=T2.semester;

Query 5 Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn) Return those professors who taught ‘CSCI204’ or ‘CSCI315’ but not both.

Relational Algebra Solution Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn) ssn(crsname<>‘CSCI204’  crsname=‘CSCI315’(Taught))-(ssn(crsname=‘CSCI204’(Taught))  ssn(crsname=‘CSCI315’(Taught)))

SQL Solution Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn) SELECT ssn FROM Taught T WHERE T.crsname=‘CSCI204’ OR T.crsname=‘CSCI315’ Except SELECT T1.ssn From Taught T1, Taught T2 Where T1.crsname = ‘CSCI204’ AND T2.crsname=‘CSCI315’ AND T1.ssn=T2.ssn;

Query 6 Return those courses that have never been taught. Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn) Return those courses that have never been taught.

Relational Algebra Solution Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn) crsname(Course)-crsname(Taught)

SQL Solution Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn) SELECT crsname FROM Course) EXCEPT FROM Taught;

Query 7 Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn) Return those courses that have been taught in two different semesters.

Relational Algebra Solution Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn) crscode( semester1 <> semester2( Taught[crsname, ssn1, semester1]  Taught[crsname, ssn2, semester2]))

SQL Solution Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn) SELECT distinct T1.crsname FROM Taught T1, Taught T2 WHERE T1.crsname=T2.crsname AND T1.semester <> T2.semester;

Query 8 Return those courses that have been taught at least twice. Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn) Return those courses that have been taught at least twice.

SQL Solution Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn) This is an example of queries in which relational algebra can’t express accurately as RA doesn’t have aggregate functions. SELECT crsname FROM Taught GROUP BY crsname HAVING COUNT(*) >= 2;

Query 9 Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn) Return those courses that have been taught by at least 3 different professors.

SQL Solution Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn) This is an example of queries in which relational algebra can’t express accurately as RA doesn’t have aggregate functions. SELECT crsname FROM (SELECT DISTINCT crsname, ssn FROM TAUGHT) GROUP BY crsname HAVING COUNT(*) >= 3

Query 10 Return the names of the professors who ever taught ‘CSCI204’. Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn) Return the names of the professors who ever taught ‘CSCI204’.

Relational Algebra Solution Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn) profname(crscode=‘CCI204’(Taught) Professor)

SQL Solution Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn) SELECT P.profname FROM Professor P, Taught T WHERE P.ssn = T.ssn AND T.crsname = ‘CSCI204’;

Query 11 Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn) Return the names of the senior professors who ever taught ‘CSCI204’.

Relational Algebra Solution Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn) profname(crsname=‘CSCI204’(Taught) status=‘senior’(Professor))

SQL Solution Professor(ssn, profname, status) Course(crscode, crsname, credits) Taught(crsname, semester, ssn) SELECT P.profname FROM Professor P, Taught T WHERE P.status = ‘senior’ AND P.ssn = T.ssn AND T.crsname = ‘CSCI204’;