6/22/2018.

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,
Displaying Data from Multiple Tables. EMPNO DEPTNO LOC NEW YORK CHICAGO NEW YORK DALLAS
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
Data Warehousing/Mining 1 Data Warehousing/Mining Comp 150 Aggregation in SQL (not in book) Instructor: Dan Hebert.
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.
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.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: Advanced Queries.
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.
Relational Algebra - Chapter (7th ed )
Structured Query Language. Group Functions What are group functions ? Group Functions Group functions operate on sets of rows to give one result per group.
 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.
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.
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.
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 資料庫管理與資訊科技.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Chapter 7 SQL HUANG XUEHUA. AGGREGATE FUNCTIONS Include COUNT, SUM, MAX, MIN, and AVG Query 15: Find the maximum salary, the minimum salary, and the.
Chapter 8 Part 2 SQL-99 Schema Definition, Constraints, Queries, and Views.
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,
DatabaseIM ISU1 Fundamentals of Database Systems Chapter 6 The Relational Algebra.
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.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
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.
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.
1 Database Systems SQL: Advanced Queries. 2 Union, Intersection, and Except (2)   Find the names of those people who are either a graduate student or.
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.
1 Chapter (8) SQL The Relational Database Standard Objectives Presenting the main features of SQL SQL is the Structured Query Language SQL commands SQL.
More SQL: Complex Queries,
CS580 Advanced Database Topics
SQL: SchemaDefinition, Constraints, and Queries and Views
Chapter 4 Basic SQL.
CS580 Advanced Database Topics
Retrieval Queries in SQL(DML)
Subject Name: DATA BASE MANAGEMENT SYSTEM Subject Code: 10cs54
More SQL: Complex Queries, Triggers, Views, and Schema Modification
376a. Database Design Dept. of Computer Science Vassar College
11/9/2018.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Session - 6 Sequence - 2 SQL: The Structured Query Language:
Chapter 4 & 5 Basic SQL More SQL
More SQL: Complex Queries, Triggers, Views, and Schema Modification
SQL (more).
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:

6/22/2018

More Complex SQL Queries Chapter 5 More Complex SQL Queries

NULLS IN SQL QUERIES SQL allows queries that check if a value is NULL (missing or undefined or not applicable) SQL uses IS or IS NOT to compare NULLs because it considers each NULL value distinct from other NULL values, so equality comparison is not appropriate . Query 1: Retrieve the names of all employees who do NOT have supervisors. Q1: SELECT Fname, Lname FROM EMPLOYEE WHERE Super_ssn IS NULL; Note: If a join condition is specified, tuples with NULL values for the join attributes are not included in the result

NESTING OF QUERIES A complete SELECT query, called a nested query , can be specified within the WHERE-clause of another query, called the outer query Many of the previous queries can be specified in an alternative form using nesting Query 2: Retrieve the names and addresses of all employees who work for the 'Research' department. Q2: SELECT Fname, Lname, Address FROM EMPLOYEE WHERE Dno IN (SELECT Dnumber FROM DEPARTMENT WHERE Dname='Research' );

NESTING OF QUERIES (cont.) The nested query selects the number of the 'Research' department The outer query select an EMPLOYEE tuple if its Dno value is in the result of the nested query The comparison operator IN compares a value v with a set (or multi-set) of values V, and evaluates to TRUE if v is one of the elements in V In general, we can have several levels of nested queries A reference to an unqualified attribute refers to the relation declared in the innermost nested query In this example, the nested query is not correlated with the outer query

CORRELATED NESTED QUERIES If a condition in the WHERE-clause of a nested query references an attribute of a relation declared in the outer query , the two queries are said to be correlated The result of a correlated nested query is different for each tuple (or combination of tuples) of the relation(s) in the outer query Query 3: Retrieve the name of each employee who has a dependent with the same first name as the employee. Q3: SELECT E.Fname, E.Lname FROM EMPLOYEE AS E WHERE E.Ssn IN (SELECT Essn FROM DEPENDENT AS D WHERE D.Essn=E.Ssn AND E.Fname=D.Depenedent_name);

CORRELATED NESTED QUERIES (cont.) In Q3, the nested query has a different result for each tuple in the outer query A query written with nested SELECT... FROM... WHERE... blocks and using the = or IN comparison operators can always be expressed as a single block query. For example, Q3 may be written as in Q3A Q3A:SELECT E.Fname, E.Lname FROM EMPLOYEE E, DEPENDENT D WHERE E.Ssn=D.Essn AND E.Fname=D.Dependent_name;

THE EXISTS FUNCTION EXISTS is used to check whether the result of a correlated nested query is empty (contains no tuples) or not We can formulate Query 3 in an alternative form that uses EXISTS as Q3B below

THE EXISTS FUNCTION (cont.) Query 3B: Retrieve the name of each employee who has a dependent with the same first name as the employee. Q3B: SELECT Fname, Lname FROM EMPLOYEE WHERE EXISTS (SELECT * FROM DEPENDENT WHERE Ssn=Essn AND Fname=Dependent_name);

THE EXISTS FUNCTION (cont.) Query 4: Retrieve the names of employees who have no dependents. Q4: SELECT Fname, Lname FROM EMPLOYEE WHERE NOT EXISTS (SELECT * FROM DEPENDENT WHERE Ssn=Essn); In Q4, the correlated nested query retrieves all DEPENDENT tuples related to an EMPLOYEE tuple. If none exists , the EMPLOYEE tuple is selected. EXISTS is necessary for the expressive power of SQL

