1 / 22

CpSc 3220 The Language of SQL

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 of data. Some Aggregate Functions. DISTINCT SUM AVG MIN MAX COUNT. SQL Commands Used in Aggregations. GROUP BY HAVING.

river
Download Presentation

CpSc 3220 The Language of SQL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CpSc 3220The Language of SQL Chapters 10-12

  2. Summarizing Data • Most SQL functions apply to scalar arguments • SUMMARY or AGGREGATE functions apply to rows of data

  3. Some Aggregate Functions • DISTINCT • SUM • AVG • MIN • MAX • COUNT

  4. SQL Commands Used in Aggregations • GROUP BY • HAVING

  5. Syntax of the SELECT statement SELECT [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]]

  6. 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;

  7. Aggregate Functions • Most aggregate functions apply only to numeric column data • SUM • AVG • MIN • MAX • COUNT • GROUP_CONCAT

  8. The COUNT Function • Three different usages Select count(*) from Grades; Select count(Grade) from Grades where gradeType=‘homework’; Select count(DISTINCT FeeType) from Fees;

  9. 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

  10. GROUP BY Example SELECT GradeType ,AVG(Grade) FROM Grades GROUP BY GradeType ORDER BY GradeType;

  11. Multiple Columns and Sorting • Groups can be based on more than one column • Example: SELECT GradeType,Student,AVG(Grade) FROM Grades GROUP BY GradeType,Student ORDER BY GradeType,Student

  12. 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 Grades GROUP BY GradeType,Student HAVING AVG(Grade) >= 70 ORDER BY GradeType,Student

  13. Selection Criteria on Aggregates SELECT colList1 FROM tableList WHERE conditionForIncludionInTable GROUP BY colList2 HAVING conditionForInclusionInGroup ORDER BY colList3

  14. The Full Select Command SELECT exp_list1 FROM table_list WHERE condition_exp GROUP BY exp_list2 HAVING condition_exp ORDER BY exp_list3

  15. Combining Tables • The previous slides have used the term tableListin 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

  16. Combining Tables with INNER JOIN • Example SELECT colList FROM table1 INNER JOIN table2 ON joinCondition [INNER JOIN table2 ON joinCondition]. . . WHERE colListSelectionCondition

  17. Alternate Notation SELECT colList FROM table1[, table2 ] . . . WHERE joinAndColListSelectionCondition

  18. 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

  19. LEFT OUTER JOIN SELECT colList FROM table1 LEFT [OUTER] JOIN table2 ON joinCondition [LEFT [OUTER] JOIN table3 ON joinCondition] . . .

  20. RIGHT JOIN SELECT colList FROM table1 RIGHT [OUTER] JOIN table2 ON joinCondition [RIGHT [OUTER] JOIN table3 ON joinCondition]. . .

  21. 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

  22. Full Joins • Not allowed in MySQL

More Related