Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
MySQL Statistical Functions
Next article icon

Statistical Functions in PL/SQL

Last Updated : 11 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

PL/SQL provides powerful statistical functions to perform various statistical calculations directly within the Oracle database. It provides a rich set of statistical functions that allow developers to perform complex calculations without the need for external tools.

These functions, such as AVG, STDDEV, VARIANCE, and CORR, can be integrated directly into SQL queries or PL/SQL programs to analyze data efficiently. In this article, we will explore key statistical functions in PL/SQL with practical examples and outputs.

Statistical Functions in PL/SQL

Statistical functions in PL/SQL allow developers to perform mathematical and statistical analysis directly in the Oracle database. These functions improve data analysis, reporting, and performance optimization. Some of the most commonly used statistical functions include AVG, STDDEV, VARIANCE, CORR, COVAR_POP, and COVAR_SAMP.

Creating a Sample Table

Let's begin by creating a sample table called SalesData, which contains sales information for different products. The SalesData table contains three columns: ProductID, SalesAmount, and SalesCount. The data represents the sales amount and count for different products.

Query:

-- Create the SalesData table
CREATE TABLE SalesData (
ProductID NUMBER PRIMARY KEY,
SalesAmount NUMBER,
SalesCount NUMBER
);

-- Insert data into the SalesData table
INSERT INTO SalesData (ProductID, SalesAmount, SalesCount)
VALUES
(1, 500, 30),
(2, 1000, 50),
(3, 750, 25),
(4, 600, 20),
(5, 850, 35);

Output:

ProductIDSalesAmountSalesCount
150030
2100050
375025
460020
585035

AVG Function

The AVG function calculates the average value of a numeric column in a table. It is commonly used to find the mean of a set of data, such as sales amounts or quantities.

Query:

-- Calculate the average sales amount
SELECT AVG(SalesAmount) AS AverageSales
FROM SalesData;

Output:

AverageSales
740

Explanation:

In this example, the AVG function returns 740, meaning the average sales amount across all products in the SalesData table is b. This is the sum of all sales amounts divided by the number of products.

STDDEV Function

The STDDEV function calculates the standard deviation of a numeric column, which measures how much the values in a dataset deviate from the average. It helps to understand the spread or variability in the data

Query:

-- Calculate the standard deviation of sales amount
SELECT STDDEV(SalesAmount) AS StdDevSales
FROM SalesData;

Output:

StdDevSales
188.107

Explanation:

In this case, the STDDEV(SalesAmount) function returns 188.107, meaning the sales amounts in the SalesData table deviate, on average, by 188.107 from the mean sales amount. A higher standard deviation indicates more variation in sales.

VARIANCE Function

The VARIANCE function calculates the statistical variance of a numeric column, which quantifies how much the values in a dataset differ from the average value. Variance is essentially the average of the squared differences from the mean. Variance is the square of the standard deviation.

Query:

-- Calculate the variance of sales amount
SELECT VARIANCE(SalesAmount) AS VarianceSales
FROM SalesData;

Output:

VarianceSales
35476.25

Explanation:

In this example, the VARIANCE(SalesAmount) function returns 35476.25, indicating that the sales amounts in the SalesData table have a significant variability. A higher variance means that the sales figures are spread out over a wider range, reflecting more inconsistency in sales performance.

CORR Function

The CORR function computes the correlation coefficient between two numeric columns, providing a measure of how closely the two variables are related. A value close to 1 indicates a strong positive correlation. which means that as one variable increases, the other also tends to increase.

Query:

-- Calculate the correlation between SalesAmount and SalesCount
SELECT CORR(SalesAmount, SalesCount) AS SalesCorrelation
FROM SalesData;

Output:

SalesCorrelation
0.959689

Explanation:

In this case, the CORR(SalesAmount, SalesCount) function returns a value of 0.959689. This high correlation coefficient suggests a strong positive relationship between the sales amount and the sales count, indicating that higher sales amounts are associated with a greater number of sales transactions.

COVAR_POP and COVAR_SAMP Functions

The COVAR_POP and COVAR_SAMP functions calculate the population covariance and sample covariance between two columns, respectively. Covariance indicates the directional relationship between two variables. Hence, these functions are essential for understanding the relationship between two sets of data.

