0% found this document useful (0 votes)
14 views6 pages

1741273670348

The document explains SQL functions using a retail store database with two tables: Sales and Customers. It details aggregate functions like COUNT, SUM, AVG, MIN, and MAX for summarizing data, as well as scalar functions like UPPER, LOWER, LENGTH, ROUND, and DATE for data formatting. The summary emphasizes that aggregate functions summarize sets of values while scalar functions operate on individual values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views6 pages

1741273670348

The document explains SQL functions using a retail store database with two tables: Sales and Customers. It details aggregate functions like COUNT, SUM, AVG, MIN, and MAX for summarizing data, as well as scalar functions like UPPER, LOWER, LENGTH, ROUND, and DATE for data formatting. The summary emphasizes that aggregate functions summarize sets of values while scalar functions operate on individual values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

SQL With Real

World Examples
SQL Functions Explained with a Retail Store Example

Imagine you’re managing a retail store database with two tables:


1.Sales: Contains sales details like SaleID, Product, Quantity, Price, and SaleDate.
2.Customers: Contains customer details like CustomerID, Name, Email, and City.
Here’s how SQL functions work in this context:
1. Aggregate Functions
These functions perform calculations on a set of values and return a single value.
COUNT()
•What it does: Counts the number of rows.
•Real-World Example: You want to know how many sales were made in the last month. lets take (sept)
•SQL: SELECT COUNT(*) FROM Sales WHERE SaleDate >= '2023-09-01';
•This counts all rows in the Sales table where the SaleDate is in September 2023.

SUM()
•What it does: Calculates the sum of a column.
•Real-World Example: You want to calculate the total revenue from all sales.
•SQL: SELECT SUM(Quantity * Price) AS TotalRevenue FROM Sales;
•This calculates the total revenue by multiplying Quantity and Price for each sale and summing them up.
@sandeepsajjanapu
AVG()
•What it does: Calculates the average of a column.
•Real-World Example: You want to find the average sale amount per transaction.
•SQL: SELECT AVG(Quantity * Price) AS AverageSale FROM Sales;
•This calculates the average revenue per sale.
MIN()
•What it does: Finds the minimum value in a column.
•Real-World Example: You want to find the cheapest product sold.
•SQL: SELECT MIN(Price) AS CheapestProduct FROM Sales;
•This retrieves the minimum value in the Price column.
MAX()
•What it does: Finds the maximum value in a column.
•Real-World Example: You want to find the most expensive product sold.
•SQL: SELECT MAX(Price) AS MostExpensiveProduct FROM Sales;
•This retrieves the maximum value in the Price column.

@sandeepsajjanapu
2. Scalar Functions
These functions operate on a single value and return a single value.

UPPER()
•What it does: Converts text to uppercase.
•Real-World Example: You want to display customer names in uppercase for a report.
•SQL: SELECT UPPER(Name) AS UppercaseName FROM Customers;
•This converts the Name column to uppercase.
LOWER()
•What it does: Converts text to lowercase.
•Real-World Example: You want to standardize email addresses to lowercase.
•SQL: SELECT LOWER(Email) AS LowercaseEmail FROM Customers;
•This converts the Email column to lowercase.

@sandeepsajjanapu
LENGTH()
•What it does: Returns the length of a string.
•Real-World Example: You want to find customers with short names (e.g., less than 5 characters).
•SQL: SELECT Name FROM Customers WHERE LENGTH(Name) < 5;
•This retrieves names with fewer than 5 characters.
ROUND()
•What it does: Rounds a numeric value.
•Real-World Example: You want to round the total revenue to 2 decimal places.
•SQL: SELECT ROUND(SUM(Quantity * Price), 2) AS RoundedRevenue FROM Sales;
•This rounds the total revenue to 2 decimal places.
DATE()
•What it does: Extracts the date part of a datetime value.
•Real-World Example: You want to extract the date from the SaleDate column.
•SQL: SELECT DATE(SaleDate) AS SaleDateOnly FROM Sales;
•This extracts only the date part from the SaleDate column.

@sandeepsajjanapu
Analogy
Think of the retail store database as a sales and customer management system:
•Aggregate Functions are like calculators that help you summarize data
(e.g., total revenue, average sale).
•Scalar Functions are like tools that help you clean and format data
(e.g., converting text to uppercase, rounding numbers).

Summary in Simple Terms


•Aggregate Functions: Perform calculations on a set of values
(e.g., count sales, calculate total revenue, find average sale amount).
•Scalar Functions: Operate on a single value
(e.g., convert text to uppercase, find the length of a string, round numbers).

@sandeepsajjanapu

You might also like