Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017

Slides:



Advertisements
Similar presentations
Database Systems: Design, Implementation, and Management Tenth Edition
Advertisements

SQL Subqueries Objectives of the Lecture : To consider the general nature of subqueries. To consider simple versus correlated subqueries. To consider the.
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
XP Chapter 3 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Analyzing Data For Effective Decision Making.
Introduction to Structured Query Language (SQL)
Introduction to Structured Query Language (SQL)
Concepts of Database Management Sixth Edition
Microsoft Access 2010 Chapter 7 Using SQL.
Chapter 3 Single-Table Queries
Microsoft Access 2010 Chapter 7 Using SQL. Change the font or font size for SQL queries Create SQL queries Include fields in SQL queries Include simple.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
Introduction to Databases Chapter 7: Data Access and Manipulation.
Analyzing Data For Effective Decision Making Chapter 3.
CSC271 Database Systems Lecture # 12. Summary: Previous Lecture  Row selection using WHERE clause  WHERE clause and search conditions  Sorting results.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
Instructor: Craig Duckett Lecture 03: Tuesday, April 14, 2015 SQL Sorting, Aggregates and Joining Tables 1.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
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.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
1 Section 10 - Embedded SQL u Many computer languages allow you to embed SQL statements within the code (e.g. COBOL, PowerBuilder, C++, PL/SQL, etc.) u.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Instructor: Craig Duckett Lecture 09: Thursday, October 29 th, 2015 CASE, ORDER BY, GROUP BY, HAVING, Subqueries 1 BIT275: Database Design (Fall 2015)
Restricting and Sorting Data
CHAPTER 7 DATABASE ACCESS THROUGH WEB
SQL Query Getting to the data ……..
MySQL Subquery Source: Dev.MySql.com
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Relational Database Design
Writing Basic SQL SELECT Statements
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
Basic select statement
ATS Application Programming: Java Programming
Subqueries Schedule: Timing Topic 25 minutes Lecture
Using Subqueries to Solve Queries
Aggregating Data Using Group Functions
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
SQL LANGUAGE and Relational Data Model TUTORIAL
Prof: Dr. Shu-Ching Chen TA: Haiman Tian
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Using Subqueries to Solve Queries
SQL Subquery.
Reporting Aggregated Data Using the Group Functions
Contents Preface I Introduction Lesson Objectives I-2
Relational Database Design
Chapter 8 Advanced SQL.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Using Subqueries to Solve Queries
IST 318 Database Administration
Reporting Aggregated Data Using the Group Functions
Using Subqueries to Solve Queries
Reporting Aggregated Data Using the Group Functions
Subqueries Schedule: Timing Topic 25 minutes Lecture
Subqueries Schedule: Timing Topic 25 minutes Lecture
分组函数 Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice
Shelly Cashman: Microsoft Access 2016
Restricting and Sorting Data
Aggregating Data Using Group Functions
Presentation transcript:

Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017 CASE, ORDER BY, GROUP BY, HAVING, Subqueries

MID-TERM EXAM is LECTURE 10, THIS Thursday, April 27 Assignment 2 is due LECTURE 12, Tuesday, May 9th in StudentTracker by MIDNIGHT Assignment 3 is due LECTURE 20, Tuesday, June 6th Database Presentation is due LECTURE 20, Tuesday, June 6th Final Exam is LECTURE 12, Thursday, June 8th

3 x 150 Points (450 points Total) Assignment 1 GRADED! Assignment 2 (Stage 2): DUE LECTURE 12 Tuesday, May 9th Assignment 3 (Stage 3): DUE LECTURE 20 Tuesday, June 6th Database Presentation: DUE LECTURE 20 Tuesday, June 6th

Tuesday (LECTURE 8) Database Design for Mere Mortals: Chapter 6 Thursday (LECTURE 9) The Language of SQL: Chapters 7, 8

CASE ORDER BY GROUP BY HAVING Subqueries

SQL: The CASE Expression The CASE expression and its shorthand equivalents, COALESCE() and NULLIF(), let you take actions based on a condition’s truth value (true, false, or unknown). The CASE expression’ important characteristics are: If you’ve programmed before, you’ll recognize that CASE provides SQL the equivalent of the if-then-else, case, or switch statements used in procedural languages, except that CASE is an expression, not a statement. * CASE is used to evaluate several conditions and return a single value for the first true condition. CASE allows you to display an alternative value to the actual value in a column. CASE makes no changes to the underlying data. * An expression can be evaluated, and as such returns a single value. It is only one possible part of a statement. A statement is a collection of elements such as identifiers, reserved keywords, data types, functions, expressions, operators and comments, to make the smallest possible unit of code. A statement can be executed.

SQL: The CASE Expression A common use of CASE is to replace codes or abbreviations with more-readable values. EXAMPLE: If the column marital status contains the integer codes 1, 2, 3, or 4—meaning single, married, divorced, or widowed—your human readers will prefer to see explanatory text rather than cryptic codes. (Some database designers prefer to use codes, because it’s more efficient to store and manage abbreviated codes than explanatory text.) CASE has two formats: simple and searched. The simple CASE expression compares an expression to a set of simple expressions to determine the result. The searched CASE expression evaluates a set of logical (Boolean) expressions to determine the result. CASE returns an optional ELSE result as the default value if no test condition is true.

