0% found this document useful (0 votes)
11 views

Part 3 - Expressions and Functions

This document discusses SQL expressions and functions. It provides examples of using mathematical, string, date/time, and aggregate functions in SQL queries. Specifically, it shows how to: 1) Perform calculations on columns by selecting new calculated columns like profit (ItemPrice - ItemCost); 2) Use aliases to name calculated columns; 3) Apply mathematical functions like LOG(), string functions like UCASE(), and date functions like DatePart() to columns; 4) Use aggregate functions like AVG() and COUNT() to summarize data from multiple rows.

Uploaded by

chi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Part 3 - Expressions and Functions

This document discusses SQL expressions and functions. It provides examples of using mathematical, string, date/time, and aggregate functions in SQL queries. Specifically, it shows how to: 1) Perform calculations on columns by selecting new calculated columns like profit (ItemPrice - ItemCost); 2) Use aliases to name calculated columns; 3) Apply mathematical functions like LOG(), string functions like UCASE(), and date functions like DatePart() to columns; 4) Use aggregate functions like AVG() and COUNT() to summarize data from multiple rows.

Uploaded by

chi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Database Concepts and Skills for Big

Data
Le Duy Dung (Andrew)
SQL – Part 3
SQL Expressions and Functions
Performing Mathematical Calculations
❑ Let’s consider the ITEM table
❑ We want to calculate the ItemPrice – ItemCost and show
the result in a new column called Profit
Performing Mathematical Calculations
❑ First, select the required columns:
SELECT ItemID, ItemDescription, ItemCost, ItemPrice
FROM ITEM;
Performing Mathematical Calculations
❑ Add a new calculated column (ItemPrice - ItemCost):
SELECT ItemID, ItemDescription, ItemCost, ItemPrice , ItemPrice - ItemCost
FROM ITEM;

❑ To make this more readable, we can use multiple lines for columns
SELECT ItemID,
ItemDescription,
ItemCost,
ItemPrice ,
ItemPrice - ItemCost
FROM ITEM;
Using Aliases
• The previous example returns ItemPrice - ItemCost.
• To create a name for the new column, we should use Aliases
• The AS keyword will be used to create Aliases
Performing Mathematical Calculations
❑ Define new name for the new column
SELECT ItemID,
ItemDescription,
ItemCost,
ItemPrice ,
ItemPrice – ItemCost AS Profit
FROM ITEM;
Performing Mathematical Calculations
❑ Example: Calculate the profit for items from Vendor ID = 1
SELECT ItemID,
ItemDescription,
ItemCost,
ItemPrice ,
ItemPrice – ItemCost AS Profit
FROM ITEM
WHERE VendorID=1;
Functions
❑ Functions in DBMS are used for performing operations on data.
❑ Functions are highly DBMS specific. Meaning functions used in Access
could be different from the functions in MySQL, PostgreSQL, …
❑ This means the SQL statement created in one DBMS might not work
in all DBMSs.
❑ The SQL functions can be categorized into the following groups
• Numerical functions
• String (text) function s
• Date and time function s
• System functions
Numerical Manipulation Functions
❑ There are many numerical manipulation functions available in
SQL, a few examples are:
• ABS() → Returns the absolute value of a number
• SIN() → Returns a value specifying the sine of an angle .
• COS() → Returns a value specifying the cosine of an angle
• TAN() → Returns a value specifying the tangent of an angle
• LOG() → Returns a value specifying the natural logarithm of a number
• SQR() → Returns a value specifying the square root a number
• SGN() → Returns a value (Integer) indicating the sign of a number
• ROUND → Returns a number rounded to a specified number of decimal places.
Performing Mathematical Calculations
❑ Define new name for the new column
SELECT ItemID,
ItemDescription,
ItemCost,
ItemPrice ,
Log(ItemPrice – ItemCost) AS LogProfit
FROM ITEM;
String Manipulation Functions
❑ There are many string manipulation functions available in
SQL, a few examples are:
• UC ASE() → Conver ts a string to upper case
• LC ASE() → Conver ts a string to lowercase
• LEN() → Returns the length of a string
• LTRIM() → Trims white space from left of string
• RTRIM() → Trims while space from right of string
• TRIM() → Trims white space from both sides of string
• LEFT() → Return characters from the beginning of a string
• RIGHT() → Return characters from the end of a string
• MID() → Return characters from the middle of a string
String Manipulation Functions
❑ Define new name for the new column
SELECT FirstName, UCASE(FirstName) AS UppercaseFirstName
FROM CUSTOMER;
String Manipulation Functions
❑ Example: Using LEFT, RIGHT, MID, and LEN functions
SELECT ItemDescription,
LEFT(ItemDescription, 4) AS IDL,
RIGHT(ItemDescription, 2) AS IDR,
MID(ItemDescription, 5, 2) AS IDM,
LEN(ItemDescription) AS IDLen
FROM ITEM;
❑ Arguments:
• LEFT(String, Number of characters to return)
• RIGHT(Str ing, Number of characters to return)
• MID(String, the position of the character to begin, Numb er of characters to return)
Date & Time Manipulation Functions
❑ Date and Times are stored in a special format so they can
be stored or filtered quickly
❑ Date and Time formats and functions are often not
consistent in DBMSs
❑ There are many Date and Time manipulation functions. The
most important one is DatePart function
❑ DatePart(): return an Integer containing the specified part
of a given date.
Date & Time Manipulation Functions
❑Example: Using DatePart function
SELECT SaleID,
SaleDate ,
DatePart(‘yyyy’, SaleDate) as SaleYear
FROM SALE;

SELECT SaleID,
SaleDate ,
FROM SALE
WHERE DatePart(‘yyyy’, SaleDate) = 2019;
Date & Time Manipulation Functions
❑Example: Using DatePart function
SELECT SaleID,
SaleDate ,
DatePart(‘m’, SaleDate) as SaleMonth,
DatePart(‘d’, SaleDate) as SaleDay
FROM SALE;
Aggregate Functions
❑ The aggregate functions are used to summarize data such as:
▪ Finding the highest or lowest value in a table column
▪ Calculating the summation or the average of set of rows in a table
▪ Counting the number of rows in a table based on some criteria
❑ By using the aggregate functions, we can extract some
information from dataset without retrieving all data
❑ In other words, an aggregate function calculates and returns
a single value based on a set of rows
Aggregate Functions
❑ The aggregate functions are:
• AVG(): returns a column’s average value
• COUNT(): returns the number of rows in a column
• MAX(): returns a column’s highest value
• MIN(): returns a column’s lowest value
• SUM(): returns the sum of column’s value
Aggregate Functions
❑ Example:
SELECT AVG(ItemPrice) as AvgPrice
FROM ITEM;

SELECT AVG(ItemPrice) as AvgPrice


FROM ITEM
WHERE VendorID = 1;
QnA!

You might also like