Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.

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,
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.
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 Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
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 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.
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.
Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc.
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,
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.
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.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide
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.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
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 © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 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)
Slide 8- 1 THE HAVING-CLAUSE Provides a condition on the summary information Sometimes we want to retrieve the values of these functions for only those.
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.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
Structured Query Language (2) The main reference of this presentation is the textbook and PPT from : Elmasri & Navathe, Fundamental of Database Systems,
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.
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.
Chapter 8 SQL-99: Schema Definition, Constraints, & Queries and Views.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 (SQL Basics) Lecture # 9 July 7,2012.
Copyright © 2004 Pearson Education, Inc. Schema Definition, Basic Constraints, and Queries.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Relational Database Design by ER- and EER- to-Relational Mapping.
Slide Chapter 8 Views تنبيه : شرائح العرض (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 6 The Relational Algebra and Relational Calculus تنبيه : شرائح العرض.
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 7 Data Modeling Using the Entity- Relationship (ER) Model تنبيه :
The Relational Algebra and Relational Calculus
CS580 Advanced Database Topics
6/22/2018.
SQL Grouping, Ordering & Arithmetics Presented by: Dr. Samir Tartir
Presentation transcript:

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة من الادوات في ذلك. حيث المرجع الاساسي للمادة هي الكتاب المعتمد في وصف المقرر

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 2 NULLS IN SQL QUERIES SQL allows queries that check if a value is NULL Query 14: Retrieve the names of all employees who do not have supervisors. Q14:SELECT FNAME, LNAME FROMEMPLOYEE WHERESUPERSSN IS NULL

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 3 AGGREGATE FUNCTIONS Query 15: Find the maximum salary, the minimum salary, and the average salary among all employees. Q15:SELECT MAX(SALARY), MIN(SALARY), AVG(SALARY) FROMEMPLOYEE

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 4 AGGREGATE FUNCTIONS (contd.) Query 16: Find the maximum salary, the minimum salary, and the average salary among employees who work for the 'Research' department. Q16: SELECT MAX(SALARY), MIN(SALARY), AVG(SALARY) FROMEMPLOYEE, DEPARTMENT WHEREDNO=DNUMBER AND DNAME='Research'

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 5 AGGREGATE FUNCTIONS (contd.) Queries 17 : Retrieve the total number of employees in the company Q17:SELECT COUNT (*) FROMEMPLOYEE Queries 18: Retrieve the number of employees in the 'Research' department Q18:SELECT COUNT (*) FROMEMPLOYEE, DEPARTMENT WHEREDNO=DNUMBER AND DNAME='Research’

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 6 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

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 7 GROUPING (contd.) Query 20: For each department, retrieve the department number, the number of employees in the department, and their average salary. Q20:SELECT DNO, COUNT (*), AVG (SALARY) FROMEMPLOYEE GROUP BYDNO In Q20, the EMPLOYEE tuples are divided into groups- 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 A join condition can be used in conjunction with grouping

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 8 GROUPING (contd.) Query 21: For each project, retrieve the project number, project name, and the number of employees who work on that project. Q21:SELECT PNUMBER, PNAME, COUNT (*) As NoOfEmployee FROMPROJECT, WORKS_ON WHEREPNUMBER=PNO GROUP BYPNUMBER, PNAME In this case, the grouping and functions are applied after the joining of the two relations

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 9 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)

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 10 THE HAVING-CLAUSE (contd.) Query 22: 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. Q22: SELECT PNUMBER, PNAME, COUNT(*) FROMPROJECT, WORKS_ON WHEREPNUMBER=PNO GROUP BYPNUMBER, PNAME HAVINGCOUNT (*) > 2

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 11 THE HAVING-CLAUSE (contd.) Q22:SELECT PNUMBER, PNAME, COUNT (*) As NoOfEmployee FROMPROJECT, WORKS_ON WHEREPNUMBER=PNO GROUP BYPNUMBER, PNAME HAVINGCOUNT (*) > 2

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 12 ORDER BY The ORDER BY clause is used to sort the tuples in a query result based on the values of some attribute(s) Query 28: Retrieve a list of employees and the projects each works in, ordered by the employee's department, and within each department ordered alphabetically by employee last name. Q28: SELECT DNAME, LNAME, FNAME, PNAME FROM DEPARTMENT, EMPLOYEE, WORKS_ON, PROJECT WHEREDNUMBER=DNO AND SSN=ESSN AND PNO=PNUMBER ORDER BYDNAME, LNAME

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 13

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 14 ORDER BY (contd.) The default order is in ascending order of values We can specify the keyword DESC if we want a descending order; the keyword ASC can be used to explicitly specify ascending order, even though it is the default Q28: SELECT DNAME, LNAME, FNAME, PNAME FROM DEPARTMENT, EMPLOYEE, WORKS_ON, PROJECT WHERE DNUMBER=DNO AND SSN=ESSN AND PNO=PNUMBER ORDER BYDNAME DESC, LNAME DESC

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe ORDER BY (contd.) 15

Copyright © 2011 Ramez Elmasri and Shamkant B. Navathe 16 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 FROM [WHERE ] [GROUP BY ] [HAVING ] [ORDER BY ]