Relational Algebra and SQL Exercises

Slides:



Advertisements
Similar presentations
Relational Algebra and SQL Exercises
Advertisements

IS698: Database Management Min Song IS NJIT. The Relational Data Model.
Relational Algebra Tim Kaddoura CS157A. Introduction  Relational query languages are languages for describing queries on a relational database  Three.
Relational Algebra and SQL Exercises Dr. Shiyong Lu Department of Computer Science Wayne State University ©copyright 2007, all rights.
CSCI 4333 Database Design and Implementation Review for Midterm Exam II Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
CREATE VIEW SYNTAX CREATE VIEW name [(view_col [, view_col …])] AS [WITH CHECK OPTION];
INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012.
1 Relational Algebra and SQL Chapter 6. 2 Relational Query Languages Languages for describing queries on a relational database Structured Query LanguageStructured.
Employee database: Conceptual Schema in ERD Chapter 3, page 62.
Chapter 7 Triggers and Active Databases. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Trigger Overview Element of the database schema.
1 The Relational Data Model Chapter 4. 2 Data and Its Structure Data is actually stored as bits, but it is difficult to work with data at this level.
Class 8 Team Meeting (at regular class time) Team President will arrange the location Team Secretary will take attendance Everyone MUST attend!
1 The Relational Data Model Chapter 4. 2 Data and Its Structure Data is actually stored as bits, but it is difficult to work with data at this level.
Chapter 3 The Relational Data Model. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 3-2 Data and Its Structure Data is actually stored.
Relational Algebra and SQL Prof. Sin-Min Lee Department of Computer Science.
SQL SQL stands for Structured Query Language SQL allows you to access a database SQL is an ANSI standard computer language SQL can execute queries against.
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.
SQL Operations Aggregate Functions Having Clause Database Access Layer A2 Teacher Up skilling LECTURE 5.
Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Rel. model - SQL part1.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Exercise Normalization Exercise Faculty Member. Social Security Number Name Last Name First Name Middle Name Home Address Street Address or P. O. Box.
ESP 101ESP 301ESP 200 ESP 339ESP 401 ESP 402ESP 403 ESP 349 ESP 404 ESP 406 ESP 405 ESP 461 Draft of Undergraduate Plan for Merger of Special and Elementary/Early.
CSc-340 2b1 Introduction to SQL Chapter 3 [2 of 2] Null Values Aggregate Functions Nested Subqueries Modification of the Database.
Chapter 8 Using SQL in an Application. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-2 CSC 4480 outline Intro (3-9) Static SQL (10-11)
1 CSE 480: Database Systems Lecture 5: Relational Data Model.
Using sub query. Sub query A query inside another query.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
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.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
1 Chapter 5 Relational Algebra and SQL. 2 Relational Query Languages Languages for describing queries on a relational database Structured Query LanguageStructured.
Fall 2001Database Systems1 Triggers Assertions –Assertions describe rules that should hold for a given database. –An assertion is checked anytime a table.
1 Chapter 7 Triggers and Active Databases. 2 Trigger Overview Element of the database schema General form: ON IF THEN –Event- request to execute database.
1 Chapter 7 Triggers and Active Databases. 2 Trigger Overview Element of the database schema General form: ON IF THEN –Event- request to execute database.
INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)
INTRODUCTION TO SQL Database System Concepts. Agenda Overview of the SQL Query Language Data Definition Basic Query Structure Additional Basic Operations.
MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 3 © Akhilesh Bajaj, 2000, 2002, All Rights.
Chapter 5 Relational Algebra and SQL. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 5-2 Relational Query Languages Languages for describing.
Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2.
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 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.
Drill Consider the following tables with the following fields: Student: FName, LName, StudentID, Age, Yr, Course Grades: ID, P1, P2, P3 1.Display the.
CSCI 4333 Database Design and Implementation – Exercise (1) Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
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,
به نام خدا SQL QUIZ جوانمرد Website: ejavanmard.blogfa.com.
CSCI 6315 Applied Database Systems Review for Final Exam Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX
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.
CSCI 6315 Applied Database Systems Review for Midterm Exam II Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
1 Relational Algebra & SQL Query Formulation Exercise.
SQL and Relational Algebra Edel Sherratt Nigel Hardy Horst Holstein.
Structured Query Language
Chapter 3 Introduction to SQL
The Relational Model.
Introduction to SQL.
Relational Algebra and SQL
Relational Algebra and SQL
Trigger Overview Element of the database schema
Triggers and Active Databases
Relational Algebra and SQL Exercises
Relational Algebra and SQL Examples
The Relational Data Model
CSCI 4333 Database Design and Implementation – Exercise (1)
CS 405G: Introduction to Database Systems
Presentation transcript:

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

Query 1 Return those professors who have taught ‘csc6710’ but never ‘csc7710’.

Relational Algebra Solution ssn(crscode=‘csc6710’(Taught))-ssn(crscode=‘csc7710’(Taught))

SQL Solution (SELECT ssn From Taught Where crscode = ‘CSC6710’) EXCEPT

Query 2 Return those professors who have taught both ‘csc6710’ and ‘csc7710’.

