In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives.

Slides:



Advertisements
Similar presentations
Structured Query Language (SQL)
Advertisements

Database Queries and Structured Query Language (SQL) J.G. Zheng May 16 th 2008.
Database Systems: Design, Implementation, and Management Tenth Edition
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Introduction to Structured Query Language (SQL)
Chapter 8 Special-Purpose Languages. SQL SQL stands for "Structured Query Language". Allows the user to pose complex questions of a database. It also.
Introduction to Structured Query Language (SQL)
1 Creating a Non-Conditional List A- What are you going to do? You will “list” “all of the records” in a database. (it means you will not use any condition!)
Structured Query Language Part I Chapter Three CIS 218.
Introduction to Structured Query Language (SQL)
Databases Tutorial 2 Further Select Statements. Objectives for Week Data types Sort retrieved data Formatting output.
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
SQL Intermediate Workshop Authored by Jay Mussan-Levy.
SQL for Data Retrieval. Running Example IST2102 Data Preparation Login to SQL server using your account Download three SQL script files from wiki page.
Enhancements to the GROUP BY Clause Fresher Learning Program January, 2012.
Database Programming Sections 5– GROUP BY, HAVING clauses, Rollup & Cube Operations, Grouping Set, Set Operations 11/2/10.
Introduction to SQL J.-S. Chou Assistant Professor.
Rationale Aspiring Database Developers should be able to efficiently query and maintain databases. This module will help students learn the Structured.
Xin  Syntax ◦ SELECT field1 AS title1, field2 AS title2,... ◦ FROM table1, table2 ◦ WHERE conditions  Make a query that returns all records.
Using SQL Queries to Insert, Update, Delete, and View Data Date Retrieval from a single table & Calculations © Abdou Illia MIS Spring 2015.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
Chapter 3 Single-Table Queries
Introduction to Databases Chapter 7: Data Access and Manipulation.
ADVANCE T-SQL: WINDOW FUNCTIONS Rahman Wehelie 7/16/2013 ITC 226.
SQL/lesson 2/Slide 1 of 45 Retrieving Result Sets Objectives In this lesson, you will learn to: * Use wildcards * Use the IS NULL and IS NOT NULL keywords.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
SQL for Data Retrieval. Running Example IST2102 Data Preparation Login to SQL server using your account Select your database – Your database name is.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
SQL Basic. What is SQL? SQL (pronounced "ess-que-el") stands for Structured Query Language. SQL is used to communicate with a database.
Advanced SELECT Queries CS 146. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data Note the default formats… SELECT.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Slide 1 of 19Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 In this session, you will learn to: Implement stored procedures Implement.
SQL Aggregation Oracle and ANSI Standard SQL Lecture 9.
Introduction to Functions – Single Row Functions.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
05 | SET Operators, Windows Functions, and Grouping Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program.
Single-Table Queries 2: Advanced Topics CS 320. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data SELECT field1, field2,
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
IS6146 Databases for Management Information Systems Lecture 4: SQL IV – SQL Functions and Procedures Rob Gleasure robgleasure.com.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
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.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
Slide 1 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla In this session, you will learn to: Query data by using joins Query.
In this session, you will learn to: Create and manage views Implement a full-text search Implement batches Objectives.
Database Design lecture 3_2 Slide 1 Database Design Lecture 3_2 Data Manipulation in SQL Simple SQL queries References: Text Chapter 8 Oracle SQL Manual.
Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
In this session, you will learn to: Manage databases Manage tables Objectives.
1 ORACLE I 3 – SQL 1 Salim Phone: YM: talim_bansal.
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
SQL Query Getting to the data ……..
Structured Query Language
In this session, you will learn to:
Relational Database Design
Writing Basic SQL SELECT Statements
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
SQL – Entire Select.
Chapter 4 Summary Query.
CS122 Using Relational Databases and SQL
Query Functions.
Section 4 - Sorting/Functions
Objectives In this lesson, you will learn to:
分组函数 Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice
Introduction to SQL Server and the Structure Query Language
Group Operations Part IV.
Presentation transcript:

In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives

String functions: Can be used to manipulate string values in the result set Can only be used with char and varchar data types Syntax: SELECT function_name (parameters) Let’s see how… Using String Functions

Date functions: Can be used to manipulate datetime values, perform arithmetic operations, and perform date parsing Let’s see how… Using Date Functions