SQL: The CASE Expression To use a simple case expression: value1, value2, ..., valueN are expressions. result1, result2, ..., resultN are expressions returned when the corresponding value matches the expression comparison_value. All expressions must be of the same type or must be implicitly convertible to the same type. Each value is compared to comparison_ value in order. First, value1 is compared. If it matches comparison value, then result1 is returned; otherwise, value2 is compared to comparison_value. If value2 matches comparison_value, then result2 is returned, and so on. If no matches occur, default_ result is returned. If ELSE default_result is omitted, ELSE NULL is assumed.

SQL: The CASE Expression We get many questions asking whether it's possible to use an IF clause inside a SELECT statement. Well, not exactly. But you can use CASE to do the exact same thing. Let's look at what a simple CASE statement in a query looks like: This query will return three columns. The third column, StateName, is constructed using the CASE statement. The CASE statement is enclosed by the keywords CASE and END. The CASE statement evaluates the state field. If the field contains 'CA' then the CASE statement returns 'California'. 'KS' returns 'Kansas' and 'TN' returns 'Tennessee'. If the value of state does not match any of the WHEN expressions, the expression in the ELSE clause is returned, 'Some Other State'. The AS StateName aliases the column name to StateName.

SQL: The CASE Expression Another simple case expression example:

SQL: The CASE Expression To use a searched CASE expression: condition1, condition2, ..., conditionN are search conditions. (Search conditions have one or more logical expressions, with multiple expressions linked by AND or OR. result1, result2, ..., resultN are expressions returned when the corresponding condition evaluates to true. All expressions must be of the same type or must be implicitly convertible to the same type. Each condition is evaluated in order. First, condition1 is evaluated. If it’s true, result1 is returned; otherwise, condition2 is evaluated. If condition2 is true, result2 is returned, and so on. If no conditions are true, default_result is returned. If ELSE default_result is omitted, ELSE NULL is assumed.

SQL: The CASE Expression In the searched case expression, CASE is evaluating a series of boolean expressions. It returns the first expression that evaluates to true. This CASE statement will run through each WHEN clause until it finds a true one. If they all evaluate to false it will return the ELSE clause. In both cases if there is no ELSE clause it will return a NULL. In this second example, each boolean expression is independent of each other statement. I can actually compare different columns (e.g., state and city). Remember that it searches through the WHEN clauses from top to bottom so order them carefully.

SQL: The CASE Expression Another example of a searched case expression.

Difference between ORDER BY and GROUP BY The difference is exactly what the name implies: an ORDER BY sorts and a GROUP BY performs a grouping operation usually based on an aggregate of some sort. So what does this mean, exactly? ORDER BY is used to order the rows resulted from a SELECT statement. ORDER BY allows you to sort the result set according to different criteria, such as sort by name from a-z, then sort by price highest to lowest: ORDER BY name, price DESC GROUP BY is used to group rows in a SELECT, usually when aggregating rows (e.g. using SUM, or COUNT, or AVG for a set of rows with the same values for some fields). GROUP BY allows you to take your result set, group it into logical groups and then run aggregate queries on the groups. For example, you could select all employees, group them by their workplace location and calculate the average salary. This would give you the average salary of an employee at a given workplace location in your database.

ORDER BY

ORDER BY

GROUP BY

GROUP BY with ORDER BY

SQL: Filtering Groups with HAVING The HAVING clause sets conditions on the GROUP BY clause similar to the way that WHERE interacts with SELECT. The HAVING clause’s important characteristics are: The HAVING clause comes after the GROUP BY clause and before the ORDER BY clause. Just as WHERE limits the number of rows displayed by SELECT, HAVING limits the number of groups displayed by GROUP BY. The WHERE search condition is applied before grouping occurs, and the HAVING search condition is applied after. HAVING syntax is similar to the WHERE syntax, except that HAVING can contain aggregate functions. A HAVING clause can reference any of the items that appear in the SELECT list. The sequence in which the WHERE, GROUP BY, and HAVING clauses are applied is: The WHERE clause filters the rows that result from the operations specified in the FROM and JOIN clauses. The GROUP BY clause groups the output of the WHERE clause. The HAVING clause filters rows from the grouped result.

SQL: Filtering Groups with HAVING List the number of books of each type for each publisher, for publishers with more than one title of a type.

SQL: Filtering Groups with HAVING List the number of titles and average revenue for the types with average revenue more than $1 million.

SQL: Filtering Groups with HAVING List the number of books of each type for each publisher, for publishers with more than one title of a type.

SQL: Subqueries A Subquery or Inner query or Nested query is a query within another SQL query, and embedded within the WHERE clause. A subquery is used to return data that will be used in the main query as a condition to further restrict the data to be retrieved. Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE statements along with the operators like =, <, >, >=, <=, IN, BETWEEN etc. There are a few rules that subqueries must follow: Subqueries must be enclosed within parentheses ( ) A subquery can have only one column in the SELECT clause, unless multiple columns are in the main query for the subquery to compare its selected columns. An ORDER BY cannot be used in a subquery, although the main query can use an ORDER BY. The GROUP BY can be used to perform the same function as the ORDER BY in a subquery.

SQL: Subqueries TO BE CONTINUED… WE WILL BE DEDICATING MORE TIME TO THIS TOPIC NEXT TUESDAY (MID-TERM ON THURSDAY) Example

BIT 275 ICE 9