Relational Algebra Solution ssn(crscode=‘csc6710’(Taught))  ssn(crscode=‘csc7710’(Taught)),

SQL Solution SELECT T1.ssn From Taught T1, Taught T2, Where T1.crscode = ‘CSC6710’ AND T2.crscode=‘CSC7710’ AND T1.ssn=T2.ssn

Query 3 Return those professors who have never taught ‘csc7710’.

Relational Algebra Solution ssn(Professor)-ssn(crscode=‘csc7710’(Taught)),

SQL Solution (SELECT ssn From Professor) EXCEPT From Taught T Where T.crscode = ‘CSC7710’)

Query 4 Return those professors who taught ‘CSC6710’ or ‘CSC7710” but not both.

Relational Algebra Solution ssn(crscode<>‘csc7710’  crscode=‘csc7710’(Taught))-(ssn(crscode=‘csc6710’(Taught))  ssn(crscode=‘csc7710’(Taught)))

SQL Solution (SELECT ssn FROM Taught T WHERE T.crscode=‘CSC6710’ OR T.crscode=‘CSC7710’) Except (SELECT T1.ssn From Taught T1, Taught T2, Where T1.crscode = ‘CSC6710’) AND T2.crscode=‘CSC7710’ AND T1.ssn=T2.ssn)

Query 5 Return those courses that have never been taught.

Relational Algebra Solution crscode(Course)-crscode(Taught)

SQL Solution (SELECT crscode FROM Course) EXCEPT FROM TAUGHT )

Query 6 Return those courses that have been taught at least in 10 semesters.

SQL Solution SELECT crscode FROM Taught GROUP BY crscode HAVING COUNT(*) >= 10

Query 7 Return those courses that have been taught by at least 5 different professors.

SQL Solution SELECT crscode FROM (SELECT DISTINCT crscode, ssn FROM TAUGHT) GROUP BY crscode HAVING COUNT(*) >= 5

Query 8 Return the names of professors who ever taught ‘CSC6710’.

Relational Algebra Solution profname(crscode=‘csc6710’(Taught) Professor)

SQL Solution SELECT P.profname FROM Professor P, Taught T WHERE P.ssn = T.ssn AND T.crscode = ‘CSC6710’

Query 9 Return the names of full professors who ever taught ‘CSC6710’.

Relational Algebra Solution profname(crscode=‘csc6710’(Taught) status=‘full’(Professor))

SQL Solution SELECT P.profname FROM Professor P, Taught T WHERE P.status = ‘full’ AND P.ssn = T.ssn AND T.crscode = ‘CSC6710’

Query 10 Delete those professors who never taught a course.

SQL Solution DELETE FROM Professor WHERE ssn NOT IN (SELECT ssn FROM Taught )

Query 11 Change all the credits to 4 for those courses that are taught in f2006 semester.

SQL Solution UPDATE Course SET credits = 4 WHERE crscode IN ( SELECT crscode FROM Taught WHERE semester = ‘f2006’ )

Query 12 Return the names of the professors who have taught more than 30 credits of courses.

SQL Solution SELECT profname FROM Professor WHERE ssn IN ( SELECT T.ssn FROM Taught T, Course C WHERE T.crscode = C.crscode GROUP BY T.ssn HAVING SUM(C.credits) > 30 )

Query 13 List all the course names that professor ‘Smith” taught in Fall of 2007.

Relational Algebra Solution crsname(profname=‘Smith’(Professor) semester=‘f2007’(Taught) Course)

SQL Solution SELECT crsname FROM Professor P, Taught T, Course C WHERE P.profname = ‘Smith’ AND P.ssn = T.ssn AND T.semester = ‘F2007’ AND T.crscode = C.crscode

Query 14 In chronological order, list the number of courses that the professor with ssn ssn = 123456789 taught in each semester.

SQL Solution SELECT semester, COUNT(*) FROM Taught WHERE ssn = ‘123456789’ GROUP BY semester ORDER BY semester ASC

Query 15 In alphabetical order of the names of professors, list the name of each professor and the total number of courses she/he has taught.

SQL Solution SELECT P.profname, COUNT(*) FROM Professor P, Taught T WHERE P.ssn = T.ssn GROUP BY P.ssn, P.profname ORDER BY P.profname ASC

Query 16 Delete those professors who taught less than 10 courses.

SQL Solution DELETE FROM Professor WHERE ssn IN( SELECT ssn FROM Taught GROUP BY ssn HAVING COUNT(*) < 10 )

Query 17 Delete those professors who taught less than 40 credits.

SQL Solution DELETE FROM Professor WHERE ssn IN( SELECT T.ssn FROM Taught T, Course C WHERE T.crscode = C.crscode GROUP BY ssn HAVING SUM(C.credits) < 40 )

Query 18 List those professors who have not taught any course in the past three semesters (F2006, W2007, F2007).

SQL Solution SELECT * FROM Professor P WHERE NOT EXISTS( FROM Taught WHERE P.ssn = T.ssn AND (T.semester = ‘F2006’ OR T.semester = ‘W2007’ OR T.semester=‘F2007’)) )