![Page 1: CpSc 3220 The Language of SQL Chapters 10-12. Summarizing Data Most SQL functions apply to scalar arguments SUMMARY or AGGREGATE functions apply to rows](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649cfa5503460f949cbd35/html5/thumbnails/1.jpg)
CpSc 3220The Language of SQL
Chapters 10-12
![Page 2: CpSc 3220 The Language of SQL Chapters 10-12. Summarizing Data Most SQL functions apply to scalar arguments SUMMARY or AGGREGATE functions apply to rows](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649cfa5503460f949cbd35/html5/thumbnails/2.jpg)
Summarizing Data
• Most SQL functions apply to scalar arguments• SUMMARY or AGGREGATE functions apply to
rows of data
![Page 3: CpSc 3220 The Language of SQL Chapters 10-12. Summarizing Data Most SQL functions apply to scalar arguments SUMMARY or AGGREGATE functions apply to rows](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649cfa5503460f949cbd35/html5/thumbnails/3.jpg)
Some Aggregate Functions
• DISTINCT• SUM• AVG• MIN• MAX• COUNT
![Page 4: CpSc 3220 The Language of SQL Chapters 10-12. Summarizing Data Most SQL functions apply to scalar arguments SUMMARY or AGGREGATE functions apply to rows](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649cfa5503460f949cbd35/html5/thumbnails/4.jpg)
SQL Commands Used in Aggregations
• GROUP BY• HAVING
![Page 5: CpSc 3220 The Language of SQL Chapters 10-12. Summarizing Data Most SQL functions apply to scalar arguments SUMMARY or AGGREGATE functions apply to rows](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649cfa5503460f949cbd35/html5/thumbnails/5.jpg)
Syntax of the SELECT statementSELECT [ALL | DISTINCT | DISTINCTROW ] [HIGH_PRIORITY] [STRAIGHT_JOIN] [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT] [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS] select_expr [, select_expr ...] [FROM table_references [WHERE where_condition] [GROUP BY {col_name | expr | position} [ASC | DESC], ... [WITH ROLLUP]] [HAVING where_condition] [ORDER BY {col_name | expr | position} [ASC | DESC], ...] [LIMIT {[offset,] row_count | row_count OFFSET offset}] [PROCEDURE procedure_name(argument_list)] [INTO OUTFILE 'file_name' [CHARACTER SET charset_name] export_options | INTO DUMPFILE 'file_name' | INTO var_name [, var_name]] [FOR UPDATE | LOCK IN SHARE MODE]]
![Page 6: CpSc 3220 The Language of SQL Chapters 10-12. Summarizing Data Most SQL functions apply to scalar arguments SUMMARY or AGGREGATE functions apply to rows](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649cfa5503460f949cbd35/html5/thumbnails/6.jpg)
Eliminating Duplicates with DISTINCT
• Some SQL statements will generate result tables that contain duplicate rows
• Adding the DISTINCT keyword immediately after the SELECT command eliminates duplicates
• Example:SELECT DISTINCT artist from
SongTitles;
![Page 7: CpSc 3220 The Language of SQL Chapters 10-12. Summarizing Data Most SQL functions apply to scalar arguments SUMMARY or AGGREGATE functions apply to rows](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649cfa5503460f949cbd35/html5/thumbnails/7.jpg)
Aggregate Functions
• Most aggregate functions apply only to numeric column data– SUM– AVG– MIN– MAX– COUNT– GROUP_CONCAT
![Page 8: CpSc 3220 The Language of SQL Chapters 10-12. Summarizing Data Most SQL functions apply to scalar arguments SUMMARY or AGGREGATE functions apply to rows](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649cfa5503460f949cbd35/html5/thumbnails/8.jpg)
The COUNT Function
• Three different usagesSelect count(*) from Grades;
Select count(Grade) from Grades where gradeType=‘homework’;
Select count(DISTINCT FeeType) from Fees;
![Page 9: CpSc 3220 The Language of SQL Chapters 10-12. Summarizing Data Most SQL functions apply to scalar arguments SUMMARY or AGGREGATE functions apply to rows](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649cfa5503460f949cbd35/html5/thumbnails/9.jpg)
Grouping Data
• SQL allows the grouping of rows by column values
• For example, we might want to group student records by Major code and apply summary functions to each group of rows with the same Major code value
• The GROUP BY clause handles this
![Page 10: CpSc 3220 The Language of SQL Chapters 10-12. Summarizing Data Most SQL functions apply to scalar arguments SUMMARY or AGGREGATE functions apply to rows](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649cfa5503460f949cbd35/html5/thumbnails/10.jpg)
GROUP BY Example
SELECT GradeType ,AVG(Grade) FROM GradesGROUP BY GradeTypeORDER BY GradeType;
![Page 11: CpSc 3220 The Language of SQL Chapters 10-12. Summarizing Data Most SQL functions apply to scalar arguments SUMMARY or AGGREGATE functions apply to rows](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649cfa5503460f949cbd35/html5/thumbnails/11.jpg)
Multiple Columns and Sorting
• Groups can be based on more than one column
• Example:SELECT GradeType,Student,AVG(Grade)FROM GradesGROUP BY GradeType,StudentORDER BY GradeType,Student
![Page 12: CpSc 3220 The Language of SQL Chapters 10-12. Summarizing Data Most SQL functions apply to scalar arguments SUMMARY or AGGREGATE functions apply to rows](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649cfa5503460f949cbd35/html5/thumbnails/12.jpg)
Group Conditions
• Conditions can be used for Groups but not with a WHERE clause; a HAVING clause is used instead
• Example:SELECT GradeType,Student,AVG(Grade)FROM GradesGROUP BY GradeType,StudentHAVING AVG(Grade) >= 70ORDER BY GradeType,Student
![Page 13: CpSc 3220 The Language of SQL Chapters 10-12. Summarizing Data Most SQL functions apply to scalar arguments SUMMARY or AGGREGATE functions apply to rows](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649cfa5503460f949cbd35/html5/thumbnails/13.jpg)
Selection Criteria on Aggregates
SELECT colList1FROM tableListWHERE conditionForIncludionInTableGROUP BY colList2HAVING conditionForInclusionInGroupORDER BY colList3
![Page 14: CpSc 3220 The Language of SQL Chapters 10-12. Summarizing Data Most SQL functions apply to scalar arguments SUMMARY or AGGREGATE functions apply to rows](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649cfa5503460f949cbd35/html5/thumbnails/14.jpg)
The Full Select Command
SELECT exp_list1FROM table_listWHERE condition_expGROUP BY exp_list2HAVING condition_expORDER BY exp_list3
![Page 15: CpSc 3220 The Language of SQL Chapters 10-12. Summarizing Data Most SQL functions apply to scalar arguments SUMMARY or AGGREGATE functions apply to rows](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649cfa5503460f949cbd35/html5/thumbnails/15.jpg)
Combining Tables
• The previous slides have used the term tableList in the WHERE clause but we have only generated queries for a single table
• Tables can and will be combined in most meaningful queries
• The simplest way of combining tables is by Cartesian Product; attach every row from Table1 to every row from Table2
![Page 16: CpSc 3220 The Language of SQL Chapters 10-12. Summarizing Data Most SQL functions apply to scalar arguments SUMMARY or AGGREGATE functions apply to rows](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649cfa5503460f949cbd35/html5/thumbnails/16.jpg)
Combining Tables with INNER JOIN
• ExampleSELECT colList
FROM table1 INNER JOIN table2 ON joinCondition[INNER JOIN table2 ON joinCondition]. . .WHERE colListSelectionCondition
![Page 17: CpSc 3220 The Language of SQL Chapters 10-12. Summarizing Data Most SQL functions apply to scalar arguments SUMMARY or AGGREGATE functions apply to rows](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649cfa5503460f949cbd35/html5/thumbnails/17.jpg)
Alternate Notation
SELECT colListFROM table1 [, table2 ] . . .WHERE joinAndColListSelectionCondition
![Page 18: CpSc 3220 The Language of SQL Chapters 10-12. Summarizing Data Most SQL functions apply to scalar arguments SUMMARY or AGGREGATE functions apply to rows](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649cfa5503460f949cbd35/html5/thumbnails/18.jpg)
Combining Tales with OUTER JOIN
• OUTER JOINs allow the creation of a entry in the combined table even when a matching row is not found in one of the tables to be joined. The column entries for the missing data are all set to NULL
![Page 19: CpSc 3220 The Language of SQL Chapters 10-12. Summarizing Data Most SQL functions apply to scalar arguments SUMMARY or AGGREGATE functions apply to rows](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649cfa5503460f949cbd35/html5/thumbnails/19.jpg)
LEFT OUTER JOIN
SELECT colListFROM table1 LEFT [OUTER] JOIN table2 ON joinCondition[LEFT [OUTER] JOIN table3 ON joinCondition] . . .
![Page 20: CpSc 3220 The Language of SQL Chapters 10-12. Summarizing Data Most SQL functions apply to scalar arguments SUMMARY or AGGREGATE functions apply to rows](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649cfa5503460f949cbd35/html5/thumbnails/20.jpg)
RIGHT JOIN
SELECT colListFROM table1 RIGHT [OUTER] JOIN table2 ON joinCondition[RIGHT [OUTER] JOIN table3 ON joinCondition]. . .
![Page 21: CpSc 3220 The Language of SQL Chapters 10-12. Summarizing Data Most SQL functions apply to scalar arguments SUMMARY or AGGREGATE functions apply to rows](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649cfa5503460f949cbd35/html5/thumbnails/21.jpg)
Table Order in OUTER JOINs
• LEFT and RIGHT and INNER JOINs can be combined in a single statement
• Be careful to make sure multiple JOINs are done in the desired sequence
• A LEFT and a RIGHT combination is a FULL JOIN
![Page 22: CpSc 3220 The Language of SQL Chapters 10-12. Summarizing Data Most SQL functions apply to scalar arguments SUMMARY or AGGREGATE functions apply to rows](https://reader036.vdocuments.site/reader036/viewer/2022062421/56649cfa5503460f949cbd35/html5/thumbnails/22.jpg)
Full Joins
• Not allowed in MySQL