Your Logo Fundamentals of Database Systems Fourth Edition El Masri & Navathe Instructor: Mr. Ahmed Al Astal Chapter 8 (Cont.) SQL-99: Schema Definition,

Slides:



Advertisements
Similar presentations
SQL DESIGN AND IMPLEMENTATION CONTENT SOURCES: ELAMSARI AND NAVATHE, FUNDAMENTALS OF DATABASE MANAGEMENT SYSTEMSELAMSARI AND NAVATHE, FUNDAMENTALS OF.
Advertisements

COMPANY schema EMPLOYEE
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
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.
METU Department of Computer Eng Ceng 302 Introduction to DBMS SQL-99: Schema Definition, Basic Constraints, and Queries by Pinar Senkul resources: mostly.
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.
Copyright © 2004 Pearson Education, Inc.. Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries.
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.
관계 연산자 & 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: Constraints, and Queries and Views.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: Advanced Queries.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
Copyright © 2004 Pearson Education, Inc. Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries.
Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc.
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 © 2004 Pearson Education, Inc.. Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
Structured Query Language. Group Functions What are group functions ? Group Functions Group functions operate on sets of rows to give one result per group.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
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.
Copyright © 2004 Pearson Education, Inc.. Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries.
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.
Copyright © 2004 Pearson Education, Inc.. Lecture-7 Chapter 8 SQL: Schema Definition, Basic Constraints, and Queries.
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.
Copyright © 2004 Pearson Education, Inc. Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries.
Copyright © 2004 Pearson Education, Inc.. Lecture-7 Chapter 8 SQL: Schema Definition, Basic Constraints, and Queries.
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.
Copyright © 2004 Pearson Education, Inc.. Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries.
Structured Query Language (2) The main reference of this presentation is the textbook and PPT from : Elmasri & Navathe, Fundamental of Database Systems,
Faculty of Information Science and Technology Mahanakorn University of Technology Topic 8 SQL-99: Schema Definition, Basic Constraints, and Queries.
SQL SQL-99: a. Schema Definition b. Basic Constraints c. Queries.
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.
1 Chapter 5 The SQL Language Data Definition Language The Data Definition Language (DDL) is used to create and destroy databases and database.
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.
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 © 2004 Pearson Education, Inc.. Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
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.
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.
11/9/2018.
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:

Your Logo Fundamentals of Database Systems Fourth Edition El Masri & Navathe Instructor: Mr. Ahmed Al Astal Chapter 8 (Cont.) SQL-99: Schema Definition, Basic Constraints, and Queries

Your Logo 2 SQL: USE OF DISTINCT To eliminate duplicate tuples in a query result, the keyword DISTINCT is used For example, the result of Q12 may have duplicate SALARY values whereas Q12A does not have any duplicate values Q12:SELECT SALARY FROMEMPLOYEE Q12A: SELECT DISTINCT SALARY FROMEMPLOYEE

Your Logo 3 SET OPERATIONS SQL has directly incorporated some set operations There is a union operation (UNION), and in some versions of SQL there are set difference (MINUS) and intersection (INTERSECT) operations The resulting relations of these set operations are sets of tuples; duplicate tuples are eliminated from the result The set operations apply only to union compatible relations ; the two relations must have the same attributes and the attributes must appear in the same order

Your Logo 4 SET OPERATIONS (Cont.) Query 13: Make a list of all project numbers for projects that involve an employee whose last name is 'Smith' as a worker or as a manager of the department that controls the project. (SELECT PNAME FROMPROJECT, DEPARTMENT, EMPLOYEE WHEREDNUM=DNUMBER AND MGRSSN=SSN ANDLNAME='Smith') UNION (SELECT PNAME FROMPROJECT, WORKS_ON, EMPLOYEE WHERE PNUMBER=PNO AND ESSN=SSN ANDLNAME='Smith')