Query:

-- Calculate the population covariance between SalesAmount and SalesCount
SELECT COVAR_POP(SalesAmount, SalesCount) AS CovarPop
FROM SalesData;

-- Calculate the sample covariance between SalesAmount and SalesCount
SELECT COVAR_SAMP(SalesAmount, SalesCount) AS CovarSamp
FROM SalesData;

Output for COVAR_POP:

CovarPop
4241.25

Output for COVAR_SAMP:

CovarSamp
5301.563

Explanation:

  • The COVAR_POP(SalesAmount, SalesCount) function returns a value of 4241.25, indicating a positive covariance, meaning that as sales amounts increase, sales counts also tend to increase within the entire population.
  • The COVAR_SAMP(SalesAmount, SalesCount) function yields 5301.563. This value is slightly higher than the population covariance because it considers one less degree of freedom, reflecting the covariance relationship based on a sample from the data.

Conclusion

Oracle PL/SQL provides a wide range of statistical functions that allow developers to perform statistical operations directly within SQL queries. These functions—such as AVG, STDDEV, VARIANCE, and CORR—are useful for data analysis and reporting without requiring external tools.

With these powerful features, we can perform more advanced statistical analysis within our database queries, enhancing the value of our data-driven applications.


Next Article
MySQL Statistical Functions

A

abhaystriver
Improve
Article Tags :
  • Databases
  • PL/SQL

Similar Reads

  • SQL - Statistical Functions
    SQL statistical functions are essential tools for extracting meaningful insights from databases. These functions, enable users to perform statistical calculations on numeric data. Whether determining averages, sums, counts, or measures of variability, these functions empower efficient data analysis
    4 min read
  • MySQL Statistical Functions
    MySQL provides a rich set of statistical functions that we can use to perform various statistical analyses directly within the database. These functions help us to derive insights and trends from large datasets and are essential for data analysis. This article will explore some of the key MySQL stat
    4 min read
  • Window Functions in PL/SQL
    In Oracle PL/SQL, analyzing and managing complex data relationships often involves performing calculations across sets of rows. This is where window functions, sometimes referred to as "Analytic functions," come into play. They enable powerful data analysis, such as sales forecasting, time-series an
    7 min read
  • PL/SQL MIN() Function
    PL/SQL means Procedural Language / relational Structured Query Language, the extended language of Oracle. It is primarily used to manage and manipulate databases. One of the most frequently utilized SQL functions is the MIN() function. This powerful aggregate function is essential for finding the sm
    6 min read
  • Math Functions in PL/SQL
    In PL/SQL, mathematical functions play a important role in performing calculations and manipulating numeric data. These functions allow us to execute a wide range of mathematical operations from basic arithmetic to complex computations within our PL/SQL code. In this article, we will learn about Mat
    4 min read
  • PL/SQL SUM() Function
    The SUM() function in PL/SQL is used to calculate the sum of the numeric column. It is an aggregate function that performs calculations on a set of values and returns a single value. In this article, we will explore the syntax, usage, and examples of the PL/SQL SUM() function to help us understand i
    5 min read
  • PL/SQL Functions
    PL/SQL functions are reusable blocks of code that can be used to perform specific tasks. They are similar to procedures but must always return a value. A function in PL/SQL contains:Function Header: The function header includes the function name and an optional parameter list. It is the first part o
    4 min read
  • PLSQL | SQRT Function
    In PL/SQL, the SQRT function is used to find the square root of a number. This function is really handy for various tasks that involve mathematical calculations, such as analyzing statistics, solving geometry problems, or handling financial data. The SQRT function is easy to use and can simplify com
    4 min read
  • Using LENGTH() Function in SQL
    Understanding the length of strings within a database is a fundamental aspect of effective data management and analysis. In the world of SQL, the LENGTH() function provides an essential tool for measuring the number of characters in a string, which can be invaluable for various tasks such as data va
    4 min read
  • Window Functions in SQL
    SQL window functions are essential for advanced data analysis and database management. They enable calculations across a specific set of rows, known as a "window," while retaining the individual rows in the dataset. Unlike traditional aggregate functions that summarize data for the entire group, win
    7 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences