chapter seven (part 2) multiple row functions:
DESCRIPTION
Chapter Seven (part 2) Multiple Row Functions:. Objectives: -Multiple row functions -Ordering -Grouping -Concept of JOIN. Aggregate Functions:. MAX (DISTINCT | ALL) (value) MIN (DISTINCT | ALL) (value) AVG(DISTINCT | ALL) (value) SUM (DISTINCT | ALL) (value) - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Chapter Seven (part 2) Multiple Row Functions:](https://reader030.vdocuments.mx/reader030/viewer/2022032805/56813194550346895d9806fc/html5/thumbnails/1.jpg)
1
Chapter Seven (part 2)
Multiple Row Functions:
Objectives:-Multiple row
functions-Ordering-Grouping-Concept of JOIN
![Page 2: Chapter Seven (part 2) Multiple Row Functions:](https://reader030.vdocuments.mx/reader030/viewer/2022032805/56813194550346895d9806fc/html5/thumbnails/2.jpg)
2
Aggregate Functions:
MAX (DISTINCT | ALL) (value) MIN (DISTINCT | ALL) (value) AVG (DISTINCT | ALL) (value) SUM (DISTINCT | ALL) (value) COUNT (DISTINCT | ALL) (value)* STDDEV (DISTINCT | ALL) (value) VARIANCE (DISTINCT | ALL) (value)
![Page 3: Chapter Seven (part 2) Multiple Row Functions:](https://reader030.vdocuments.mx/reader030/viewer/2022032805/56813194550346895d9806fc/html5/thumbnails/3.jpg)
3
Aggregate Functions:
List the highest GPA
SELECT MAX (GPA)FROM Student;
![Page 4: Chapter Seven (part 2) Multiple Row Functions:](https://reader030.vdocuments.mx/reader030/viewer/2022032805/56813194550346895d9806fc/html5/thumbnails/4.jpg)
4
Aggregate Functions:
List average, max, min, and total salary of cosc faculty
SELECT AVG(salary), MIN(salary), MAX(salary), SUM(salary)
FROM faculty WHERE dept = ‘COSC’;
![Page 5: Chapter Seven (part 2) Multiple Row Functions:](https://reader030.vdocuments.mx/reader030/viewer/2022032805/56813194550346895d9806fc/html5/thumbnails/5.jpg)
5
Aggregate Functions:
List average salary of cosc faculty:
SELECT AVG( NVL(salary,0)),COUNT(*)
FROM faculty WHERE dept = ‘COSC’;
![Page 6: Chapter Seven (part 2) Multiple Row Functions:](https://reader030.vdocuments.mx/reader030/viewer/2022032805/56813194550346895d9806fc/html5/thumbnails/6.jpg)
6
Practice:
Find Average, and sum of quoted price from order form. Also the number of orders.
![Page 7: Chapter Seven (part 2) Multiple Row Functions:](https://reader030.vdocuments.mx/reader030/viewer/2022032805/56813194550346895d9806fc/html5/thumbnails/7.jpg)
7
Practice:
Find the highest quoted price from order form. Order number must be in this range :2000 to 3000.
![Page 8: Chapter Seven (part 2) Multiple Row Functions:](https://reader030.vdocuments.mx/reader030/viewer/2022032805/56813194550346895d9806fc/html5/thumbnails/8.jpg)
8
Distinct:
SELECT DISTinct (dept)FROM Faculty;
![Page 9: Chapter Seven (part 2) Multiple Row Functions:](https://reader030.vdocuments.mx/reader030/viewer/2022032805/56813194550346895d9806fc/html5/thumbnails/9.jpg)
9
Practice:
How many customers have an order on March, 10 2001?
![Page 10: Chapter Seven (part 2) Multiple Row Functions:](https://reader030.vdocuments.mx/reader030/viewer/2022032805/56813194550346895d9806fc/html5/thumbnails/10.jpg)
10
Ordering ORDERING: (Default is Ascending ASC)
List students name in an alphabetic order
SELECT nameFROM studentORDER BY name;
ORDER BY Name , GPA DESC;
![Page 11: Chapter Seven (part 2) Multiple Row Functions:](https://reader030.vdocuments.mx/reader030/viewer/2022032805/56813194550346895d9806fc/html5/thumbnails/11.jpg)
11
Ordering
List of the faculty salary for the next year with 5% increase order by new salary.
SELECT name,
salary pay, salary+salary*0.05 AS
new_salaryFROM facultyORDER BY new_salary;
![Page 12: Chapter Seven (part 2) Multiple Row Functions:](https://reader030.vdocuments.mx/reader030/viewer/2022032805/56813194550346895d9806fc/html5/thumbnails/12.jpg)
12
Practice:
Find the list of customers: last name, first name, balance, credit line and birth date; order the data by birth date from older to younger and balance from lowest to highest.
![Page 13: Chapter Seven (part 2) Multiple Row Functions:](https://reader030.vdocuments.mx/reader030/viewer/2022032805/56813194550346895d9806fc/html5/thumbnails/13.jpg)
13
Grouping
SELECTFROM[WHERE][GROUP BY][ORDER BY]
![Page 14: Chapter Seven (part 2) Multiple Row Functions:](https://reader030.vdocuments.mx/reader030/viewer/2022032805/56813194550346895d9806fc/html5/thumbnails/14.jpg)
14
Grouping Average Salary of faculty members
by department
SELECT dept, AVG (Salary)FROM FacultyGROUP BY dept;
![Page 15: Chapter Seven (part 2) Multiple Row Functions:](https://reader030.vdocuments.mx/reader030/viewer/2022032805/56813194550346895d9806fc/html5/thumbnails/15.jpg)
15
Grouping
List number of courses taken by each student
SELECT ID, COUNT(*)FROM Student_CourseGROUP BY ID;
![Page 16: Chapter Seven (part 2) Multiple Row Functions:](https://reader030.vdocuments.mx/reader030/viewer/2022032805/56813194550346895d9806fc/html5/thumbnails/16.jpg)
16
Grouping by multiple attributes List total number of credits taken by
each studentSELECT ID, SUM(Cr)FROM Student_CourseGROUP BY ID;
SELECT ID, semester, SUM(Cr)FROM Student_CourseGROUP BY ID, semester;
SELECT dept, count(name)FROM facultyGROUP BY dept;
![Page 17: Chapter Seven (part 2) Multiple Row Functions:](https://reader030.vdocuments.mx/reader030/viewer/2022032805/56813194550346895d9806fc/html5/thumbnails/17.jpg)
17
Practice:
List of customer last names and first names for each customer state.
![Page 18: Chapter Seven (part 2) Multiple Row Functions:](https://reader030.vdocuments.mx/reader030/viewer/2022032805/56813194550346895d9806fc/html5/thumbnails/18.jpg)
18
Having
Condition on Group:
SELECTFROM[WHERE][GROUP BY][HAVING][ORDER BY]
![Page 19: Chapter Seven (part 2) Multiple Row Functions:](https://reader030.vdocuments.mx/reader030/viewer/2022032805/56813194550346895d9806fc/html5/thumbnails/19.jpg)
19
Having
List ID of students who have more than 20 credits and majoring in COSC.
SELECT ID
FROM Student_CourseWHERE Major = 'COSC' GROUP BY IDHAVING SUM(Cr) > 20 ;
![Page 20: Chapter Seven (part 2) Multiple Row Functions:](https://reader030.vdocuments.mx/reader030/viewer/2022032805/56813194550346895d9806fc/html5/thumbnails/20.jpg)
20
Having
SELECT dept, MAX(salary)FROM facultyGROUP BY deptHAVING MAX(salary)>50000;
![Page 21: Chapter Seven (part 2) Multiple Row Functions:](https://reader030.vdocuments.mx/reader030/viewer/2022032805/56813194550346895d9806fc/html5/thumbnails/21.jpg)
21
Having
SELECT dept, MAX(salary)FROM facultyGROUP BY deptHAVINGMAX(salary)>50000ORDER BY MAX(salary);
![Page 22: Chapter Seven (part 2) Multiple Row Functions:](https://reader030.vdocuments.mx/reader030/viewer/2022032805/56813194550346895d9806fc/html5/thumbnails/22.jpg)
22
Having
SELECT dept, AVG(MAX(salary)-MIN(salary))FROM facultyGROUP BY dept;
![Page 23: Chapter Seven (part 2) Multiple Row Functions:](https://reader030.vdocuments.mx/reader030/viewer/2022032805/56813194550346895d9806fc/html5/thumbnails/23.jpg)
23
Illegal Queries: SELECT name, count(*)
FROM department;
SELECT name, count(*) FROM department GROUP BY name; SELECT name, AVG(salary) FROM department WHERE AVG(salary) >5000;
SELECT name, AVG(salary) FROM department GROUP BY name HAVING AVG(salary) >5000;
![Page 24: Chapter Seven (part 2) Multiple Row Functions:](https://reader030.vdocuments.mx/reader030/viewer/2022032805/56813194550346895d9806fc/html5/thumbnails/24.jpg)
24
Practice:
List each SalesRepNumber with their total customer’s balance.
![Page 25: Chapter Seven (part 2) Multiple Row Functions:](https://reader030.vdocuments.mx/reader030/viewer/2022032805/56813194550346895d9806fc/html5/thumbnails/25.jpg)
25
Practice:
List each SalesRepNumber with their total customer’s balance, only for customers with balance of $1000 or more.
![Page 26: Chapter Seven (part 2) Multiple Row Functions:](https://reader030.vdocuments.mx/reader030/viewer/2022032805/56813194550346895d9806fc/html5/thumbnails/26.jpg)
26
Practice:
List each SalesRepNumber with their total customer’s balance, only if the total SalesRepNumber balance is less than $100,000, for customers with balance of $1000 or more.
![Page 27: Chapter Seven (part 2) Multiple Row Functions:](https://reader030.vdocuments.mx/reader030/viewer/2022032805/56813194550346895d9806fc/html5/thumbnails/27.jpg)
27
JOIN: Definition General Format:
SELECT col1,col2,….FROM table1, table2,…WHERE conditions;
![Page 28: Chapter Seven (part 2) Multiple Row Functions:](https://reader030.vdocuments.mx/reader030/viewer/2022032805/56813194550346895d9806fc/html5/thumbnails/28.jpg)
28
JOIN: List of student’s name with the grade
= 'A' SELECT Name
FROM Student_Course, StudentWHERE Student.ID =
Student_Course.ID and Grade =‘A’;
![Page 29: Chapter Seven (part 2) Multiple Row Functions:](https://reader030.vdocuments.mx/reader030/viewer/2022032805/56813194550346895d9806fc/html5/thumbnails/29.jpg)
29
JOIN: Aliases: FROM Student a, Student b WHERE a.ID > b.ID
![Page 30: Chapter Seven (part 2) Multiple Row Functions:](https://reader030.vdocuments.mx/reader030/viewer/2022032805/56813194550346895d9806fc/html5/thumbnails/30.jpg)
30
CARTESIAN PRODUCT:
Join condition is omitted Join condition is invalid All rows in table one are joined to all
rows in table two
SELECT *FROM Student, faculty;
![Page 31: Chapter Seven (part 2) Multiple Row Functions:](https://reader030.vdocuments.mx/reader030/viewer/2022032805/56813194550346895d9806fc/html5/thumbnails/31.jpg)
31
JOIN Equijoin:
SELECT NameFROM Student_Course, StudentWHERE Student.ID = Student_Course.ID ;
SELECT department.num_faculty,faculty.name
FROM department, facultyWHERE department.name=faculty.dept;
![Page 32: Chapter Seven (part 2) Multiple Row Functions:](https://reader030.vdocuments.mx/reader030/viewer/2022032805/56813194550346895d9806fc/html5/thumbnails/32.jpg)
32
Practice:
Find the list of customers: Last name, first name, balance and the city where their sales person lives.
![Page 33: Chapter Seven (part 2) Multiple Row Functions:](https://reader030.vdocuments.mx/reader030/viewer/2022032805/56813194550346895d9806fc/html5/thumbnails/33.jpg)
33
JOIN Non-Equijoin:
Faculty (name, salary)Status (rank, low_salry, high_salary)
Get the name, salary and rank of faculty members
SELECT name, salary, rankFROM faculty, statusWHERE salary BETWEEN low_salary AND high_salary;
![Page 34: Chapter Seven (part 2) Multiple Row Functions:](https://reader030.vdocuments.mx/reader030/viewer/2022032805/56813194550346895d9806fc/html5/thumbnails/34.jpg)
34
JOIN Outer Join:List of students who did take courses.
SELECT nameFROM student, student_courseWHERE student.id =student_course.id (+);
SELECT department.num_faculty, faculty.nameFROM department, facultyWHERE department.name=faculty.dept(+)
![Page 35: Chapter Seven (part 2) Multiple Row Functions:](https://reader030.vdocuments.mx/reader030/viewer/2022032805/56813194550346895d9806fc/html5/thumbnails/35.jpg)
35
Practice:
Find the list of customers: Last name, first name, who did not ordered some items in year 2004;
Assume there is a table called customer_order with attributes: c_number, o_number
![Page 36: Chapter Seven (part 2) Multiple Row Functions:](https://reader030.vdocuments.mx/reader030/viewer/2022032805/56813194550346895d9806fc/html5/thumbnails/36.jpg)
36
JOIN
Self Join
SELECT a.NameFROM Student a, Student bWHERE a.ID > b.ID AND
b.Name = ‘SMITH';
What is the output of this query
![Page 37: Chapter Seven (part 2) Multiple Row Functions:](https://reader030.vdocuments.mx/reader030/viewer/2022032805/56813194550346895d9806fc/html5/thumbnails/37.jpg)
37
JOIN Self Join List of Faculty member with salary
higher than salary of Mary and less than salary of John
SELECT a.NameFROMFaculty a, Faculty b, Faculty cWHERE a.Salary > b.Salary AND
a.Salary < c.Salary ANDb.Name = 'MARY' ANDc.Name = ‘JOHN’;
![Page 38: Chapter Seven (part 2) Multiple Row Functions:](https://reader030.vdocuments.mx/reader030/viewer/2022032805/56813194550346895d9806fc/html5/thumbnails/38.jpg)
38
Practice:
Find the list of customers: Last name, first name, who are reside in the same city as Ms joy Smith