Your Logo 5 SET OPERATIONS (Cont.) Query 14: Retrieve the name of each employee who is either a manger or has some dependent. (SELECT FAME, MINIT, LNAME FROMEMPLOYEE, DEARTMENT WHERESSN=MGRSSN') UNION (SELECT FAME, MINIT, LNAME FROMEMPLOYEE, DEPENDENT WHERE SSN=ESSN)

Your Logo 6 SET OPERATIONS (Cont.) Query 15: Retrieve the name of each manager who has dependents. (SELECT FAME, MINIT, LNAME FROMEMPLOYEE, DEARTMENT WHERESSN=MGRSSN') INTERSECT (SELECT FAME, MINIT, LNAME FROMEMPLOYEE, DEPENDENT WHERE SSN=ESSN)

Your Logo 7 SET OPERATIONS (Cont.) Query 15: Retrieve the name of each manager who has dependents. (SELECT FAME, MINIT, LNAME FROMEMPLOYEE, DEARTMENT WHERESSN=MGRSSN') MINUS (SELECT FAME, MINIT, LNAME FROMEMPLOYEE, DEPENDENT WHERE SSN=ESSN)

Your Logo 8 SET OPERATIONS (Cont.) Query 16: Retrieve the name of each employee who has some dependent and not a manger. (SELECT FAME, MINIT, LNAME FROMEMPLOYEE, DEPENDENT WHERE SSN=ESSN) MINUS (SELECT FAME, MINIT, LNAME FROMEMPLOYEE, DEARTMENT WHERESSN=MGRSSN')

Your Logo 9 SET OPERATIONS (Cont.) Query 17: Retrieve the names of non manager employees. (SELECT FAME, MINIT, LNAME FROMEMPLOYEE) MINUS (SELECT FAME, MINIT, LNAME FROMEMPLOYEE, DEARTMENT WHERESSN=MGRSSN')

Your Logo 10 SET OPERATIONS (Cont.) Query 18: Retrieve the name of departments that don’t have any project located in “Huston”. (SELECT DNAME FROMDEPARTMENT) MINUS (SELECT DNAME FROMDEPARTMENT, PROJECT WHEREDNUMBER=DNUM ANDPLOCATION=“Huston”)

Your Logo 11 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 19: Retrieve the name and address of all employees who work for the 'Research' department. SELECTFNAME, LNAME, ADDRESS FROM EMPLOYEE WHEREDNO IN (SELECT DNUMBER FROM DEPARTMENT WHERE DNAME='Research' )

Your Logo 12 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 either 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

Your Logo 13 CORRELATED NESTING OF 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) the outer query

Your Logo 14 CORRELATED NESTING OF QUERIES Query 20: Retrieve the name of each employee who has a dependent with the same first name as the employee. SELECT E.FNAME, E.LNAME FROMEMPLOYEE AS E WHEREESSN IN (SELECTESSN FROMDEPENDENT WHEREESSN=E.SSN AND E.FNAME=DEPENDENT_NAME)

Your Logo 15 NESTING OF QUERIES Query 21: Retrieve the Names of Managers SELECT E.FNAME, MINIT, LNAME FROMEMPLOYEE AS E WHERE ESSN IN (SELECTMGRSSN FROMDEPARTMENT) Query 22: Retrieve the Names of Supervisors SELECT E.FNAME, MINIT, LNAME FROMEMPLOYEE AS E WHERE ESSN IN (SELECTSUPERSSN FROMEMPLOYEE)

Your Logo 16 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 20in an alternative form that uses EXISTS as Q20A below Query 20A: Retrieve the name of each employee who has a dependent with the same first name as the employee. SELECT FNAME, LNAME FROMEMPLOYEE WHEREEXISTS (SELECT * FROMDEPENDENT WHERESSN=ESSN AND FNAME=DEPENDENT_NAME)

Your Logo 17 EXPLICIT SETS It is also possible to use an explicit (enumerated) set of values in the WHERE-clause rather than a nested query. Query 23: Retrieve the social security numbers of all employees who work on project number 1, 2, or 3. SELECT DISTINCT ESSN FROMWORKS_ON WHEREPNO IN (1, 2, 3)

Your Logo 18 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 24: Retrieve the names of all employees who do not have supervisors. SELECT FNAME, LNAME FROMEMPLOYEE WHERESUPERSSN IS NULL

Your Logo 19 AGGREGATE FUNCTIONS Include COUNT, SUM, MAX, MIN, and AVG Query 25: Find the maximum salary, the minimum salary, and the average salary among all employees. SELECT MAX(SALARY), MIN(SALARY), AVG(SALARY) FROMEMPLOYEE Some SQL implementations may not allow more than one function in the SELECT-clause

Your Logo 20 AGGREGATE FUNCTIONS (Cont.) Query 26: Find the maximum salary, the minimum salary, and the average salary among employees who work for the 'Research' department. SELECT MAX(SALARY), MIN(SALARY), AVG(SALARY) FROMEMPLOYEE, DEPARTMENT WHEREDNO=DNUMBER AND DNAME='Research'

Your Logo 21 AGGREGATE FUNCTIONS (Cont.) Queries 27: Retrieve the total number of employees in the 'Research' department SELECT COUNT (*) FROMEMPLOYEE, DEPARTMENT WHEREDNO=DNUMBER AND DNAME='Research’

Your Logo 22 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

Your Logo 23 GROUPING (Cont.) Query 28: For each department, retrieve the department number, the number of employees in the department, and their average salary. SELECT DNO, COUNT (*), AVG (SALARY) FROMEMPLOYEE GROUP BYDNO