EXPLICIT SETS It is also possible to use an explicit (enumerated) set of values in the WHERE-clause rather than a nested query Query 5: Retrieve the social security numbers of all employees who work on project number 1, 2, or 3. Do Not allow duplicates of social security numbers in the result. Q5: SELECT DISTINCT Essn FROM WORKS_ON WHERE Pno IN (1, 2, 3);

Joined Tables in SQL Can specify a "joined relation" in the FROM-clause Looks like any other relation but is the result of a join Allows the user to specify different types of joins (INNER JOIN, NATURAL JOIN, LEFT OUTER JOIN, RIGHT OUTER JOIN, CROSS JOIN, etc)

Joined Tables in SQL (cont.) Examples: Q6: SELECT E.Fname, E.Lname, S.Fname, S.Lname FROM EMPLOYEE E S WHERE E.Super_ssn=S.Ssn; can be written as: Q6A:SELECT E.Fname, E.Lname, S.Fname, S.Lname FROM (EMPLOYEE E LEFT OUTER JOIN EMPLOYEE S ON E. Super_ssn=S.Ssn);

Joined Tables in SQL (cont.) Q7: SELECT Fname, Lname, Address FROM EMPLOYEE, DEPARTMENT WHERE Dname='Research' AND Dnumber=Dno; could be written as: Q7A:SELECT Fname, Lname, Address FROM (EMPLOYEE JOIN DEPARTMENT ON Dnumber=Dno) WHERE Dname='Research’; or as: Q7B:SELECT Fname, Lname, Address FROM (EMPLOYEE NATURAL JOIN (DEPARTMENT AS DEPT(Dname, Dno, Mssn, Msdate))) WHERE DNAME='Research’;

Joined Tables in SQL (cont.) Example to illustrate multiple joins in the joined tables For every project located in ‘Stafford’, list the project number, the controlling department number, and the department manager’s last name, birth date and address. Q8:SELECT Pnumber, Dnum, Lname, Bdate, Address FROM (PROJECT JOIN DEPARTMENT ON Dnum=Dnumber) JOIN EMPLOYEE ON Mgr_ssn=Ssn) ) WHERE Plocation='Stafford’;

Aggregate Functions Include COUNT, SUM, MAX, MIN, and AVG Query 9: Find the maximum salary, the minimum salary, and the average salary among all employees. Q9: SELECT MAX(Salary), MIN(Salary), AVG(Salary) FROM EMPLOYEE;

Aggregate Functions (cont.) Query 10: Find the maximum salary, the minimum salary, and the average salary among employees who work for the 'Research' department. Q10: SELECT MAX(Salary), MIN(Salary), AVG(Salary) FROM EMPLOYEE, DEPARTMENT WHERE Dno=Dnumber AND Dname='Research‘;

Aggregate Functions (cont.) Queries 11 and 12: Retrieve the total number of employees in the company (Q11), and the number of employees in the 'Research' department (Q12). Q11: SELECT COUNT (*) FROM EMPLOYEE; Q12: SELECT COUNT (*) FROM EMPLOYEE, DEPARTMENT WHERE Dno=Dnumber AND Dname='Research’;

Grouping In many cases, we want to apply the aggregate functions to subgroups of tuples in a relation Each subgroup of tuples consists of the set of tuples that have the same value for the grouping attribute(s) The function is applied to each subgroup independently SQL has a GROUP BY-clause for specifying the grouping attributes, which must also appear in the SELECT-clause

Grouping (cont.) Query 13: For each department, retrieve the department number, the number of employees in the department, and their average salary. Q13: SELECT Dno, COUNT (*), AVG (Salary) FROM EMPLOYEE GROUP BY Dno; In Q13, the EMPLOYEE tuples are divided into groups with each group having the same value for the grouping attribute Dno The COUNT and AVG functions are applied to each such group of tuples separately The SELECT-clause includes only the grouping attribute and the functions to be applied on each group of tuples

Grouping (cont.) Query 14: For each project, retrieve the project number, project name, and the number of employees who work on that project. Q14: SELECT Pnumber, Pname, COUNT (*) FROM PROJECT, WORKS_ON WHERE Pnumber=Pno GROUP BY Pnumber, Pname; In this case, the grouping and functions are applied after the joining of the two relations

The HAVING-Clause Sometimes we want to retrieve the values of these functions for only those groups that satisfy certain conditions The HAVING-clause is used for specifying a selection condition on groups (rather than on individual tuples)

The HAVING-Clause (cont.) Query 15: For each project on which more than two employees work , retrieve the project number, project name, and the number of employees who work on that project. Q15: SELECT Pnumber, Pname, COUNT(*) FROM PROJECT, WORKS_ON WHERE Pnumber=Pno GROUP BY Pnumber, Pname HAVING COUNT (*) > 2;

Summary of SQL Queries A query in SQL can consist of up to six clauses, but only the first two, SELECT and FROM, are mandatory. The clauses are specified in the following order: SELECT <attribute list> FROM <table list> [WHERE <condition>] [GROUP BY <grouping attribute(s)>] [HAVING <group condition>] [ORDER BY <attribute list>]

Summary of SQL Queries (cont.) The SELECT-clause lists the attributes or functions to be retrieved The FROM-clause specifies all relations (or aliases) needed in the query but not those needed in nested queries The WHERE-clause specifies the conditions for selection and join of tuples from the relations specified in the FROM-clause GROUP BY specifies grouping attributes HAVING specifies a condition for selection of groups ORDER BY specifies an order for displaying the result of a query A query is evaluated by first applying the WHERE-clause, then GROUP BY and HAVING, and finally the SELECT-clause