1 Schema for Student Registration System Student Student (Id, Name, Addr, Status) Professor Professor (Id, Name, DeptId) Course Course (DeptId, CrsCode,

Slides:



Advertisements
Similar presentations
CSCI 4333 Database Design and Implementation Review for Midterm Exam I
Advertisements

TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
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.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
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.
E-R Diagram for a Banking Enterprise
1 Relational Algebra and SQL Chapter 6. 2 Relational Query Languages Languages for describing queries on a relational database Structured Query LanguageStructured.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 90 Database Systems I SQL Queries.
FALL 2004CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
Nov 24, 2003Murali Mani SQL B term 2004: lecture 12.
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.
SQL Neyha Amar CS 157A, Fall Inserting The insert statement is used to add a row of data into a table Strings should be enclosed in single quotes,
Relational Algebra and SQL Prof. Sin-Min Lee Department of Computer Science.
--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.
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.
INFS614 - Lecture week 9 1 More on SQL Lecture Week 9 INFS 614, Fall 2008.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
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 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
1 CSE 480: Database Systems Lecture 11: SQL. 2 SQL Query SELECT FROM WHERE –In MySQL, FROM and WHERE clauses are optional –Example:
Relational Algebra - Chapter (7th ed )
Instructor: Jinze Liu Fall Basic Components (2) Relational Database Web-Interface Done before mid-term Must-Have Components (2) Security: access.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
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.
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.
From Relational Algebra to SQL CS 157B Enrique Tang.
1 Chapter 5 Relational Algebra and SQL. 2 Relational Query Languages Languages for describing queries on a relational database Structured Query LanguageStructured.
INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012.
1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)
Chapter 5 Relational Algebra and SQL. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 5-2 Relational Query Languages Languages for describing.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
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.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Query Processing – Implementing Set Operations and Joins Chap. 19.
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples. v If the.
1 CSE 480: Database Systems Lecture 16: Relational Algebra.
SQL: Structured Query Language Instructor: Mohamed Eltabakh 1 Part II.
CSCI 6315 Applied Database Systems Review for Final Exam Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX
SQL: Interactive Queries (2) Prof. Weining Zhang Cs.utsa.edu.
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.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Database Systems Chapter 6
More SQL: Complex Queries,
Chapter 3 Introduction to SQL
COP Introduction to Database Structures
CS 480: Database Systems Lecture 12 February 11, 2013.
Chapter # 6 The Relational Algebra and Calculus
Introduction to SQL.
Chapter 3 Introduction to SQL(3)
Relational Algebra and SQL
Schedule Today: Next After that Subqueries, Grouping and Aggregation.
SQL: Structured Query Language DML- Queries Lecturer: Dr Pavle Mogin
SQL Structured Query Language 11/9/2018 Introduction to Databases.
SQL : Query Language Part II CS3431.
CS 405G: Introduction to Database Systems
More SQL: Complex Queries, Triggers, Views, and Schema Modification
SQL: Structured Query Language
CS 405G: Introduction to Database Systems
Presentation transcript:

1 Schema for Student Registration System Student Student (Id, Name, Addr, Status) Professor Professor (Id, Name, DeptId) Course Course (DeptId, CrsCode, CrsName, Descr) Transcript Transcript (StudId, CrsCode, Semester, Grade) Teaching Teaching (ProfId, CrsCode, Semester) Department Department (DeptId, Name)

2 Query Sublanguage of SQL Tuple variable Course Tuple variable C ranges over rows of Course. Evaluation strategy: – FROM clause produces Cartesian product of listed tables – WHERE clause assigns rows to C in sequence and produces table containing only rows satisfying condition – SELECT clause retains listed columns Course Equivalent to:  CrsName  DeptId=‘CS’ (Course) SELECT C.CrsName Course FROM Course C WHERE C.DeptId = ‘CS’

3 Join Queries List CS courses taught in S2000 Tuple variables clarify meaning. Join condition “C.CrsCode=T.CrsCode” – relates facts to each other Selection condition “ T.Semester=‘S2000’ ” – eliminates irrelevant rows Equivalent (using natural join) to: SELECT C.CrsName CourseTeaching FROM Course C, Teaching T WHERE C.CrsCode=T.CrsCode AND T.Semester=‘S2000’ Course  CrsName (Course Teaching  Semester=‘S2000’ (Teaching) ) CourseTeaching  CrsName (  Sem=‘S2000’ (Course Teaching) )

