SQL: Structured Query Language

Slides:



Advertisements
Similar presentations
CS4432: Database Systems II Query Operator & Algebraic Expressions 1.
Advertisements

1 Today’s Class  Relational Model  SQL CS F212 Database Systems.
INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012.
E-R Diagram for a Banking Enterprise
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
©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.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
Nov 24, 2003Murali Mani SQL B term 2004: lecture 12.
SQL Neyha Amar CS 157A, Fall Inserting The insert statement is used to add a row of data into a table Strings should be enclosed in single quotes,
Murali Mani SQL. Murali Mani SELECT-FROM-WHERE SELECT * FROM Student WHERE sName=“Greg” AND address=“320 FL”  (sName=“Greg” AND address=“320 FL”) (Student)
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. SQL - part 2 - Database Management Systems I Alex Coman, Winter 2006.
MySQL Tutorial (2) Introduction to Database. Banking Example branch (branch-name, branch-city, assets) customer (customer-name, customer-street, customer-city)
Instructor: Mohamed Eltabakh
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.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
©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.
Structured Query Language 2 Presented by: Annisa, M.Kom. Source: Database System Concepts 5 th edition.
Relational Algebra Instructor: Mohamed Eltabakh 1.
CSc-340 2b1 Introduction to SQL Chapter 3 [2 of 2] Null Values Aggregate Functions Nested Subqueries Modification of the Database.
Advanced SQL Murat Kantarcioglu Adapted from Silberchatz et al. slides.
©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.
NULLs & Outer Joins Objectives of the Lecture : To consider the use of NULLs in SQL. To consider Outer Join Operations, and their implementation in SQL.
Computing & Information Sciences Kansas State University Monday, 08 Sep 2008CIS 560: Database System Concepts Lecture 5 of 42 Monday, 08 September 2008.
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.
Advanced SQL: Triggers & Assertions
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Extended Relational-Algebra-Operations Generalized Projection Aggregate Functions Outer Join.
Relational Algebra Instructor: Mohamed Eltabakh 1.
Chapter 3: Relational Model III Additional Relational Algebra Operations Additional Relational Algebra Operations Views Views.
SQL: Structured Query Language Instructor: Mohamed Eltabakh 1 Part II.
Relational Algebra Instructor: Mohamed Eltabakh 1.
Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Subqueries CIS 4301 Lecture Notes Lecture /23/2006.
SQL: Structured Query Language Instructor: Mohamed Eltabakh 1.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
More SQL: Complex Queries,
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
Chapter 3 Introduction to SQL(3)
SQL Structured Query Language 11/9/2018 Introduction to Databases.
SQL : Query Language Part II CS3431.
CS 405G: Introduction to Database Systems
Database Applications (15-415) SQL-Part II Lecture 9, February 04, 2018 Mohammad Hammoud.
SQL Views CS542.
Functional Dependencies and Normalization
SQL : Query Language CS3431.
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
SQL Views and Updates cs3431.
Advanced SQL: Views & Triggers
Instructor: Mohamed Eltabakh
Instructor: Mohamed Eltabakh
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Instructor: Mohamed Eltabakh
SQL: Structured Query Language
Instructor: Mohamed Eltabakh
SQL: Structured Query Language
Normalization cs3431.
Instructor: Mohamed Eltabakh
Introduction to Database
SQL: Structured Query Language
SQL: Structured Query Language
Advanced Topics: Indexes & Transactions
SQL: Structured Query Language
Functional Dependencies and Normalization
Functional Dependencies and Normalization
CS 405G: Introduction to Database Systems
Presentation transcript:

SQL: Structured Query Language Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu

Aggregation + GroupBy

Possible Aggregations in SQL SELECT COUNT (*) FROM Student; SELECT COUNT (sNumber) FROM Student; SELECT MIN (sNumber) FROM Student; SELECT MAX (sNumber) FROM Student; SELECT SUM (sNumber) FROM Student; SELECT AVG (sNumber) FROM Student;