Mathematical functions: Can be used to manipulate numeric values in a result set Let’s see how… Using Mathematical Functions

Just a minute Identify the utility of the datepart function. Answer: The datepart function is used to extract different parts of a date value.

Just a minute The management of AdventureWorks wants to increase the shift time from 8 hours to 10 hours. Calculate the end time of the shifts based on their start time. Answer: SELECT ShiftID, StartTime, 'EndTime' = dateadd(hh, 10, StartTime) FROM HumanResources.Shift

Ranking functions: Can be used to generate sequential numbers for each row or to give a rank based on specific criteria Supported by SQL Server are: row_number rank dense_rank Let’s see how… Using Ranking Functions

System functions: Can be used to query system tables Commonly used in SQL Server are: host_id host_name suser_id Let’s see how… Using System Functions

Problem Statement: The management at AdventureWorks, Inc. wants to view a report that displays the employee ID, designation, and age of the employees who are working as a marketing manager or a marketing specialist. The data should be displayed in uppercase. The employee details are stored in the Employee table in the AdventureWorks database. How will you display the required data? Demo: Customizing the Result Set

Solution: To solve the preceding problem, you need to perform the following tasks: 1. Create a query. 2. Execute the query to display data. Demo: Customizing the Result Set (Contd.)

Aggregate functions: Can be used to summarize values of a column based on a set of rows Supported by SQL Server are: avg count min max sum Let’s see how… Summarizing Data by Using Aggregate Functions

Just a minute What would be the output of the following query? SELECT 'Maximum Rate' = max (UnitPrice) FROM Sales.SalesOrderDetail Answer: The query displays the maximum unit price from the SalesOrderDetail table.

Data is grouped to generate summarized reports. Clauses used to group data are: GROUP BY COMPUTE COMPUTE BY PIVOT Grouping Data

GROUP BY: Summarizes the result set into groups as defined in the query by using aggregate functions Uses the HAVING clause to eliminate all those groups that do not match the condition Syntax: SELECT column_list FROM table_name WHERE condition [GROUP BY [ALL] expression [, expression] [HAVING search_condition] Let’s see how… Grouping Data (Contd.)

COMPUTE: Can be used to generate summary rows by using aggregate functions COMPUTE BY: Can be used to calculate summary values of the result set on a group of data The column on which the data is to be grouped is mentioned after the BY keyword. Grouping Data (Contd.)

Syntax: SELECT column_list FROM table_name ORDER BY column_name COMPUTE aggregate_function (column_name) [, aggregate_function (column_name)...] [BY column_name [, column_name]...] Let’s see how… Grouping Data (Contd.)

PIVOT: Is used to transform a set of columns into values Syntax: SELECT * FROM table_name PIVOT (aggregation_function (value_column) FOR pivot_column IN (column_list) ) table_alias Let’s see how… Grouping Data (Contd.)

Just a minute When grouping data, which of the following clauses helps eliminate the groups that do not match the condition specified? 1. NOT IN 2. HAVING 3. WHERE 4. COMPUTE Answer: 2. HAVING

Match the column A and with column B. Column AColumn B ALLUsed by aggregate functions PIVOTReturns zero or more values INUsed for modifying comparison operator >Relational Operator Column AColumn B ALLUsed for modifying comparison operator PIVOTRelational Operator INReturns zero or more values >Used by aggregate functions Answer: Just a minute

Problem Statement: You are a database developer of AdventureWorks, Inc. The management wants to view the average quantity ordered for each product group. The data should be displayed in the descending order of ProductID. The sales details are stored in the SalesOrderHeader and SalesOrderDetails tables in the AdventureWorks database. How will you generate this report? Demo: Summarizing and Grouping Data

Solution: To solve the preceding problem, you need to perform the following tasks: 1. Create a query. 2. Execute the query to verify the result. Demo: Summarizing and Grouping Data (Contd.)

In this session, you learned that: The string functions are used to format data in the result set. The date functions are used to manipulate date values. The mathematical functions are used to perform numerical operations. The ranking functions are used to generate sequential numbers for each row or to give a rank based on specific criteria. The system functions are used to query system tables. The aggregate functions, such as avg, count, min, max, and sum are used to retrieve summarized data. The GROUP BY and PIVOT clauses are used to group the result set. The COMPUTE and COMPUTE BY clauses are used to calculate summarized values in a grouped result set. Summary