SQL (cont’d) CSE3330 Spring 2014 Chengkai Li. NULL values.

Slides:



Advertisements
Similar presentations
COMPANY schema EMPLOYEE
Advertisements

Basic Queries. 2 Retrieval Queries in SQL SQL has one basic statement for retrieving information from a database; the SELECT statement This is not the.
SQL Query Slides Sharif University Of Technology Database Systems CE 384 Prepared By: Babak Bagheri Hariri
Your Logo Fundamentals of Database Systems Fourth Edition El Masri & Navathe Instructor: Mr. Ahmed Al Astal Chapter 8 (Cont.) SQL-99: Schema Definition,
FEN More about SELECT, Nested selects GROUP BY, HAVING, ORDER BY Other joins Aggregate functions Views More about SQL.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.
1 CSE 480: Database Systems Lecture 10: SQL - DML Reference: Read Chapter 4 of the textbook.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2- 1.
Database Systems Chapter 8 ITM 354. Data Definition, Constraints, and Schema Changes Used in SQL to Create, Drop, and Alter the descriptions of the tables.
More SQL: Complex Queries, Triggers, Views, and Schema Modification 1.
관계 연산자 & SQL. Selection SELECT * FROM r WHERE A=B AND D>5.
SQL The relational DB Standard CS-450 Dr. Ali Obaidi.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1 CREATE/DROP/ALTER TABLE Data types : char, varchar, decimal, date CREATE TABLE DEPARTMENT.
Al-Imam University Girls Education Center Collage of Computer Science 1 ST Semester, 1432/1433H Chapter 8 Part 4 SQL-99 Schema Definition, Constraints,
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
FEN  Queries: SELECT  Data Manipulation: INSERT, UPDATE, DELETE SQL: Structured Query Language – Part 2.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: Constraints, and Queries and Views.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc.
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:
SQL: Schema Definition, Constraints, & Queries and Views.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Lecture # 11 July 12,2012 More SQL: Complex Queries, Triggers,
Relational Algebra - Chapter (7th ed )
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide
Slide Chapter 8 SQL: SchemaDefinition, Constraints, and Queries and Views.
 Employee (fname, minit, lname, ssn, bdate, address, sex, salary, superssn, dno)  Department (dname, dnumber, mgrssn, mgrstartdate) 
