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.

Slides:



Advertisements
Similar presentations
SQL – Lesson II Grade 12.
Advertisements

Union, Intersection, Difference (subquery) UNION (subquery) produces the union of the two relations. Similarly for INTERSECT, EXCEPT = intersection and.
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.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications C. Faloutsos & A. Pavlo Lecture#6: Rel. model - SQL part1 (R&G, chapter.
 Database is SQL1.mdb ◦ import using MySQL Migration Toolkit 
CS411 Database Systems Kazuhiro Minami 06: SQL. Join Expressions.
CREATE VIEW SYNTAX CREATE VIEW name [(view_col [, view_col …])] AS [WITH CHECK OPTION];
Data Warehousing/Mining 1 Data Warehousing/Mining Comp 150 Aggregation in SQL (not in book) Instructor: Dan Hebert.
INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012.
Subqueries Example Find the name of the producer of ‘Star Wars’.
Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete.
Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.
Nov 24, 2003Murali Mani SQL B term 2004: lecture 12.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #3.
--The SQL Query Language DML--1 LIKE  LIKE allows to select character strings which have some element in common by using wild cards:  Wild cards:  “%”
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.
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. SQL - part 2 - Database Management Systems I Alex Coman, Winter 2006.
Winter 2002Arthur Keller – CS 1807–1 Schedule Today: Jan. 24 (TH) u Subqueries, Grouping and Aggregation. u Read Sections Project Part 2 due.
SQL Operations Aggregate Functions Having Clause Database Access Layer A2 Teacher Up skilling LECTURE 5.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
CSc-340 2b1 Introduction to SQL Chapter 3 [2 of 2] Null Values Aggregate Functions Nested Subqueries Modification of the Database.
1 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Instructor: Jinze Liu Fall Basic Components (2) Relational Database Web-Interface Done before mid-term Must-Have Components (2) Security: access.
MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 5 © Akhilesh Bajaj, 2000, 2002, 2003, All.
BACS 287 Structured Query Language 1. BACS 287 Visual Basic Table Access Visual Basic provides 2 mechanisms to access data in tables: – Record-at-a-time.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)
ICS 321 Spring 2011 The Database Language SQL (iii) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 3/14/20111Lipyeow.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
Course title: Database-ii Chap No: 03 “Advanced SQL” Course instructor: ILTAF MEHDI.
SCUHolliday - coen 1787–1 Schedule Today: u Subqueries, Grouping and Aggregation. u Read Sections Next u Modifications, Schemas, Views. u Read.
1 SY306 Web and Databases for Cyber Operations Set #13: SQL SELECT Grouping and sub-queries.
1 CSE 480: Database Systems Lecture 16: Relational Algebra.
1 Schema for Student Registration System Student Student (Id, Name, Addr, Status) Professor Professor (Id, Name, DeptId) Course Course (DeptId, CrsCode,
SQL: Structured Query Language Instructor: Mohamed Eltabakh 1 Part II.
SQL LANGUAGE TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Day 5 - More Complexity With Queries Explanation of JOIN & Examples Explanation of JOIN & Examples Explanation & Examples of Aggregation Explanation &
SQL: Interactive Queries (2) Prof. Weining Zhang Cs.utsa.edu.
Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Subqueries CIS 4301 Lecture Notes Lecture /23/2006.
1 Introduction to Database Systems, CS420 SQL JOIN, Aggregate, Grouping, HAVING and DML Clauses.
More SQL: Complex Queries,
Chapter 3 Introduction to SQL
Slides are reused by the approval of Jeffrey Ullman’s
Outerjoins, Grouping/Aggregation Insert/Delete/Update
Chapter 3 Introduction to SQL(3)
CPSC-310 Database Systems
Schedule Today: Next After that Subqueries, Grouping and Aggregation.
SQL : Query Language Part II CS3431.
CS 405G: Introduction to Database Systems
SQL – Entire Select.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
CSCE 315 – Programming Studio Spring 2010 Project 1, Lecture 4
SQL: Structured Query Language
Structured Query Language
CMPT 354: Database System I
Query Functions.
SQL: Structured Query Language
Section 4 - Sorting/Functions
SQL: Structured Query Language
Instructor: Zhe He Department of Computer Science
SQL: Structured Query Language
Presentation transcript:

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 (p#, pname) - Course (c#, prof#, title, credits, room#) - Enroll (stud#, course#) - Room (r#, capacity)

ORT Braude, CSE 61309, ©2004 Gary Schloss Draw an ER model of that database. Make sure to indicate that each course is taught by one professor and in one classroom.

ORT Braude, CSE 61309, ©2004 Gary Schloss Exam Question #1 (contd.) Write an RA expression that finds the names of all students who are enrolled in a class taught by Professor Jones, and are enrolled in a Physics class. JonesC#s = PROJECT [SELECT (Professor JOIN Course)] JonesStuds = PROJECT [Student JOIN (Enroll JOIN JonesC#s)] PhysicsStuds = PROJECT [Student JOIN (SELECT (Enroll JOIN Course))] Answer = PROJECT [Student JOIN (JonesStuds INTERSECT PhysicsStuds)]

ORT Braude, CSE 61309, ©2004 Gary Schloss Exam Question #1 (contd.) Write an RA expression that finds the names of all students who are not enrolled in two classes held in the same room. StudCseRm = PROJECT (Course JOIN Enroll) SameRmStuds = PROJECT (StudCseRm JOIN StudCseRm) AllStuds = PROJECT Student Answer = PROJECT [Student JOIN (AllStuds – SameRmStuds)]

ORT Braude, CSE 61309, ©2004 Gary Schloss Exam Question #1 (contd.) Write an SQL query that lists, in alphabetical order, the title of all the courses either taught by Prof. Smith or are taught in room 44. Do not list duplicate titles. SELECT DISTINCT title FROM Course WHERE c# IN (SELECT c# FROM Professor, Course WHERE p# = prof# AND pname = “Smith”) OR room# = 44 ORDER BY title;

ORT Braude, CSE 61309, ©2004 Gary Schloss Exam Question #1 (contd.) Write an SQL query that considers all the courses that have ever been taught by Prof. Brown and are of 3 credits, and groups them according to title. For each course, the query gives its title and the average capacity of rooms in which the course was offered, and only courses with average room capacity of greater than 20 are listed. SELECT title, AVG(capacity) FROM Room, Course WHERE r# = room# AND credits = 3 AND c# IN (SELECT c# FROM Professor, Course WHERE p# = prof# AND pname = “Brown”) GROUP BY title HAVING AVG(capacity) > 20;

ORT Braude, CSE 61309, ©2004 Gary Schloss Reminder: Use of Subqueries To build a dynamic WHERE clause like above: WHERE Column IN (Subquery) To reference a derived table SELECT lastname FROM (SELECT * FROM Employees WHERE ) AS E –E is a data set returned to the outer query and can now be used for other SQL functions Example: Find all orders for items from France that were handled by sales employees and were sold to customers in France

ORT Braude, CSE 61309, ©2004 Gary Schloss Solution 1: use three subqueries SELECT order# FROM (SELECT * FROM Employees WHERE title LIKE ‘Sales %’) E JOIN (SELECT * FROM Orders WHERE shipcountry = ‘France’) O ON E.emp# = O.emp# JOIN (SELECT * FROM Customers WHERE country = ‘France’) C ON O.cust# = C.cust#; Solution 2: use joins SELECT order# FROM Orders O JOIN Employees E ON E.emp# = O.emp# JOIN Customers C ON O.cust# = C.cust# WHERE E.title LIKE ‘Sales %’ AND O.shipcountry = ‘France’ AND C.country = ‘France’;

ORT Braude, CSE 61309, ©2004 Gary Schloss Solution 3: use one subquery SELECT S.order# FROM (SELECT O.order#, E.title, C.country, O.shipcountry FROM Orders O JOIN Employees E ON E.emp# = O.emp# JOIN Customers C ON O.cust# = C.cust#) S WHERE S.title LIKE ‘Sales %’ AND S.shipcountry = ‘France’ AND S.country = ‘France’; For all three solutions, the Query Analyzer will generate exactly the same query plan. So, which approach is best? Answer: It Depends…

ORT Braude, CSE 61309, ©2004 Gary Schloss A Few Useful Hints To count number of tuples, use: SELECT COUNT(*) AS RowCount FROM table; SELECT COUNT(DISTINCT ProdName) FROM table; If in a subquery a dynamically determined data set may return empty, use: IF EXISTS (SELECT * FROM table WHERE ) IF NOT EXISTS (SELECT * FROM table WHERE ) The order in which an SQL statement is evaluated: (1) FROM (2) WHERE; (3) Aggregate Functions (MAX, MIN, SUM, AVG, COUNT) and GROUP BY (4) HAVING (5) ORDER BY (6) SELECT