 Database is SQL1.mdb ◦ import using MySQL Migration Toolkit 

Slides:



Advertisements
Similar presentations
A Guide to SQL, Seventh Edition. Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables.
Advertisements

N.G.Acharya & D.K.Marathe college Chembur-E, Mumbai-71
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.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
Aggregate Functions Presenter: Sushma Bandekar CS 157(A) – Fall 2006.
Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs WHERE SUM (duration) = 100; (this will give an error)
SQL Subqueries Objectives of the Lecture : To consider the general nature of subqueries. To consider simple versus correlated subqueries. To consider the.
SQL (2).
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Chapter 6 Set Functions.
Chapter 11 Group Functions
The University of Akron Dept of Business Technology Computer Information Systems The Relational Model: Query-By-Example (QBE) 2440: 180 Database Concepts.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 6: Set Functions.
Introduction to Oracle9i: SQL1 SQL Group Functions.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 7: Aggregates.
Chapter 6 SQL: Data Manipulation Cont’d. 2 ANY and ALL u ANY and ALL used with subqueries that produce single column of numbers u ALL –Condition only.
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
Microsoft Access 2010 Chapter 7 Using SQL.
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.
Concepts of Database Management, Fifth Edition
Chapter 6 Group Functions. Chapter Objectives  Differentiate between single-row and multiple-row functions  Use the SUM and AVG functions for numeric.
SQL Queries and Subqueries Zaki Malik September 04, 2008.
1 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
A Guide to MySQL 5. 2 Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables Use a subquery.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) Structured Query Language Introduction to SQL Structured Query Language.
Instructor: Jinze Liu Fall Basic Components (2) Relational Database Web-Interface Done before mid-term Must-Have Components (2) Security: access.
Using Special Operators (LIKE and IN)
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.
1 Pertemuan > > Matakuliah: >/ > Tahun: > Versi: >
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.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)
Structured Query Language Introduction. Basic Select SELECT lname, fname, phone FROM employees; Employees Table LNAMEFNAMEPHONE JonesMark SmithSara
IST 210 SQL Todd Bacastow IST 210: Organization of Data.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
1 IT420: Database Management and Organization SQL part 3 7 February 2006 Adina Crăiniceanu
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: 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.
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 5 SQL.
Course title: Database-ii Chap No: 03 “Advanced SQL” Course instructor: ILTAF MEHDI.
1 SY306 Web and Databases for Cyber Operations Set #13: SQL SELECT Grouping and sub-queries.
Query Processing – Implementing Set Operations and Joins Chap. 19.
Drill Consider the following tables with the following fields: Student: FName, LName, StudentID, Age, Yr, Course Grades: ID, P1, P2, P3 1.Display the.
Agenda for Class - 03/04/2014 Answer questions about HW#5 and HW#6 Review query syntax. Discuss group functions and summary output with the GROUP BY statement.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
SQL: Interactive Queries (2) Prof. Weining Zhang Cs.utsa.edu.
SQL: Additional Notes. 2 Example 5.3 Use of DISTINCT List the property numbers of all properties that have been viewed. SELECT propertyNo FROM Viewing;
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Chapter 11 – Data Manipulation: Relational Algebra and SQL1 Unit 9: Data Manipulation: Relational Algebra and SQL IT238: Data Modeling and Database Design.
Chapter 11 SQL: Data Manipulation
Slides are reused by the approval of Jeffrey Ullman’s
CS580 Advanced Database Topics
Chapter 3 Introduction to SQL(3)
Working with Tables: Join, Functions and Grouping
SQL : Query Language Part II CS3431.
Chapter Name SQL: Data Manipulation
SQL – Entire Select.
Reporting Aggregated Data Using the Group Functions
Contents Preface I Introduction Lesson Objectives I-2
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

 Database is SQL1.mdb ◦ import using MySQL Migration Toolkit 

 Based on tables used in ◦ “The Essence of SQL”, by David Rozenshtein ◦ Five tables  STUDENT  PROFESSOR  COURSE  TAKE  TEACH ◦ Three types of SQL queries ◦ Eighteen standard questions  “Essence” discusses methodology, however does not provide any data ◦ SQL1 database contains data according to the book’s table definitions ◦ Data is ‘salted’ (known results for the queries)

 Can be lumped into six categories ◦ Type I, II, or III categories  Type I: single level queries  Type II: nested, no correlations  Type III: nested, with correlations ◦ Aggregation  sums, averages, grouping ◦ Enhancements  sorting ◦ Extensions  WHILE loop

 E1.Who takes CS112?  E2.What are the student names and numbers of students taking CS112?  E3.Who takes CS112 or CS114?  E4.Who take both CS112 and CS114?  E5.Who does not take CS112?  E6.Who takes a course which is not CS112?  E7.Who takes at least two courses?  E8.Who takes at most two courses?  E9.Who takes exactly two courses?  E10. Who takes only CS112?  E11. Who takes either CS112 or CS114? (not both)  E12. Who are the youngest students?  E13. Who takes every course?

 Type 1 queries ◦ basic form has three clauses  SELECT  FROM  WHERE  E1 (Who takes CS112?) SELECT sno FROM Take WHERE (cno=“CS112”);  E2 (Who takes CS112? – show name and number) SELECT Student.sno, Sname FROM Student,Take WHERE (Student.sno=Take.sno) AND (cno=“CS112”);

 Who takes CS112 or CS114?

 “distinct” keyword requires sorting ◦ performance hit  rewrite the WHERE clause to use list membership operator “IN” ◦ IN returns true is the value on the left is equal to one of the value list members on the right  WHERE (cno IN(“CS112”,”CS114”))  The “IN” operator is important for Type II queries!

 Who takes both CS112 and CS114? SELECT sno FROM take WHERE (cno=“CS112”) and (cno=“CS114”);  Why is this wrong? ◦ it will compile and return an answer… so.. why? ◦ because it tries to find any entry for which the course number is CS112 and CS114…  it can only be one at a time… ◦ translation from English to SQL needs to be considered  rephrase the question?  reverse engineer the problem?

 The information resides in the ‘take’ table ◦ find students who take CS112 (who takes one) ◦ find students who take CS114 (who take the other) ◦ determine if it’s the same student (where are they equal)  Ideally use another ‘copy’ of the take table SELECT take.sno FROM take, take WHERE (take.sno=take.sno) AND (take.cno=“CS112”) and (take.cno=“CS114”);  however…. how does the compiler know which take table to use when??? ◦ Develop an ‘alias’ for one of the take tables, and then use the alias name!

 Alias ◦ created in the FROM clause and used only for the duration of the query SELECT X.sno FROM take X, take WHERE (X.sno=take.sno) AND (X.cno=“CS112”) and (take.cno=“CS114”);

 E5: Who does not take CS112?  E6: Who takes a course which is not CS112? Included in E5?Included in E6? Student takes both 112 and 114 NoYes Student takes nothingYesNo Only takes 112No Only takes 114Yes

 Who takes a course which is not CS112? ◦ (student will be selected even if takes CS112 in addition to another course) SELECT sno FROM take WHERE (cno!=“CS112”);

 Who does not take CS112? ◦ A Type II query ◦ done in two passes, not one (subquery)  1 st pass  who takes CS112?  2 nd pass  who is not in the results of the first pass?

 Who takes at least two classes? ◦ Last of the Type I queries ◦ Use two “copies” of the TAKE table  See who exists in both copies, but have DIFFERENT classes  The student number the same, but differ class numbers SELECT x.sno FROM take x, take y Where (x.sno=y.sno) AND (x.cno != y.cno);

 Questions 1,2,3,4,6,7 can be answered with Type I queries ◦ Question 5 is a Type II query and cannot be answered with a Type I query

 E8 through E11  E8: Who takes at most two courses? SELECT sno FROM student WHERE NOT (sno IN (Select x.sno From Take x, Take y, Take Where (x.sno=y.xno) And (y.sno=take.sno) And (x.cno!=y.cno) And (y.cno !=take.cno) And (x.cno!=take.cno)));

 Who takes exactly two courses ◦ Who takes at least two courses and does not take at least three courses? Note: 1)“x” alias used twice in main and subquery, but “scope” keeps them separate 2)The “>” used in the where clause is used to eliminate duplicates

 Who takes only CS112? ◦ Who takes CS112 and is not in the group of who does not take CS112? SELECT sno FROM take WHERE (cno=“CS112”) AND NOT (sno IN (SELECT sno FROM take WHERE (cno != “CS112”))); Alternate: simplified query

 Who take either CS112 or CS114? ◦ But not both… ◦ Find who takes both and from that  Find who is NOT in that group, but takes CS112 or CS114 SELECT sno FROM take WHERE ((cno=“CS112”) or (cno=“CS114”)) AND NOT (sno IN (SELECT x.sno FROM take x, take WHERE (x.sno=take.sno) AND (x.cno=“CS112) AND (take.cno=“CS114))); NOTE: extra parens around the “OR” In the WHERE clause. This means that the OR is executed first;. Normal Precedence: NOT, AND OR

 E12: Who are the youngest students? ◦ The min( ) function can be used or a TYPE II can do it as well SELECT sno FROM student WHERE NOT (age IN (SELECT x.age FROM student x, student WHERE (x.age > student.age)));  The subquery returns all ages if there is age smaller than it ◦ It excludes the smallest age  Then the main query retrieves the students who are not in that group

 Who takes every course? ◦ Note the use of the CONCAT( ) function  Allows us to make up a new “field” for the duration of the query  Saves comparing multiple fields

 Local reference in each level of the query ◦ This makes it a TYPE II  Innermost sno,cno comes from TAKE  Middle sno comes from cross-product of STUDENT,COURSE  Outermost sno comes from STUDENT

 For each department that has more than 2 professors older than 40, what is the average salary of these professors? SELECT dept,avg(salary) FROM professor WHERE (age>40) GROUP BY dept HAVING (COUNT(*)>2);

 Count(*) from professor ◦ Counts the rows  Count(Distinct(dept)) from professor ◦ Counts how many unique values exist  Count would return  When using aggregate functions (min, max, avg, sum, count, etc) you cannot have a non- aggregated column in the SELECT or HAVING clause unless it’s already aggregated in the GROUP BY clause

 What is the GPA of each student? Select sno, GPA=round(SUM(Grade*credits)/Sum(credits),2) FROM take,course WHERE (take.cno=course.cno) GROUP BY sno;

 TYPE I question involving “at least, at most, exactly” can be performed using aggregate SELECT sno FROM take GROUP BY sno HAVING (count(*)>=3);

 What is the overall average salary of all professors older than 40? SELECT AVG(salary) FROM professor WHERE (age>40);  GROUP BY clause not needed ◦ Implicit ‘single group’ of all professors over 40

 Which professor’s salary is greater than the overall average salary? SELECT x.fname,x.lname FROM professor x, professor y GROUP BY x.fname,x.lname,x.salary HAVING (x.salary > avg(y.salary));

 Which professor’s salary is greater than his/her department’s average salary? SELECT x.fname,x.lname FROM professor x,professor y WHERE (x.dept=y.dept) GROUP BY x.fname,x.lname,x.salary HAVING (x.salary > avg(y.salary));

 Sample Type III SELECT sno,sname FROM student WHERE (“CS112” IN (SELECT cno FROM take WHERE (sno=student.sno)));  What makes this a Type III ? ◦ The reference in the inner where [take.]sno = student.sno ◦ “reaches” outside the scope of the inner query ◦ It is a “non-local reference” or “correlation”  It’s E2… What are the student numbers and names of the students taking CS112?

 Who teaches CS112?

 Who takes CS112? ◦ Using the EXISTS operator  Returns TRUE if at least one row returned by subquery SELECT sno,sname FROM student WHERE EXISTS (SELECT * FROM take WHERE (sno=student.sno) AND (cno=“CS112”)); EXISTS requires usage of the “*” in the select subquery