SQL. SQL Functions. SQL Aggregate Functions. SQL Scalar functions. SQL Aggregate Functions. The AVG() Function

Similar documents
SQL Aggregate Functions

CSC Web Programming. Introduction to SQL

Chapter # 7 Introduction to Structured Query Language (SQL) Part II

Jarek Szlichta

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

Sql Server Syllabus. Overview

Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

ASSIGNMENT NO Computer System with Open Source Operating System. 2. Mysql

UNIT-3. The entity-relationship model (or ER model) is a way of graphically representing the

RDBMS-Day3. SQL Basic DDL statements DML statements Aggregate functions

Oracle Database 11g: SQL and PL/SQL Fundamentals

SQL functions fit into two broad categories: Data definition language Data manipulation language

Data Manipulation Language (DML)

Chapter-14 SQL COMMANDS

Oracle Database: SQL and PL/SQL Fundamentals NEW

Oracle Database: SQL and PL/SQL Fundamentals Ed 2

Advanced SQL Tribal Data Workshop Joe Nowinski

Unit 1 - Chapter 4,5

SQL stands for Structured Query Language. SQL lets you access and manipulate databases

Database Foundations. 6-4 Data Manipulation Language (DML) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

DATABASE MANAGEMENT SYSTEMS PREPARED BY: ENGR. MOBEEN NAZAR

Lab # 4 Hands-On. DDL and DML Advance SQL Statements Institute of Computer Science, University of Tartu, Estonia

Introduction to SQL. IT 5101 Introduction to Database Systems. J.G. Zheng Fall 2011

Some Basic Aggregate Functions FUNCTION OUTPUT The number of rows containing non-null values The maximum attribute value encountered in a given column

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

T-SQL Training: T-SQL for SQL Server for Developers

Full file at

UNIT-IV (Relational Database Language, PL/SQL)

Database Management Systems,

The Structured Query Language Get Started

Relational Database Language


SQL Server Administration Class 4 of 4. Activant Prophet 21. Basic Data Manipulation

DATABASE MANAGEMENT SYSTEMS

2) SQL includes a data definition language, a data manipulation language, and SQL/Persistent stored modules. Answer: TRUE Diff: 2 Page Ref: 36

Activant Solutions Inc. SQL 2005: Basic Data Manipulation

Database Management Systems by Hanh Pham GOALS

HKTA TANG HIN MEMORIAL SECONDARY SCHOOL SECONDARY 3 COMPUTER LITERACY. Name: ( ) Class: Date: Databases and Microsoft Access

Test Bank for Database Processing Fundamentals Design and Implementation 13th Edition by Kroenke

Oracle Syllabus Course code-r10605 SQL

Structured Query Language

1) Introduction to SQL

Structure Query Language (SQL)

Appendix A. Using DML to Modify Data. Contents: Lesson 1: Adding Data to Tables A-3. Lesson 2: Modifying and Removing Data A-8

Oracle Database 10g: Introduction to SQL

Structured Query Language (SQL) Part A. KSE 521 Topic 10 Mun Yi

SQL Interview Questions

1 Writing Basic SQL SELECT Statements 2 Restricting and Sorting Data

ITCertMaster. Safe, simple and fast. 100% Pass guarantee! IT Certification Guaranteed, The Easy Way!

SQL. Structured Query Language

What Are Group Functions? Reporting Aggregated Data Using the Group Functions. Objectives. Types of Group Functions

Querying Data with Transact-SQL (761)

Introduction to SQL. From Rob and Coronel (2004), Database Systems: Design, Implementation, and Management

Databases - 4. Other relational operations and DDL. How to write RA expressions for dummies

Querying Data with Transact SQL

DS Introduction to SQL Part 1 Single-Table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

SIT772 Database and Information Retrieval WEEK 6. RELATIONAL ALGEBRAS. The foundation of good database design

Introduction to SQL Server 2005/2008 and Transact SQL

SQL. Rodrigo García Carmona Universidad San Pablo-CEU Escuela Politécnica Superior

5. Single-row function

Oracle Database: Introduction to SQL Ed 2

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

Provider: MySQLAB Web page:

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

SQL. Char (30) can store ram, ramji007 or 80- b

Downloaded from

CHAPTER 16. More on SQL- Grouping Records and Table Joins

