Chapter 3 SQL. 2 Relational Commercial Languages  SQL (Structured Query Language)  the standard relational DB language  used interactively which transforms.

Slides:



Advertisements
Similar presentations
1 Today’s Class  Relational Model  SQL CS F212 Database Systems.
Advertisements

E-R Diagram for a Banking Enterprise
BRANCH-SCHEME (BRANCH-NAME, ASSETS, BRANCH-CITY)
Chapter 2 Relational Model (part II) Hankz Hankui Zhuo
SQL Sangeeta Devadiga CS157A, Fall Outline Background Data Definition Basic Structure Set Operation.
SQL.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Ordering the Display of Tuples List in alphabetic order the names of all customers having.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Extended Relational-Algebra-Operations Generalized Projection Outer Join Aggregate Functions.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions.
Temple University – CIS Dept. CIS331– Principles of Database Systems V. Megalooikonomou Query by example (based on notes by Silberchatz,Korth, and Sudarshan.
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. SQL Database Management Systems I Alex Coman, Winter 2006.
CMSC424: Database Design Instructor: Amol Deshpande
SQL SQL stands for Structured Query Language SQL allows you to access a database SQL is an ANSI standard computer language SQL can execute queries against.
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. SQL - part 2 - Database Management Systems I Alex Coman, Winter 2006.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Aandachtspunten Deadlines – alles op tijd inleveren! n Lees goed wat van je verwacht wordt!
MySQL Tutorial (2) Introduction to Database. Banking Example branch (branch-name, branch-city, assets) customer (customer-name, customer-street, customer-city)
MySQL Tutorial Introduction to Database. Learning Objectives  Read and write Data Definition grammar of SQL  Read and write data modification statements.
CIS552SQL1 Data Definition Language Insertions Basic Query Structure Set Operations Aggregate Functions Null Values Nested Subqueries Derived Relations.
Chapter 3: SQL Data Definition Language Data Definition Language Basic Structure of SQL Basic Structure of SQL Set Operations Set Operations Aggregate.
CS 370 Database Systems Lecture 13 Introduction to SQL.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
Database System Concepts, 5th Ed. Bin Mu at Tongji University Chapter 3: SQL.
-- Introduction to database principles Maoying Wu March 11, 2013 Chapter 3: Basic SQL.
Chapters 2 & 6 The Relational Model. 2  A tabular data structure  Tables (relations) with unique names  rows (tuples/entities/records)  columns (attributes/fields)
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
SQL. Basic Structure SQL is based on set and relational operations with certain modifications and enhancements A typical SQL query has the form: select.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
3.1 Chapter 3: SQL. 3.2 Chapter 3: SQL Basic Query Structure Set Operations Aggregate Functions Null Values Nested Subqueries Complex Queries Views Modification.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Null Values It is possible for tuples to have a null value for some of their attributes The.
Lecture 6 Structured Query Language SQL Lecture 6 Structured Query Language SQL Instructor: Haya Sammaneh.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
3.1 Chapter 3: SQL Schema used in examples p (omit 3.8.2, , 3.11)
Basic Structure Given sets A 1, A 2,..., A n a relation r is a subset of A 1 x A 2 x... x A n Thus a relation is a set of n-tuples (a 1, a 2,..., a n )
Structured Query Language 2 Presented by: Annisa, M.Kom. Source: Database System Concepts 5 th edition.
Relational Algebra Instructor: Mohamed Eltabakh 1.
Chapter 3: SQL. 3.2Unite International CollegeDatabase Management Systems Chapter 3: SQL Data Definition Basic Query Structure Set Operations Aggregate.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 5 th Edition, Oct 5, 2006 Outer Join n An extension of the join operation that avoids loss.
Chapter 6: Formal Relational Query Languages. 6.2 Chapter 6: Formal Relational Query Languages Relational Algebra Tuple Relational Calculus Domain Relational.
Chapter 3: SQL. 3.2Unite International CollegeDatabase Management Systems Chapter 3: SQL Data Definition Basic Query Structure Set Operations Aggregate.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts Chapter 5: Other Relational Languages Query-by-Example (QBE)
Computing & Information Sciences Kansas State University Monday, 08 Sep 2008CIS 560: Database System Concepts Lecture 5 of 42 Monday, 08 September 2008.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts Chapter 5: Other Relational Query Languages Tuple Relational Calculus Domain Relational Calculus.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts, 5 th Edition, Oct 5, 2006 SQL Data Definition Basic Query Structure Set Operations Aggregate.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
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.
Source: Database System Concepts, Silberschatz etc Edited: Wei-Pang Yang, IM.NDHU, Introduction to Database CHAPTER 5 Other Relational Languages.
Chapter 4: SQL Complex Queries Complex Queries Views Views Modification of the Database Modification of the Database Joined Relations Joined Relations.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Extended Relational-Algebra-Operations Generalized Projection Aggregate Functions Outer Join.
Computing & Information Sciences Kansas State University Friday, 08 Sep 2006CIS 560: Database System Concepts Lecture 07 of 42 Tuesday, 30 January 2007.
MySQL Tutorial Introduction to Database. Introduction of MySQL  MySQL is an SQL (Structured Query Language) based relational database management system.
SCUHolliday - coen 1787–1 Schedule Today: u Subqueries, Grouping and Aggregation. u Read Sections Next u Modifications, Schemas, Views. u Read.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
IS 230Lecture 7Slide 1 Relational Algebra Lecture 8 Text Book – Chapter.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL Bambang.
Database System Concepts ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
Query Languages Language in which user requests information from the database. Categories of languages Procedural Non-procedural, or declarative “Pure”
Structured Query Language (SQL)
Chapter 3: SQL.
SQL Structured Query Language 11/9/2018 Introduction to Databases.
Chapter 3: SQL.
Instructor: Mohamed Eltabakh
SQL: Structured Query Language
Introduction to Database
CS 405G: Introduction to Database Systems
Presentation transcript:

Chapter 3 SQL

2 Relational Commercial Languages  SQL (Structured Query Language)  the standard relational DB language  used interactively which transforms inputs into desired outputs via relations  relations are closed under the SQL operations  Data Definition Language (DDL) of SQL  create/delete/modify relation schemas/views/indices  Create table: specifies a new relation schema create table r(A 1 D 1,..., A n D n ) where r is new relation name A i, 1  i  n, an attribute name D i, 1  i  n, domain of A i

3 Relational Commercial Languages  Data Definition Language (DDL) of SQL  Drop table: removes an existing relation drop table r: deletes all information of r (schema + tuples in r) delete r: deletes all tuples in r but retains the schema of r  Alter table: adds an attribute to an existing relation alter table r add A D alter table r drop A where r is an existing relation A is an attribute name D is the domain of A Note: the new attribute will have NULLs in all tuples of r

4 Relational Commercial Languages  Data Manipulation Language (DML) of SQL  a query language based on the relational algebra and tuple calculus  retrieve/insert/delete/modify tuples in DB relations  DML operations:  Select format: SELECT A 1, A 2,…, A n Usage: projects attributes A 1, A 2,…, A n in order - duplicate rows remain - use the qualifier, DISTINCT, to remove duplicate rows

5 SQL  DML operations:  Where format: WHERE P Usage: selects tuples which satisfy the predicate P  From format: FROM r 1, r 2,…, r m Usage: performs a cartesian product on relations r 1, r 2, , r m  Typical SQL Query: SELECT A 1, A 2, , A n FROM r 1, r 2, , r m WHERE P – in Relational Algebra:  A 1, A 2, …, A n (  P (r 1  r 2    r m )) – Where clause is optional and P involves attrs appear in the From clause – A 1, A 2, , A n can be replaced by ‘*’ to select all attributes in the query

6 SQL  Set (union/intersect/subtraction) operations:  Combine the results of two SELECT statements into a single one  Apply only to union compatible relations Union (  ): returns those tuples generated by either query and eliminates duplicate tuples (use Union all to retain duplicates) Intersect (  ): returns only those tuples generated by both queries Minus (  ): returns only those tuples generated by the first query but not the second query Format: (SELECT …) UNION/ INTERSECT/ MINUS (SELECT …) e.g., Let is_taking(Sname, Course#) and has_taken (Sname, Course#) be 2 relations (SELECT DISTINCT Course# FROM has_taken WHERE Sname = ‘Joe Young’) UNION (SELECT DISTINCT Course# FROM is_taking WHERE Sname = ‘Joe Young’)

7 SQL  Set membership:  Usage: IN/NOT IN is used as part of the WHERE-clause to indicate a set membership or absence of set membership  Retrieval/update using subquery: IN (NOT IN) Format: SELECT A 1, A 2,…, A n FROM r 1, r 2,…, r m WHERE P (NOT) IN subquery  NOTE: works from bottom-up, i.e., a subquery is evaluated first prior to the evaluation of the main (outer) query  Example: Given Is_taking(Sname, Course#) and Has_taken(Sname, Course#) SELECT DISTINCT Sname FROM Is_taking I WHERE Course# [NOT] IN (SELECT Course# FROM Has_taken H WHERE H.Sname = I.Sname)

8 SQL  Set Comparison:  Tests a single value against elements of a set Format: SELECT … WHERE A i Comp subquery where Comp  {  some,  some,  some,  some,  some,  some,  all,  all,  all,  all,  all,  all }  Example. Given the relation Student(SSNO, Sname, Age) SELECT SSNO FROM Student WHERE Age  Some (SELECT DISTINCT Age FROM Student WHERE Sname  ‘Joe Young’)

9 Definition of “Some” Clause in SQL  E “  comp  some” r  t  r  such that (E  comp  some t ), where  comp   { ,   } (5  some ) = 0 5 (5  some ) (read: 5  some tuple in the relation) 0 5 ) (5  some 0 5 ) (5  some Note that “  some”  IN, = false = true = true (since 0  5) true but “  some”  NOT IN

10 Definition of “All” Clause in SQL  E  comp  all r  t  r  (E  comp  all t) (5  all ) = 4 6 (5  all ) = 6 10 ) = (5  all 4 5 ) = (5 = all Note that “  all”  NOT IN, false true false true (since 5  4 and 5  6) but “= all”  IN

11 SQL  (Sub)Set Comparison:  compares sets (not) being subsets of other sets Format: SELECT … WHERE subquery CONTAINS/NOT CONTAINS subquery  Example. Given Is_taking(Sname, Course#) and Has_taken(Sname, Course#) SELECT I.Sname FROM Is_taking I WHERE (SELECT DISTINCT H.Course# FROM Has_taken H WHERE I.Sname  H.Sname) [NOT] CONTAINS (SELECT DISTINCT IT.Course# FROM Is_taking IT WHERE IT.Sname  I.Sname)

12 SQL  Set Comparison:  Tests for (non-)empty relations generated by subqueries Format: SELECT … WHERE EXISTS/NOT EXISTS subquery  Example. Consider the following database schema: customer(customer-name, street, customer-city) deposit(branch-name, account-number, customer-name, balance) borrow(branch-name, loan-number, customer-name, amount) Query: “Find all customers who have both an account and a loan at the Perryridge branch” SELECT customer-name FROM customer C (SELECT * FROM deposit D WHERE D.customer-name  C.customer-name AND branch-name = ‘Perryridge’) AND EXISTS FROM borrow B WHERE B.customer-name  C.customer-name AND branch-name  ‘Perryridge’) (SELECT * WHERE EXISTS

13 SQL  Joins  defined in terms of  (FROM),  (WHERE) and  (SELECT) Format: Same as a typical SQL query except 1. Attributes appear in more than one relation should be designated with relation name (tuple variable) 2. WHERE-clause specifies the join on common attributes from different relations  Example. Given Borrower(customer-name, loan-number) and Loan(loan-number, branch-name, amount) SELECT customer-name, T.loan-number, amount FROM Borrower T, Loan S WHERE T.loan-number  S.loan-number

14 SQL  The Except Operation:  Same as set minus (difference)  Automatically eliminate duplicates. To retain all duplicates, use except all  Format: Subquery Except Subquery  Example. Given the following relations Depositor(account-number, customer-name, balance) and Borrower(loan-number, customer-name, amount) Find all customers who have an account but no loan. (SELECT customer-name FROM depositor) EXCEPT (SELECT customer-name FROM borrower)

15 The Rename Operation  SQL allows relations and attributes to be renamed using the AS clause: old-name AS new-name  Example. “Find the name, loan number, and loan amount of all customers; rename loan-number as loan-id.” /* Given Borrower (Customer-name, Loan-number) Loan (Loan-number, Branch-name, Amount) */ SELECT customer-name, B.loan-number AS loan-id, amount FROM borrower B, loan L WHERE B.loan-number = L.loan-number

16 Derived Relations  Using subqueries in the FROM clause to generate derived relations  Data in any derived relations can be used in the WHERE clause of the same SQL statement.  Example. “Find the cheapest laptops (identified by Model) that have the largest hard drive among all the laptops in the DB,” where /* Given Laptop(Model, Speed, RamSize, HdSize, Screen, Price) */ SELECT Model FROM (SELECT Model, Price FROM Laptop WHERE HdSize  ALL (SELECT L.HdSize FROM Laptop L)) WHERE Price  ALL (SELECT Price FROM Laptop WHERE HdSize  ALL (SELECT L.HdSize FROM Laptop L))

17 Derived Relations  A derived relation cannot be used in another subquery of the same SQL statement  Example. “Find the cheapest laptops that have the largest hard drive among all the laptops in the DB.” SELECT Model FROM (SELECT Model, Price FROM Laptop WHERE HdSize  ALL (SELECT L.HdSize FROM Laptop L)) MaxSize WHERE Price  ALL (SELECT M.Price FROM MaxSize M); /* An invalid Oracle SQL Query due to the scope error */

18 Derived Relations  Each derived relation can be assigned a name and can be used in the same SQL statement  Example. “Find the laptops that have the largest hard drive among all the laptops in the database and cost less than $2,500.” SELECT Model FROM (SELECT Model, Price FROM Laptop WHERE HdSize  ALL (SELECT L.HdSize FROM Laptop L)) MaxSize WHERE MaxSize.Price  2500;

19 Database Updates in SQL  Deletion: removes whole tuples  Format: DELETE r [WHERE P]  Insertion: adds new tuples with correct arity and domain values  Format: INSERT INTO r[A 1, A 2,…, A n ] VALUES (V 1, V 2,…, V n ) Where V i  dom(A i ), 1  i  n. If A 1, A 2,…, A n are not specified, then V 1, V 2,…, V n must be listed according to the order of attributes shown in the schema  Modification: modifies tuple values in a relation r  Format: Update r SET attr  value [WHERE P]

20 Modification of the Database – Insertion  Example. Given Account(Account-number, Branch-name, Balance), add a new tuple to account INSERT INTO account VALUES (‘A-9732’, ‘Perryridge’,1200) or equivalently INSERT INTO account (branch-name, balance, account-number) VALUES (‘Perryridge’, 1200, ‘A-9732’)  Example. Add a new tuple to account with balance set to null INSERT INTO account VALUES (‘A-777’,‘Perryridge’, null)

21 Modification of the Database – Insertion  A Select-From-Where statement should be fully evaluated before any of its results are inserted into the relation; otherwise, the query INSERT INTO table1 SELECT * FROM table1 would cause problems.  Example. “Provide as a gift for all loan customers of the Perryridge branch, a $200 savings account. Let the loan number serve as the account number for the new savings account.” Account(Acct-number, Branch-name, Balance) Loan (Loan-number, Branch-name, Amount) INSERT INTO account SELECT loan-number, branch-name, 200 FROM loan WHERE branch-name = ‘Perryridge’ / * Depositor(Customer-name, Acct-number) */ INSERT INTO depositor SELECT customer-name, L.loan-number FROM loan L, borrower B WHERE branch-name = ‘Perryridge’ AND L.loan-number = B.loan-number / * Borrower(Customer-name, Loan-number) */

22 Modification of the Database – Updates  Example. “Increase all accounts with balances over $10,000 by 6%, all other accounts receive 5%.”  Write two update statements: UPDATE account SET balance = balance  1.06 WHERE balance > UPDATE account SET balance = balance  1.05 WHERE balance   The order is important  Can be done better using the case statement

23 “Case” Statements for DB Updates  Example. “Increase all accounts with balances over $10,000 by 6%, all other accounts receive 5%” using a case statement. UPDATE account SET balance = CASE WHEN balance <= THEN balance *1.05 ELSE balance * 1.06 END

24 “Case” Statements for DB Retrieval  Example. “Find the name, (salary) category, and department of each employee, sorted by category and department. The salary categories include high (i.e., 100,000  Salary), medium (i.e., 50,000  Salary  100,000) and low (i.e., Salary  50,000). /* Employee(Ename, ESSN, Address, Salary); Works_for(ESSN, Dno) */ SELECT Ename, Category, DNO FROM Employee E, Works_for W, (SELECT ESSN AS CSSN, CASE WHEN Salary >= THEN ‘High’ WHEN Salary = THEN ‘Medium’ ELSE ‘Low’ END AS Category FROM Employee) WHERE E.ESSN = CSSN AND E.ESSN = W.ESSN ORDER BY Category, Dno;

25 “Case” Statements for DB Retrieval  Example. “Find the crew members (identified by CID) who have been or have not been assigned to at least one flight. (The result table must include two columns, CID and Assigned, such that the column values of Assigned are either `Yes', for assigned, or `No', for not assigned.) /* Given crew (CID, Name, Position, Fly_Hours) assign (Flight#, CID) */ SELECT CID, (CASE WHEN CID IN (SELECT CID FROM Assign) THEN ‘Yes’ ELSE ‘No’ END) AS Assigned FROM Crew;

26 Modification of the DB – Deletion  Example. Delete all account records at the Perryridge branch DELETE FROM account WHERE branch-name = ‘Perryridge’  Example. Delete all accounts at every branch located in Needham Account (Account-number, Branch-name, Balance) Branch (Branch-name, Branch-city, Assets) Depositor (Customer-name, Account-number) DELETE FROM depositor WHERE account-number IN (SELECT account-number FROM branch B, account A WHERE branch-city = ‘Needham’ AND B.branch-name = A.branch-name) DELETE FROM account WHERE branch-name IN (SELECT branch-name FROM branch WHERE branch-city = ‘Needham’) Must be in one transaction; otherwise, the order must be specified as is

27 Views  Provide a mechanism to hide certain data from the view of certain users.  To create a view we use the command: CREATE VIEW v AS  query expression  where   query expression  is any legal expression  The (new) view name is represented by v

28 Example Queries of Views  A view consisting of the branches and all their customer names CREATE VIEW all-customer AS (SELECT branch-name, customer-name FROM depositor D, account A WHERE D.account-number = A.account-number) UNION (SELECT branch-name, customer-name FROM borrower B, loan L WHERE B.loan-number = L.loan-number)  Find all the customers of the Perryridge branch SELECT customer-name FROM all-customer WHERE branch-name = ‘Perryridge’ depositor (customer-name, account-number) account (account-number, branch-name, balance) borrower (customer-name, loan-number) loan (loan-number, branch-name, amount)

29 SQL  Aggregate Functions:  functions: avg, count, sum, max, min apply to a list of values appearing in the aggregated column(s) to yield a single value  functions on groups of tuples (generated using Group By clause) Format: SELECT aggregate function(A 1, A 2, …, A n ) FROM r 1, r 2, …, r m WHERE P GROUP BY (B 1, …, B k ) /* GROUP BY is optional */ Usage: group together tuples that have the same value of specified attribute(s) expressed in the GROUP BY clause Constraint: in the SELECT clause that includes aggregations, only those attributes mentioned in the GROUP BY may appear unaggregated  With aggregate functions, SQL is strictly more powerful than the relational algebra, but is less powerful than a general-purpose programming language

30 SQL Aggregate Functions  Perform some computation/summarization  A significant extension of relational algebra  SQL supports five aggregate operations: 1. COUNT ([DISTINCT] A): Number of (unique) values in A 2. SUM ([DISTINCT] A): The sum of all (unique) values in A 3. AVG ([DISTINCT] A): The average of all (unique) values in A 4. MAX (A): The maximum value in A 5. MIN (A): The minimum value in A where A is a column (i.e., an attribute), and SUM and AVG operate only on a set of numerical values

31 Examples of SQL Aggregate Functions  Let crew(ID#, Name, Position, Seniority, Fly_Hours) be a relation schema  Find the average flying hours of all crew members. SELECT AVG(Fly_Hours) FROM crew;  Find the average flying hours of all crew members who are pilots. SELECT AVG(Fly_Hours) FROM crew WHERE Position  ‘Pilot’;  Find the name and flying hours of the crew member who has accumulated the most flying hours. SELECT Name, MAX(Fly_Hours) FROM crew; NOTE: illegal query in SQL.

32 Examples of SQL Aggregate Functions  Example. Given the following relations: crew(ID#, Name, Position, Seniority, Fly_Hours) flight(Flight#, Source_City, Dest_City, Date, No_Pass, Dep_Time, Arr_Time)  Find the name and flying hours of the crew member who has accumulated the most flying hours. SELECT Name, Fly_Hours FROM crew WHERE Fly_Hours  (SELECT MAX(Fly_Hours) FROM crew)  Find the number of source cities in the database. SELECT COUNT(DISTINCT Source_City) AS Num_Source_City FROM flight;

33 SQL Aggregate Functions with Group-By  Example. Given the following relations: crew(ID#, Name, Position, Seniority, Fly_Hours) flight(Flight#, Source_City, Dest_City, Date, No_Pass, Dep_Time, Arr_Time)  Find the lowest seniority for each position. SELECT Position, MIN(Seniority) FROM crew GROUP BY Position;  Find the number of flights originated from a particular city. SELECT Source_City, COUNT(Dep_Time) FROM flight GROUP BY Source_City;  Find the latest flight (identified by dep_time) before 8:00 p.m. from each city. SELECT Source_City, MAX(Dep_Time) FROM flight WHERE Dep_Time  2000 GROUP BY Source_City;

34 SQL Aggregate Functions with Group-By  Example. Given the following relations: crew(ID#, Name, Position, Seniority, Fly_Hours) flight(Flight#, Source_City, Dest_City, Date, No_Pass, Dep_Time, Arr_Time) assign(ID#, Flight#)  Find the average number of passengers between two cities. SELECT Source_City, Dest_City, AVG(No_Pass) FROM flight GROUP BY Source_City, Dest_City;  Find the number of flights assigned to each crew member (identified by name). SELECT Name, COUNT(Flight#) FROM assigns a, crew c WHERE a.ID#  c.ID# GROUP BY a.ID#, Name

35 SQL Aggregate Functions – Having Clause  Aggregate operations using Group By and Having, which specifies conditions on each group created by Group By  Group By–Having clause chooses groups based on some aggregate property of each group itself  Example. Given the following relations: crew(ID#, Name, Position, Seniority, Fly_Hours) assign(ID#, Flight#)  Find the average number of flying hours of the crew members on any flight with more than one crew member SELECT Flight#, AVG(Fly_Hours) FROM crew c, assigns a WHERE c.ID#  a.ID# GROUP BY Flight# HAVING COUNT(*)  1;

36 SQL Aggregate Functions with Group-By and Having  Given the following relation schemas: Account(Branch-Name, Account-Number, Balance) Customer(Customer-Name, Street, City) Depositor(Customer-Name, Account-Number)  Find the average account balance at each branch SELECT Branch-Name, AVG(Balance) FROM account GROUP BY Branch-Name;  Find the average account balance at each branch of which the average account balance is more than $1,200. SELECT Branch-Name, AVG(Balance) FROM account GROUP BY Branch-Name HAVING AVG(Balance)  1200;

37 SQL Aggregate Functions with Group-By and Having  Given the following relation schemas: Account(Branch-Name, Account-number, Balance) Customer(Customer-Name, Street, City) Depositor(Customer-Name, Account-Number)  Find the number of tuples, i.e., customer, in the customer table. SELECT COUNT(*) FROM customer; Note: SQL does not allow the use of DISTINCT with COUNT(*)  Find the average balance for each customer who lives in Harrison and has at least three accounts SELECT d.Customer-Name, AVG(Balance) FROM depositor d, account a, customer c WHERE d.Account-Number  a.Account-Number AND d.Customer-Name  c.Customer-Name AND City  ‘Harrison’ GROUP BY d.Customer-Name HAVING COUNT(DISTINCT d.Account-Number)  3;