4 Correspondence Between SQL and Relational Algebra SELECT C.CrsName CourseTeaching FROM Course C, Teaching T WHERE C.CrsCode = T.CrsCode AND T.Semester = ‘S2000’ Also equivalent to:  CrsName  C_ CrsCode=T_CrsCode AND Semester=‘S2000’ Course (Course [C_CrsCode, DeptId, CrsName, Desc ] Teaching  Teaching [ ProfId, T_CrsCode, Semester ]) This is the simplest evaluation algorithm for SELECT. Relational algebra expressions are procedural.  Which of the two equivalent expressions is more easily evaluated?

5 Self-join Queries Find Ids of all professors who taught at least two courses in the same semester: SELECT T1.ProfId TeachingTeaching FROM Teaching T1, Teaching T2 WHERE T1.ProfId = T2.ProfId AND T1.Semester = T2.Semester AND T1.CrsCode <> T2.CrsCode Tuple variables are essential in this query! Equivalent to: Teaching  ProfId (  T1.CrsCode  T2.CrsCode (Teaching[ProfId, T1.CrsCode, Semester] Teaching Teaching[ProfId, T2.CrsCode, Semester]))

6 Duplicates Duplicate rows not allowed in a relation However, duplicate elimination from query result is costly and not done by default; must be explicitly requested: SELECT DISTINCT ….. FROM …..

7 Equality and comparison operators apply to strings (based on lexical ordering) WHERE S.Name < ‘P’ Use of Expressions Concatenate operator applies to strings WHERE S.Name || ‘--’ || S.Address = …. Expressions can also be used in SELECT clause : SELECT S.Name || ‘--’ || S.Address AS NmAdd Student FROM Student S

8 Set Operators SQL provides UNION, EXCEPT (set difference), and INTERSECT for union compatible tables Example: Find all professors in the CS Department and all professors that have taught CS courses ( SELECT P.Name ProfessorTeaching FROM Professor P, Teaching T WHERE P.Id=T.ProfId AND T.CrsCode LIKE ‘CS%’) UNION (SELECT P.Name Professor FROM Professor P WHERE P.DeptId = ‘CS’)

9 Nested Queries List all courses that were not taught in S2000 SELECT C.CrsName Course FROM Course C WHERE C.CrsCode NOT IN (SELECT T.CrsCode --subquery Teaching FROM Teaching T WHERE T.Sem = ‘S2000’) Evaluation strategy: subquery evaluated once to produces set of courses taught in S2000. Each row (as C) tested against this set.

10 Correlated Nested Queries Output a row if prof has taught a course in dept. (SELECT T.ProfId --subquery TeachingCourse FROM Teaching T, Course C WHERE T.CrsCode=C.CrsCode AND C.DeptId=D.DeptId --correlation ) SELECT P.Name, D.Name --outer query ProfessorDepartment FROM Professor P, Department D WHERE P.Id IN -- set of all ProfId’s who have taught a course in D.DeptId

11 Correlated Nested Queries (con’t) Tuple variables T and C are local to subquery Tuple variables P and D are global to subquery Correlation Correlation: subquery uses a global variable, D The value of D.DeptId parameterizes an evaluation of the subquery Subquery must (at least) be re-evaluated for each distinct value of D.DeptId Correlated queries can be expensive to evaluate

12 Division in SQL Query type: Find the subset of items in one set that are related to all items in another set Example: Find professors who taught courses in all departments – Why does this involve division? ProfId DeptIdDeptId All department Ids Contains row if professor p taught a course in department d  ProfId,DeptId (Teaching Course) /  DeptId (Department)

13 Division in SQL Strategy for implementing division in SQL: – Find set, A, of all departments in which a particular professor, p, has taught a course – Find set, B, of all departments – Output p if A  B, or, equivalently, if B–A is empty