Index. Bitmap Heap Scan, 156 Bitmap Index Scan, 156. Rahul Batra 2018 R. Batra, SQL Primer,

Unit Assessment Guide

CGS 3066: Spring 2017 SQL Reference

Structure Query Language (SQL)

Data about data is database Select correct option: True False Partially True None of the Above

Structured Query Language: Introduction

Lecture 06. Fall 2018 Borough of Manhattan Community College

MIS2502: Data Analytics SQL Getting Information Out of a Database Part 1: Basic Queries

DEVELOPING DATABASE APPLICATIONS (INTERMEDIATE MICROSOFT ACCESS, X405.5)

Database Processing: Fundamentals, Design, and Implementation

QUETZALANDIA.COM. 5. Data Manipulation Language

tablename ORDER BY column ASC tablename ORDER BY column DESC sortingorder, } The WHERE and ORDER BY clauses can be combined in one

UNIT III INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL)

Lecture 2: Chapter Objectives. Relational Data Structure. Relational Data Model. Introduction to Relational Model & Structured Query Language (SQL)

HPCC JDBC Driver. Boca Raton Documentation Team

Lecture 2: Chapter Objectives

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

Data Infrastructure IRAP Training 6/27/2016

SQL BASICS WITH THE SMALLBANKDB STEFANO GRAZIOLI & MIKE MORRIS

D B M G. SQL language: basics. Managing tables. Creating a table Modifying table structure Deleting a table The data dictionary Data integrity

Structured Query Language (SQL) lab syllabus 4 th science. SQL is used to communicate with a database. it is the standard language for relational

Exact Numeric Data Types

Introduction. Sample Database SQL-92. Sample Data. Sample Data. Chapter 6 Introduction to Structured Query Language (SQL)

Database design process

Lab # 4. Data Definition Language (DDL)

STRUCTURED QUERY LANGUAGE (SQL)

Definition of terms Objectives Interpret history and role of SQL Define a database using SQL data definition iti language Write single table queries u

MySQL. Prof.Sushila Aghav

1. Data Definition Language.

SQL. SQL Summary. Application Scenarios. Reserved Word. Type Conversion. MaxCompute

Learn Well Technocraft

Table of Contents. PDF created with FinePrint pdffactory Pro trial version

SQL QUERIES. CS121: Relational Databases Fall 2017 Lecture 5

Transcription:

SQL Functions SQL has many built-in functions for performing calculations on data. SQL Aggregate Functions SQL aggregate functions return a single value, calculated from values in a column. Useful aggregate functions: AVG() - Returns the average value COUNT() - Returns the number of rows FIRST() - Returns the first value LAST() - Returns the last value MAX() - Returns the largest value MIN() - Returns the smallest value SUM() - Returns the sum SQL Scalar functions SQL scalar functions return a single value, based on the input value. Useful scalar functions: UCASE() - Converts a field to upper case LCASE() - Converts a field to lower case MID() - Extract characters from a text field LEN() - Returns the length of a text field ROUND() - Rounds a numeric field to the number of decimals specified NOW() - Returns the current system date and time FORMAT() - Formats how a field is to be displayed SQL Aggregate Functions The AVG() Function The AVG() function returns the average value of a numeric column. SQL AVG() Syntax SELECT AVG(column_name) FROM table_name SELECT AVG(OrderPrice) AS OrderAverage FROM Orders SELECT Customer FROM Orders WHERE OrderPrice>(SELECT AVG(OrderPrice) FROM Orders)

SQL COUNT() Function The COUNT() function returns the number of rows that matches a specified criteria. SQL COUNT(column_name) Syntax The COUNT(column_name) function returns the number of values (NULL values will not be counted) of the specified column: SELECT COUNT(column_name) FROM table_name SQL COUNT(*) Syntax The COUNT(*) function returns the number of records in a table: SELECT COUNT(*) FROM table_name SQL COUNT(DISTINCT column_name) Syntax The COUNT(DISTINCT column_name) function returns the number of distinct values of the specified column: SELECT COUNT(DISTINCT column_name) FROM table_name Note: COUNT(DISTINCT) works with ORACLE and Microsoft SQL Server, but not with Microsoft Access. SELECT COUNT(Customer) AS CustomerNilsen FROM Orders WHERE Customer='Nilsen' SELECT COUNT(*) AS NumberOfOrders FROM Orders SQL FIRST() Function The FIRST() Function The FIRST() function returns the first value of the selected column. SQL FIRST() Syntax SELECT FIRST(column_name) FROM table_name SELECT FIRST(OrderPrice) AS FirstOrderPrice FROM Orders Tip: Workaround if FIRST() function is not supported: SELECT OrderPrice FROM Orders ORDER BY O_Id LIMIT 1