Database Management Systems. NESTING OF QUERIES  Some queries require that existing values in the database be retrieved and then used in a comparison.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
Announcements Written Homework 1 due Friday –If you have fourth edition make sure you do the right problems Program 3 out today, due next Friday Nov 10.
Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
莊裕澤資料庫基本概念 1 Yuh-Jzer Joung 莊 裕 澤 Dept. of Information Management National Taiwan University February, 2002 資料庫管理與資訊科技.
SQL-99: SchemaDefinition, Constraints, and Queries and Views.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
Structured Query Language (2) The main reference of this presentation is the textbook and PPT from : Elmasri & Navathe, Fundamental of Database Systems,
Announcements Written Homework 1 due Nov 2 –See course web page –Exercises 5.12, 5.15, 6.17, 6.20, 6.22 (a,c,f only). Today –SQL (chapter 8) –Instructional.
Chapter 11 SQL DML. Retrieval Queries in SQL SQL has one basic statement for retrieving information from a database; the SELECT statement This is not.
Announcements Written Homework 1 due Nov 2 –See course web page –Exercises 5.12, 5.15, 6.17, 6.20, 6.22 (a,c,f only). Today –continue with SQL (chapter.
Chapter SQL DML. Retrieval Queries in SQL SQL has one basic statement for retrieving information from a database; the SELECT statement This is not the.
SQL-99: SchemaDefinition, Constraints, and Queries and Views.
FEN Introduction to the database field:  SQL: Structured Query Language Seminar: Introduction to relational databases.
COMP163 Database Management Systems September 18, 2008 Lecture 8 – Sections SQL Queries.
ISC321 Database Systems I Chapter 5: SQL: Advanced Queries, Assertions, Triggers, and Views Fall 2015 Dr. Abdullah Almutairi.
SQL Aggregeringsfunktioner. AGGREGATE FUNCTIONS Include COUNT, SUM, MAX, MIN, and AVG Query 15: Find the maximum salary, the minimum salary, and the average.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 (SQL Basics) Lecture # 9 July 7,2012.
Lecture # 25 Chapter 8 SQL-99: Schema Definition, Constraints, and Queries and Views Muhammad Emran Database Systems.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Lecture # 10 July 10,2012 More SQL: Complex Queries, Triggers,
CS580 Advanced Database Topics Chapter 8 SQL Irena Pevac.
CS580 Advanced Database Topics
6/22/2018.
Retrieval Queries in SQL(DML)
More SQL: Complex Queries, Triggers, Views, and Schema Modification
376a. Database Design Dept. of Computer Science Vassar College
More SQL: Complex Queries, Triggers, Views, and Schema Modification
SQL-99: Schema Definition, Constraints, and Queries and Views
SQL Grouping, Ordering & Arithmetics Presented by: Dr. Samir Tartir
SQL: Set Operations & Nested Queries. Presented by: Dr. Samir Tartir
Presentation transcript:

SQL (cont’d) CSE3330 Spring 2014 Chengkai Li

NULL values

NULL values are all different The following query looks for employees who earn the same as their supervisors. If the database does not contain salary information of an employee and the supervisor (null values), the employee doesn’t appear in query result. (Which is what we shall expect.) SELECT E1.fname, E1.lname FROM EMPLOYEE AS E1, EMPLOYEE AS E2 WHERE E1.superssn=E2.ssn AND E1.salary= E2.salary;

Except that NULL values are all equal in UNION/INTERSECT/EXCEPT/DISTINCT/GROUP BY SELECT COUNT(*) FROM ( SELECT E.superssn FROM EMPLOYEE AS E, DEPARTMENT AS D WHERE E.dno=D.dnumber AND D.dname=‘research’ INTERSECT SELECT E.superssn FROM EMPLOYEE AS E, WORKS_ON AS W WHERE E.ssn=W.essn AND W.hours > 30 ) AS T; EMPLOYEE DEPARTMENT WORKS_ON ssn superssn dno dnumber dname essn proj hours NULL 1 1 research NULL 2 2 sales

Except that NULL values are all equal in UNION/INTERSECT/EXCEPT/DISTINCT/GROUP BY All employees with “null” salary values belong to a group. SELECT salary, COUNT(*) FROM EMPLOYEE GROUP BY salary;

Aggregation Take out null values, before aggregates are calculated. (except COUNT(*)) Suppose there are null values in “salary”. SELECT AVG(salary) FROM EMPLOYEE; is different from SELECT SUM(salary)/COUNT(*) FROM EMPLOYEE; SELECT COUNT(salary) FROM EMPLOYEE; is different from SELECT COUNT(*) FROM EMPLOYEE;

The confusing DISTINCT (at least in MySQL) SELECT a, b FROM T; a b NULL SELECT DISTINCT a, b FROM T; a b NULL SELECT COUNT (DISTINCT a, b) FROM T; COUNT (DISTINCT a, b) NULL values are all equal in DISTINCT, without COUNT. They are ignored in COUNT (DISTINCT …)

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8 Subqueries

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9 Subqueries A parenthesized SELECT statement (subquery) in another SELECT statement. Can be in FROM clause Can be in WHERE clause

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Subquery in FROM In place of a relation in the FROM clause, we can place another query, and then query its result. use a tuple-variable to name tuples of the result. Subquery in FROM is not mentioned in textbook, but is useful. The SQL syntax on page 140 needs revision to include subquery in FROM SELECT FROM ( SELECT... FROM … [WHERE …] [ GROUP BY … [HAVING …] ] [ORDER BY …] ) AS … [WHERE …] [ GROUP BY … [HAVING …]] [ORDER BY …] 10

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Example SELECT S.sid, S.name, R.semester, R.year FROM students AS S, ( SELECT * FROM register WHERE grade=‘A’ ) AS R WHERE S.sid=R.sid SELECT MAX(total) FROM ( SELECT sid, COUNT(*) as total FROM register GROUP BY sid ) AS R 11

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Subquery in WHERE Subqueries in WHERE are also called nested queries IN EXISTS > / = / ANY/ALL 12

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley SELECT statements embedded in other statements UNION, INTERSECT, EXCEPT Q4:(SELECT PNAME FROMPROJECT, DEPARTMENT, EMPLOYEE WHEREDNUM=DNUMBER AND MGRSSN=SSN AND LNAME='Smith') UNION (SELECT PNAME FROMPROJECT, WORKS_ON, EMPLOYEE WHEREPNUMBER=PNO AND ESSN=SSN AND LNAME='Smith') 13

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Correlated Subqueries Subqueries in FROM must not be correlated SELECT * FROM Dependents AS D, ( SELECT * FROM Employee AS E WHERE E.SSN=D.ESSN ) AS R Nested queries (Subqueries in WHERE) can be correlated 14

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Nested Queries can be deep SELECTPNO FROMWORKS_ON WHEREESSN IN (SELECT SSN FROMEMPLOYEE WHERELNAME=‘Smith’ AND DNO IN (SELECT DNUMBER FROMDEPARTMENT WHEREDNAME= ‘Research’) ); 15

MySQL doesn’t provide EXCEPT/MINUS and INTERSECT

A nice write-up at hing/csci4333fall2013/slides/MySQL-set- operators.pdf hing/csci4333fall2013/slides/MySQL-set- operators.pdf However, complexity due to null value is not considered. Bag semantics is not considered either.

Rewrite EXCEPT and INTERSECT SELECT ssn FROM EMPLOYEE WHERE salary < EXCEPT SELECT essn FROM WORKS_ON WHERE hours > 30; Rewrite this query using IN, EXISTS, LEFT OUTER JOIN

Rewrite EXCEPT by IN SELECT ssn FROM EMPLOYEE WHERE salary < EXCEPT SELECT essn FROM WORKS_ON WHERE hours > 30; SELECT ssn FROM EMPLOYEE WHERE salary < AND ssn NOT IN (SELECT essn FROM WORKS_ON WHERE hours > 30);

Rewrite EXCEPT by EXISTS SELECT ssn FROM EMPLOYEE WHERE salary < EXCEPT SELECT essn FROM WORKS_ON WHERE hours > 30; SELECT ssn FROM EMPLOYEE WHERE salary < AND NOT EXISTS (SELECT essn FROM WORKS_ON WHERE hours > 30 AND essn=ssn);

Rewrite EXCEPT by LEFT OUTER JOIN SELECT ssn FROM EMPLOYEE WHERE salary < EXCEPT SELECT essn FROM WORKS_ON WHERE hours > 30; Incorrect rewriting: SELECT ssn FROM EMPLOYEE LEFT OUTER JOIN WORKS_ON ON ssn=essn WHERE salary < AND hours <= 30; WORKS_ON EMPLOYEE essn pno hours ssn salary

Rewrite EXCEPT by LEFT OUTER JOIN Correct rewriting: SELECT ssn FROM ((SELECT ssn FROM EMPLOYEE WHERE salary < 10000) AS R LEFT OUTER JOIN (SELECT essn FROM WORKS_ON WHERE hours > 30) AS T ON R.ssn=T.essn) WHERE T.essn IS NULL; WORKS_ON EMPLOYEE R T R left outer join T essn pno hours ssn salary ssn essn ssn essn