14 Division – SQL Solution SELECT P.Id Professor FROM Professor P WHERE NOT EXISTS (SELECT D.DeptId -- set B of all dept Ids Department FROM Department D EXCEPT SELECT C.DeptId -- set A of dept Ids of depts in -- which P taught a course TeachingCourse FROM Teaching T, Course C WHERE T.ProfId=P.Id -- global variable AND T.CrsCode=C.CrsCode)  ProfId,DeptId (Teaching Course) /  DeptId (Department)

15 Aggregates Functions that operate on sets: – COUNT, SUM, AVG, MAX, MIN Produce numbers (not tables) Not part of relational algebra (but not hard to add) SELECT COUNT(*) Professor FROM Professor P SELECT MAX (Salary) Employee FROM Employee E

16 Aggregates (cont’d) SELECT COUNT ( T.CrsCode ) Teaching FROM Teaching T WHERE T.Semester = ‘S2000’ SELECT COUNT (DISTINCT T.CrsCode ) Teaching FROM Teaching T WHERE T.Semester = ‘S2000’ Count the number of courses taught in S2000 But if multiple sections of same course are taught, use:

17 Grouping But how do we compute the number of courses taught in S2000 per professor? – Strategy 1: Fire off a separate query for each professor: SELECT COUNT( T.CrsCode ) Teaching FROM Teaching T WHERE T.Semester = ‘S2000’ AND T.ProfId = Cumbersome What if the number of professors changes? Add another query? grouping operator – Strategy 2: define a special grouping operator: SELECT T.ProfId, COUNT( T.CrsCode ) Teaching FROM Teaching T WHERE T.Semester = ‘S2000’ GROUP BY T.ProfId

18 GROUP BY

19 GROUP BY - Example SELECT T.StudId, AVG( T.Grade ), COUNT (*) Transcript FROM Transcript T GROUP BY T.StudId Transcript Attributes: –student’s Id –avg grade –number of courses

20 HAVING Clause Eliminates unwanted groups (analogous to WHERE clause, but works on groups instead of individual tuples ) HAVING condition is constructed from attributes of GROUP BY list and aggregates on attributes not in that list SELECT T.StudId, AVG (T.Grade) AS CumGpa, COUNT (*) AS NumCrs Transcript FROM Transcript T WHERE T.CrsCode LIKE ‘CS%’ GROUP BY T.StudId HAVING AVG (T.Grade) > 3.5

21 Evaluation of GroupBy with Having

22 Example Output the name and address of all seniors on the Dean’s List SELECT S.Id, S.Name StudentTranscript FROM Student S, Transcript T WHERE S.Id = T.StudId AND S.Status = ‘senior’ GROUP BY HAVING AVG (T.Grade) > 3.5 AND SUM (T.Credit) > 90 S.Id -- wrong S.Id, S.Name -- right Every attribute that occurs in SELECT clause must also occur in GROUP BY or it must be an aggregate. S.Name does not.

23 Aggregates: Proper and Improper Usage SELECT COUNT (T.CrsCode), T. ProfId – makes no sense (in the absence of GROUP BY clause) SELECT COUNT (*), AVG (T.Grade) – but this is OK WHERE T.Grade > COUNT (SELECT ….) – aggregate cannot be applied to result of SELECT statement

24 ORDER BY Clause Causes rows to be output in a specified order SELECT T.StudId, COUNT (*) AS NumCrs, AVG (T.Grade) AS CumGpa Transcript FROM Transcript T WHERE T.CrsCode LIKE ‘CS%’ GROUP BY T.StudId HAVING AVG (T.Grade) > 3.5 ORDER BY DESC CumGpa, ASC StudId Descending Ascending

25 Query Evaluation with GROUP BY, HAVING, ORDER BY 1Evaluate FROM : produces Cartesian product, A, of tables in FROM list 2Evaluate WHERE : produces table, B, consisting of rows of A that satisfy WHERE condition 3Evaluate GROUP BY : partitions B into groups that agree on attribute values in GROUP BY list 4Evaluate HAVING : eliminates groups in B that do not satisfy HAVING condition 5Evaluate SELECT : produces table C containing a row for each group. Attributes in SELECT list limited to those in GROUP BY list and aggregates over group 6Evaluate ORDER BY : orders rows of C A s b e f o r e