SQL LAST() Function The LAST() Function The LAST() function returns the last value of the selected column. SQL LAST() Syntax SELECT LAST(column_name) FROM table_name SELECT LAST(OrderPrice) AS LastOrderPrice FROM Orders Tip: Workaround if LAST() function is not supported: SELECT OrderPrice FROM Orders ORDER BY O_Id DESC LIMIT 1 SQL MAX() Function The MAX() Function The MAX() function returns the largest value of the selected column. SQL MAX() Syntax SELECT MAX(column_name) FROM table_name SELECT MAX(OrderPrice) AS LargestOrderPrice FROM Orders SQL MIN() Function The MIN() Function The MIN() function returns the smallest value of the selected column. SQL MIN() Syntax SELECT MIN(column_name) FROM table_name SELECT MIN(OrderPrice) AS SmallestOrderPrice FROM Orders

SQL SUM() Function The SUM() Function The SUM() function returns the total sum of a numeric column. SQL SUM() Syntax SELECT SUM(column_name) FROM table_name SELECT SUM(OrderPrice) AS OrderTotal FROM Orders SQL GROUP BY Statement The GROUP BY Statement The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns. SQL GROUP BY Syntax SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name SELECT Customer,SUM(OrderPrice) FROM Orders GROUP BY Customer SELECT Customer,OrderDate,SUM(OrderPrice) FROM Orders GROUP BY Customer,OrderDate

SQL HAVING Clause The HAVING Clause The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions. SQL HAVING Syntax SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name HAVING aggregate_function(column_name) operator value SELECT Customer,SUM(OrderPrice) FROM Orders GROUP BY Customer HAVING SUM(OrderPrice)<2000 SELECT Customer,SUM(OrderPrice) FROM Orders WHERE Customer='Hansen' OR Customer='Jensen' GROUP BY Customer HAVING SUM(OrderPrice)>1500 SQL Scalar functions SQL FORMAT() Function The FORMAT() function is used to format how a field is to be displayed. SQL FORMAT() Syntax SELECT FORMAT(column_name,format) FROM table_name Parameter column_name format Description Required. The field to be formatted. Required. Specifies the format. We have the following "Products" table: Example Prod_Id ProductName Unit UnitPrice 1 Jarlsberg 1000 g 10.45 2 Mascarpone 1000 g 32.56 Now we want to display the products and prices per today's date (with today's date displayed in the following format "YYYY-MM-DD"). We use the following SELECT statement: SELECT ProductName, UnitPrice, FORMAT(Now(),'YYYY-MM-DD') as PerDate FROM Products The result-set will look like this: ProductName UnitPrice PerDate Jarlsberg 10.45 2008-10-07 Mascarpone 32.56 2008-10-07

SQL NOW() Function The NOW() Function The NOW() function returns the current system date and time. SQL NOW() Syntax SELECT NOW() FROM table_name SELECT ProductName, UnitPrice, Now() as PerDate FROM Products ProductName UnitPrice PerDate Jarlsberg 10.45 10/7/2008 11:25:02 AM Mascarpone 32.56 10/7/2008 11:25:02 AM SQL DML and DDL SQL can be divided into two parts: The Data Manipulation Language (DML) and the Data Definition Language (DDL). The query and update commands form the DML part of SQL: SELECT - extracts data from a database UPDATE - updates data in a database DELETE - deletes data from a database INSERT INTO - inserts new data into a database The DDL part of SQL permits database tables to be created or deleted. It also defines indexes (keys), specifies links between tables, and imposes constraints between tables. The most important DDL statements in SQL are: CREATE DATABASE - creates a new database ALTER DATABASE - modifies a database CREATE TABLE - creates a new table ALTER TABLE - modifies a table DROP TABLE - deletes a table CREATE INDEX - creates an index (search key) DROP INDEX - deletes an index