1 SQL-3 Tarek El-Shishtawy Professor Ass. Of Computer Engineering.

Slides:



Advertisements
Similar presentations
Union, Intersection, Difference (subquery) UNION (subquery) produces the union of the two relations. Similarly for INTERSECT, EXCEPT = intersection and.
Advertisements

พีชคณิตแบบสัมพันธ์ (Relational Algebra) บทที่ 3 อ. ดร. ชุรี เตชะวุฒิ CS (204)321 ระบบฐานข้อมูล 1 (Database System I)
1 Advanced SQL Queries. 2 Example Tables Used Reserves sidbidday /10/04 11/12/04 Sailors sidsnameratingage Dustin Lubber Rusty.
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs WHERE SUM (duration) = 100; (this will give an error)
Data Warehousing/Mining 1 Data Warehousing/Mining Comp 150 Aggregation in SQL (not in book) Instructor: Dan Hebert.
4 การใช้ SQL Functions. Copyright © 2007, Oracle. All rights reserved What Are Group Functions? Group functions operate on sets of rows to give.
Chapter 11 Group Functions
Chapter 11 Group Functions (up to p.402)
Oct 28, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11.
Database Systems Chapter 6 ITM Relational Algebra The basic set of operations for the relational model is the relational algebra. –enable the specification.
Nov 24, 2003Murali Mani SQL B term 2004: lecture 12.
Introduction to Oracle9i: SQL1 SQL Group Functions.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
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,
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #3.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Winter 2002Arthur Keller – CS 1807–1 Schedule Today: Jan. 24 (TH) u Subqueries, Grouping and Aggregation. u Read Sections Project Part 2 due.
Chapter 11.1 and 11.2 Data Manipulation: Relational Algebra and SQL Brian Cobarrubia Introduction to Database Management Systems October 4, 2007.
Structured Query Language SQL: An Introduction. SQL (Pronounced S.Q.L) The standard user and application program interface to a relational database is.
Concepts of Database Management, Fifth Edition
SQL 資料庫查詢語言 取材自 EIS, 3 rd edition By Dunn et al..
Chapter 6 Group Functions. Chapter Objectives  Differentiate between single-row and multiple-row functions  Use the SUM and AVG functions for numeric.
Chapter 3 Single-Table Queries
Chapter 3 Section 3.4 Relational Database Operators
Relational Model Concepts. The relational model represents the database as a collection of relations. Each relation resembles a table of values. A table.
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Using Special Operators (LIKE and IN)
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
Information Technologies and Microsoft SQL Server Day 2 by Alper Özpınar
SQL-5 (Group By.. Having). Group By  Need: To apply the aggregate functions to subgroups of tuples in a relation, where the subgroups are based on some.
CS424 Relational Data Manipulation Relational Data Manipulation Relational tables are sets. Relational tables are sets. The rows of the tables can be considered.
Intermediate SQL: Aggregated Data, Joins and Set Operators.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Lecture 8 – SQL Joins – assemble new views from existing tables INNER JOIN’s The Cartesian Product Theta Joins and Equi-joins Self Joins Natural Join.
SQL Aggregation Oracle and ANSI Standard SQL Lecture 9.
1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus.
Course title: Database-ii Chap No: 03 “Advanced SQL” Course instructor: ILTAF MEHDI.
SCUHolliday - coen 1787–1 Schedule Today: u Subqueries, Grouping and Aggregation. u Read Sections Next u Modifications, Schemas, Views. u Read.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
April 2002 Information Systems Design John Ogden & John Wordsworth 1 Database Design SQL (1) John Wordsworth Department of Computer Science The University.
1 Chapter 3 Single Table Queries. 2 Simple Queries Query - a question represented in a way that the DBMS can understand Basic format SELECT-FROM Optional.
1 Ch. 11: Grouping Things Together  ANSI standard SQL Group functions: AVG, COUNT, MAX, MIN, STDDEV, SUM, VARIANCE  Others: 8i: GROUPING (used with CUBE.
5-1 Copyright © 2004, Oracle. All rights reserved. DISPLAYING DATA FROM MULTIPLE TABLES OUTER JOIN.
Structured Query Language
More SQL: Complex Queries,
CS3220 Web and Internet Programming More SQL
Structured Query Language (Data Manipulation Language)
Chapter 3 Introduction to SQL(3)
Databases : More about SQL
Schedule Today: Next After that Subqueries, Grouping and Aggregation.
CS 440 Database Management Systems
آزمایشگاه پایگاه داده ها دستورات مهم SQL در SQL Server
MENAMPILKAN DATA DARI SATU TABEL (Chap 2)
The Relational Algebra and Relational Calculus
CS 3630 Database Design and Implementation
SQL – Entire Select.
Chapter 4 Summary Query.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
CS4222 Principles of Database System
CS122 Using Relational Databases and SQL
Query Functions.
Section 4 - Sorting/Functions
Presentation transcript:

1 SQL-3 Tarek El-Shishtawy Professor Ass. Of Computer Engineering

2 Aggregate Functions COUNT : to count the number of rows of the relation COUNT : to count the number of rows of the relation MAX : to find the maximum value of the attribute (column) MAX : to find the maximum value of the attribute (column) MIN : to find the minimum value of the attribute MIN : to find the minimum value of the attribute SUM : to find the sum of values of the attribute provided the data type of the attribute is number SUM : to find the sum of values of the attribute provided the data type of the attribute is number AVG : to find the average of n values, ignoring null values AVG : to find the average of n values, ignoring null values STDDEV: standard deviation of n values ignoring null values STDDEV: standard deviation of n values ignoring null values VARIANCE : variance of n values ignoring null values VARIANCE : variance of n values ignoring null values

3 COUNT SELECT COUNT (*) SELECT COUNT (*) FROM table name; FROM table name; Returns No of rows of a relation Returns No of rows of a relation SELECT COUNT (attribute name) SELECT COUNT (attribute name) FROM table name; FROM table name; Returns No of rows of a relation Returns No of rows of a relation SELECT COUNT (DISTINCT attribute name) SELECT COUNT (DISTINCT attribute name) FROM table name; FROM table name; returns the number of returns the number of rows of the relation, by eliminating duplicate values. rows of the relation, by eliminating duplicate values.

4 MAX Command SELECT MAX (attribute name) FROM table name; to get the maximum price of the product Select max(Price) From consumer_product; To get the product name with maximum price Table Consumer product Name Price TV 15,000 refrigerator 10,000 washing machine 17,000 mixer 3,500 Select Name from Select Name from Consumer_product Where price in (select max(price) from consumer_product);

5 Min Command SELECT MIN (attribute name) FROM table name; to get the Minimum price of the product Select min(Price) From consumer_product; To get the product name with minimum price Table Consumer product Name Price TV 15,000 refrigerator 10,000 washing machine 17,000 mixer 3,500 Select Name from Select Name from Consumer_product Where price in (select min(price) from consumer_product);

6 AVG Command The AVG command is used to get the average value of an attribute. The syntax of AVG command is: The AVG command is used to get the average value of an attribute. The syntax of AVG command is: SELECT AVG (attribute name) SELECT AVG (attribute name) FROM table name; FROM table name;

7 GROUP BY Function The GROUP BY clause is used to group rows to compute group-statistics. The GROUP BY clause is used to group rows to compute group-statistics. It is to be noted that when the GROUP BY clause is present, then It is to be noted that when the GROUP BY clause is present, then the SELECT clause may include only the columns that appear in the GROUP BY clause the SELECT clause may include only the columns that appear in the GROUP BY clause and aggregate functions. and aggregate functions. The general Form The general Form SELECT attribute name, aggregate function FROM table name GROUP BY attribute name;

8 Example Group by

9 Having Command The HAVING command is used to select the group. In other words HAVING restricts the groups according to a specified condition. The HAVING command is used to select the group. In other words HAVING restricts the groups according to a specified condition. The syntax of HAVING command is: The syntax of HAVING command is: SELECT attribute name, aggregate function FROM table name GROUP BY attribute name HAVING condition;

10 Example find the details of the department in which more than 90 students got placement find the details of the department in which more than 90 students got placement

11 Join Operation Join operation is used to retrieve data from more than one table. Join operation is used to retrieve data from more than one table. Cartesian Product Cartesian Product If we have two tables A and B, then Cartesian product combines all rows in the table A with all rows in the table B. If we have two tables A and B, then Cartesian product combines all rows in the table A with all rows in the table B. If n1 is the number of rows in the table A and n2 is the number of rows in the table B. If n1 is the number of rows in the table A and n2 is the number of rows in the table B. Then the Cartesian product between A and B will have n1 × n2 rows. Then the Cartesian product between A and B will have n1 × n2 rows.

12 Example The relation doctor has the attribute ID, name and department. The relation doctor has the attribute ID, name and department. The relation nurse has three attributes NID, name and department. The relation nurse has three attributes NID, name and department.

13 Joints Cartesian Product Cartesian Product Select * from Doctor, Nurse; Select * from Doctor, Nurse; Gives all fields and all rows (12 Rows) Gives all fields and all rows (12 Rows)

14 Equijoin The join condition is based on equality between values in the common columns. The join condition is based on equality between values in the common columns. Called also: Called also: simple joins or inner joins simple joins or inner joins

15 Outer Joint In inner join, we select rows common to the participating tables to a join. In inner join, we select rows common to the participating tables to a join. What about the cases where we are interested in selecting elements in a table regardless of whether they are present in the second table? What about the cases where we are interested in selecting elements in a table regardless of whether they are present in the second table? We will now need to use the SQL OUTER JOIN command We will now need to use the SQL OUTER JOIN command

16 Outer Joint The syntax for performing an outer join in SQL is database-dependent. The syntax for performing an outer join in SQL is database-dependent. For example, in Oracle, we will place an "(+)" in the WHERE clause on the other side of the table for which we want to include all the rows For example, in Oracle, we will place an "(+)" in the WHERE clause on the other side of the table for which we want to include all the rows

17 Example Tab;e Store_Information store_name Sales Date Los Angeles $1500 Jan San Diego $250 Jan Los Angeles $300 Jan Boston $700 Jan Table Geography region_namestore_name EastBoston EastNew York WestLos Angeles WestSan Diego

18 Example SELECT A1.store_name, SUM(A2.Sales) SALES SELECT A1.store_name, SUM(A2.Sales) SALES FROM Geography A1, Store_Information A2 FROM Geography A1, Store_Information A2 WHERE A1.store_name = A2.store_name (+) WHERE A1.store_name = A2.store_name (+) GROUP BY A1.store_name GROUP BY A1.store_name store_name SALES store_name SALES Boston $700 Boston $700 New York New York Los Angeles $1800 Los Angeles $1800 San Diego $250 San Diego $250

19 Set Operations UNION, INTERSECTION, and the MINUS (Difference) operations are considered as SET operations. UNION, INTERSECTION, and the MINUS (Difference) operations are considered as SET operations. DELL_ DESKTOP IBM_DESKTOP

20 UNION command The union of two relations IBM DESKTOP, DELL DESKTOP is given The union of two relations IBM DESKTOP, DELL DESKTOP is given UNION command eliminates duplicate values. UNION command eliminates duplicate values. In order to get the duplicate In order to get the duplicate values, we can use UNION ALL command values, we can use UNION ALL command

21 INTERSECTION Operation The intersection operation returns the tuples that are common to the two relations. The intersection operation returns the tuples that are common to the two relations.

22 MINUS Operation If R and S are two union compatible relations then If R and S are two union compatible relations then R–S returns the tuples that are present in R but not in S. R–S returns the tuples that are present in R but not in S. S–R returns the tuples that are present in S but not in R. S–R returns the tuples that are present in S but not in R. It is to be noted that MINUS operation is not commutative That is R–S # S–R. It is to be noted that MINUS operation is not commutative That is R–S # S–R.

23 Minus