Grouping & Aggregation in SQL New optional clause called “GROUP BY” If the SELECT statement has “WHERE” Then WHERE conditions are evaluated first, then records are grouped Then count the records in each group And get the minimum gpa for each group SELECT pNumber, COUNT (sName), Min(gpa) FROM Student GROUP BY pNumber; First form groups for each pNumber

GROUP BY: Example I Student cnt  count(*) (Student) sNumber sName address pNumber 1 Dave 320FL 2 Greg 3 Matt 4 Jan 500MA cnt  count(*) (Student) pNumber,cnt count(*) ( (sNumber > 1) (Student)) SELECT count(*) AS CNT FROM Student; SELECT pNumber, count(*) AS CNT FROM Student WHERE sNumber > 1 GROUP BY pNumber; CNT 4 pNumber CNT 1 2

GROUP BY: Example II Student sNumber sName address pNumber 1 Dave 320FL 2 Greg 3 Matt 4 Jan 500MA pNumber,address, CNT  count(sName), SUM  sum(sNumber) ( (sNumber > 1) (Student)) SELECT pNumber,address, count(sName) AS CNT, sum(sNumber) AS SUM FROM Student WHERE sNumber >= 1 GROUP BY pNumber, address; pNumber address CNT SUM 1 320FL 2 3 500MA 4

Restrictions of GROUP BY If you group by A1, A2, …An, then any other column projected in SELECT clause must be inside an aggregation function SELECT pNumber, address, count(sName) AS CNT, sum(sNumber) AS SUM FROM Student WHERE sNumber > 1 GROUP BY pNumber, address; SELECT pNumber, address, sName, sum(sNumber) AS SUM FROM Student WHERE sNumber > 1 GROUP BY pNumber, address; X SELECT pNumber, count(sName) AS CNT, sum(sNumber) AS SUM FROM Student WHERE sNumber > 1 GROUP BY pNumber, address;

HAVING Clause: Putting Condition on Groups How to add conditions on each group? Select only the groups where the COUNT > 5 These conditions are after you build the groups (not before) Remember: WHERE conditions are executed before the groups are formed New optional clause called “HAVING”, added after the GROUP BY clause SELECT pNumber, COUNT (sName) FROM Student GROUP BY pNumber HAVING SUM(sNumber) > 2; Can reference aggregation inside HAVING

HAVING Clause: Example Student sNumber sName address pNumber 1 Dave 320FL 2 Greg 3 Matt 4 Jan 500MA  (SUM> 3) (pNumber,address, CNT  count(sName), SUM  sum(sNumber) ( (sNumber > 1) (Student))) SELECT pNumber,address, count(sName) AS CNT, sum(sNumber) AS SUM FROM Student WHERE sNumber > 1 GROUP BY pNumber, address HAVING sum(sNumber) > 3; Applied before the grouping Applied after the grouping on each group pNumber address CNT SUM 2 500MA 1 4

Example Queries Find customers having loans with sum > 20,000. Report the customer names and the total loans amount SELECT customer_name, sum(amount) AS SUM FROM Loan L, Borrower B WHERE L.loan_number = B. loan_number GROUP BY customer_name HAVING sum(amount) > 20,000;

Example Queries Find the cities with more than 3 branches SELECT branch_city FROM Branch GROUP BY branch_city HAVING count(branch_name) > 3;

Example Queries Report the largest and smallest loans given by each branch in NY city along with the branch name. SELECT branch_name, Max(amount) As MaxLoan, Min(amount) As MinLoan FROM Loan L, Branch B Where L.branch_name = B.branch_name AND branch_city = ‘NY’ GROUP BY branch_name;

SELECT Statement Clauses SELECT <projection list> FROM <relation names> WHERE <conditions> GROUP BY <grouping columns> HAVING <grouping conditions> ORDER BY <order columns>; optional Optional clauses if added must be in the order above Order of execution FROM  Check which relations are used WHERE  Filter records based on conditions GROUP BY  Form groups HAVING  Filter groups based on conditions ORDER BY  Sort the data SELECT  Form the projection list (output columns)

Example: Order of Execution Student sNumber sName address pNumber 1 Dave 320FL 2 Greg 3 Matt 4 Jan 500MA 5 SELECT pNumber,address, count(sName) AS CNT, sum(sNumber) AS SUM FROM Student WHERE sNumber > 1 GROUP BY pNumber, address HAVING sum(sNumber) > 3; 1 2 3 4 pNumber address CNT SUM 2 500MA 1 4

Questions optional SELECT <projection list> FROM <relation names> WHERE <conditions> GROUP BY <grouping columns> HAVING <grouping conditions> ORDER BY <order columns>; optional

More in SELECT Statement Special handling for NULL values Queries inside Insert/Update/Delete Nested subqueries

Null Values Null means ‘unknown’ value Any expression containing Null returns Null 5 + null  null ‘ABC’ || null  null Null in predicates returns UNKNOWN Predicates usually return TRUE or FALSE

Having Null in the data is problematic and needs special care… Example Student sNumber sName address pNumber 1 Dave 320FL 2 Greg null 3 Matt 4 Jan 500MA sNumber 1 2 3 SELECT sNumber FROM Student WHERE address = ‘320FL’; May or may not appear Having Null in the data is problematic and needs special care…

Use of “IS NULL” or “IS NOT NULL” Check if a value is null or not SELECT sNumber FROM Student WHERE address is null; Select student numbers where the address is null SELECT sNumber FROM Student WHERE address is not null AND address = ‘320FL’; Remember: SELECT sNumber FROM Student WHERE address = null; X The returned value here is unknown

Use of “NVL” Function NVL( exp1, exp2) If exp1 is null return exp2, otherwise return expr1 Can be used in projection list or in predicates SELECT sNumber FROM Student WHERE nvl(address, ‘n/a’) <> ‘n/a’ AND address ‘320FL’; SELECT sNumber, nvl(address, ‘N/A’) FROM Student; sNumber address 1 320FL 2 N/A 3 4 500MA

Null with Grouping & Aggregation Null is ignored with all aggregates, e.g., SUM, AVG, MIN, MAX except COUNT() Grouping Null is considered as a separate group

Example Student SELECT address, sum(pNumber) as sum, count(*) as cnt sNumber sName address pNumber 1 Dave 320FL 2 Greg null 3 Matt 4 Jan 500MA SELECT address, sum(pNumber) as sum, count(*) as cnt FROM Student GROUP BY address; address sum cnt 320FL 1 null 2 500MA

More in SELECT Statement Special handling for NULL values Queries inside Insert/Update/Delete Nested subqueries

Use of Select Inside Insert All records returned from the Select will be inserted Notice that there is no keyword “values” in this case INSERT INTO suppliers (supplier_id, supplier_name) SELECT account_no, name FROM externals Where code = 1; Number of columns and data types should match

Use of Select Inside Delete Student Registration sNumber sName address 1 Dave 320FL 2 Greg null 3 Matt 4 Jan 500MA sNumber courseID grade 4 DB1 A 2 F 3 DB2 Delete from Student all those who have grade ‘F’ Delete From Student Where sNumber in (Select sNumber from Registration Where grade = ‘F’); Students number 2 & 4 will be deleted…

Use of Select Inside Delete Student Registration sNumber sName address 1 Dave 320FL 2 Greg null 3 Matt 4 Jan 500MA sNumber courseID grade 4 DB1 A 2 F 3 DB2 Delete from Student all those who do not have registration Delete From Student Where sNumber not in (Select sNumber from Registration); Student number 1 will be deleted…

Use of Select Inside Update Student Registration sNumber sName address 1 Dave 320FL 2 Greg null 3 Matt 4 Jan 500MA sNumber courseID grade 4 DB1 A 2 F 3 DB2 Update the grades of student ‘Matt’ to B Update Registration Set grade = ‘B’ Where sNumber in (Select sNumber from Student Where sName =‘Matt’);

Remember… In Delete and Update From Student Where sNumber not in (Select sNumber from Registration); Update Registration Set grade = ‘B’ Where sNumber in (Select sNumber from Student Where sName =‘Matt’); In Delete and Update - The ‘From’ clause always has one table - The subquery can be only added in the ‘Where’ clause

More in SELECT Statement Special handling for NULL values Queries inside Insert/Update/Delete Nested subqueries

Nested Subquery SQL provides a mechanism for the nesting of subqueries. A subquery is a SELECT statement expression that is nested within another query Subquery can appear in FROM or WHERE clauses

Nested Subquery in FROM Clause SELECT * FROM Student, (inner SELECT) AS q WHERE … Table built on the fly Use the inner SELECT like any other table It is just built on the fly Inner SELECT can be a full statement with all clauses ORDER BY clause does not make sense in the inner select

Example Subquery 1 is computed on the fly It is treated as a normal table after that

Nested Subquery in WHERE Clause 2- Then, execute this statement once pNumber from the first step is known (outer SELECT) SELECT * FROM Student WHERE pNumber = (SELECT pNumber FROM Professor WHERE pName = ‘Mike’); 1- Execute this statement first to get the pNumber (inner SELECT) Since the predicates has = : The inner statement must return one record with one column In this case, DBMS will automatically convert the relation to a single scalar value Otherwise an error is generated

Example: Subqueries Retuning Scalar Value Student Professor sNumber sName address pNum 1 Dave 320FL 2 Greg 3 Matt pNumber pName address 1 MM 141FL 2 ER 201FL Select students of professor ‘MM’ SELECT sNumber, sName FROM Student WHERE pNum = (SELECT pNumber FROM Professor WHERE pName=‘MM’); sNumber sName 1 Dave 2 Greg CS3431

SubQuery Returning a Relation (General Case) Outer Select (S) SELECT sNumber, sName FROM Student WHERE pNum OP (SELECT pNumber FROM Professor WHERE pName=‘MM’); Inner Select (R) Predicates may include any of (OP above) : s in R  True if tuple s appears in R s not in R  True if tuple s does not appear in R s = R  R must return a single value (otherwise invalid op) Exists R  True if R is not empty Not Exists R  True if R is empty

Example 1: Subqueries Returning Relations Student Professor sNumber sName address pNum 1 Dave 320FL 2 Greg 3 Matt 4 Sam 30IN pNumber pName address 1 MM 141FL 2 ER 201FL 3 XY 30WA Select students of professors with address like ‘%FL’ SELECT sNumber, sName FROM Student WHERE pNum IN (SELECT pNumber FROM Professor WHERE address Like ‘%FL’); sNumber sName 1 Dave 2 Greg 3 Matt CS3431

Example 2: Subqueries Returning Relations Student Professor sNumber sName address pNum 1 Dave 320FL 2 Greg 3 Matt 4 Sam 30IN pNumber pName address 1 MM 141FL 2 ER 201FL 3 XY 30WA SELECT sNumber, sName FROM Student WHERE Exists (SELECT pNumber FROM Professor WHERE address Like ‘%FL’); sNumber sName 1 Dave 2 Greg 3 Matt 4 Sam Always true because it is not empty CS3431

Comparison Using ALL and ANY Outer Select (S) SELECT sNumber, sName FROM Student WHERE pNum OP (SELECT pNumber FROM Professor WHERE pName=‘MM’); Inner Select (R) We took: Exists, IN, NOT IN s > ALL R  True if s > all values in R s > ANY R  True if s > any value in R ‘>’ can be any of the other comparison operators, e.g., <, <=, >=, =, <> R must be relation with single column

Example Student Professor SELECT sNumber, sName FROM Student address pNum 1 Dave 320FL 2 Greg 3 Matt 4 Sam 30IN pNumber pName address 1 MM 141FL 2 ER 201FL 3 XY 30WA SELECT sNumber, sName FROM Student WHERE pNum >= ALL (SELECT pNumber FROM Professor WHERE address Like ‘%FL’); sNumber sName 3 Matt 4 Sam This inner select returns 1 , 2