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:
Mathematical functions in MySQL
Next article icon

Math Functions in PL/SQL

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

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 Math Functions in PL/SQL by performing examples from basic arithmetic to complex computations and so on.

PL/SQL Math Functions

PL/SQL provides various built-in mathematical functions that can be utilized for various purposes including data analysis, reporting and application logic. Let's learn some of the most commonly used math functions in PL/SQL.

Key Points:

  • Versatility: Math functions can handle integers, decimals, and other numeric data types.
  • Use Cases: These functions are useful in calculations involving statistics, financial operations, and scientific computations.
  • Precision: Some functions allow us to specify precision and scale, ensuring accuracy in results.

Common Math Functions

1. ABS Function

The ABS function returns the absolute value of a number.

SELECT ABS(-15) AS absolute_value FROM dual;  

Output:

ABSOLUTE_VALUE
--------------
15

Explanation: The ABS(-15) function returns 15, which is the absolute value of -15.

2. CEIL Function

The CEIL function rounds a number up to the nearest integer.

SELECT CEIL(15.2) AS ceiling_value FROM dual;

Output:

CEILING_VALUE
-------------
16

Explanation: CEIL(15.2) rounds 15.2 up to the next integer, which is 16.

3. FLOOR Function

The FLOOR function rounds a number down to the nearest integer.

SELECT FLOOR(15.8) AS floor_value FROM dual; 

Output:

FLOOR_VALUE
-----------
15

Explanation: FLOOR(15.8) rounds 15.8 down to 15.

4. ROUND Function

The ROUND function rounds a number to a specified number of decimal places.

SELECT ROUND(15.678, 2) AS rounded_value FROM dual; 

Output:

ROUNDED_VALUE
-------------
15.68

Explanation: ROUND(15.678, 2) rounds the number to two decimal places, resulting in 15.68.

5. MOD Function

The MOD function returns the remainder of a division operation.

SELECT MOD(10, 3) AS remainder FROM dual; 

Output:

REMAINDER
---------
1

Explanation: MOD(10, 3) performs the division of 10 by 3 and returns the remainder, which is 1.

6. POWER Function

The POWER function raises a number to the power of another number.

SELECT POWER(2, 3) AS power_value FROM dual; 

Output:

POWER_VALUE
-----------
8

Explanation: POWER(2, 3) raises 2 to the power of 3, resulting in 8.

7. SQRT Function

The SQRT function returns the square root of a number.

SELECT SQRT(16) AS square_root FROM dual;  

Output:

SQUARE_ROOT
-----------
4

Explanation: SQRT(16) returns the square root of 16, which is 4.

8. EXP Function

The EXP function returns e raised to the power of a given number.

SELECT EXP(1) AS exp_value FROM dual; 

Output:

EXP_VALUE
---------
2.71828183

Explanation: EXP(1) returns the value of e (approximately 2.7183) raised to the power of 1.

9. LN Function

The LN function returns the natural logarithm of a number.

SELECT LN(10) AS natural_log FROM dual; 

Output:

NATURAL_LOG
-----------
2.30258509

Explanation: LN(10) calculates the natural logarithm of 10, which is approximately 2.3026.

10. LOG Function

The LOG function returns the logarithm of a number to a specified base.

SELECT LOG(100, 10) AS log_value FROM dual; 

Output:

LOG_VALUE
---------
2

Explanation: LOG(100, 10) returns 2, because 10^2 equals 100.

Example using Math Functions in a PL/SQL Block

Let's say we have a scenario where we need to calculate the average score of students from a Scores table. We'll use various math functions to achieve this.

DECLARE
v_average_score NUMBER; -- Variable to hold the average score
v_max_score NUMBER; -- Variable to hold the maximum score
v_min_score NUMBER; -- Variable to hold the minimum score
BEGIN
-- Calculate average, max, and min scores using math functions
SELECT AVG(score), MAX(score), MIN(score)
INTO v_average_score, v_max_score, v_min_score
FROM Scores;

-- Display the results
DBMS_OUTPUT.PUT_LINE('Average Score: ' || ROUND(v_average_score, 2));
DBMS_OUTPUT.PUT_LINE('Maximum Score: ' || v_max_score);
DBMS_OUTPUT.PUT_LINE('Minimum Score: ' || v_min_score);
END;

Explanation:

  • The above block declares variables to hold average, maximum, and minimum scores.
  • It uses the AVG, MAX, and MIN aggregate functions to compute scores from the Scores table.
  • Finally, it outputs the results using DBMS_OUTPUT.

Conclusion

In this article, we have explored various math functions available in PL/SQL that allow us to perform arithmetic and statistical operations on numeric data. Understanding these functions can significantly enhance our ability to manipulate and analyze data within our Oracle databases.


Next Article
Mathematical functions in MySQL

M

mishraaabcf9
Improve
Article Tags :
  • Databases
  • PL/SQL

Similar Reads

  • PL/SQL MAX() Function
    The PL/SQL MAX() function is an essential aggregate function in Oracle databases, enabling users to efficiently determine the largest value in a dataset. Whether working with numerical data, dates, or strings, the MAX() function is flexible and widely applicable. In this article, we will provide a d
    4 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
  • 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
  • Mathematical functions in MySQL
    Mathematical functions in MySQL are essential tools for performing a variety of numerical operations directly within SQL queries. These built-in functions enable users to execute calculations, manipulate numeric data, and perform statistical analysis efficiently. In this article, We will learn about
    3 min read
  • LEAST() Function in MySQL
    The LEAST() function in MySQL is a versatile tool that returns the smallest (minimum) value from a list of expressions. It can be used with numbers, strings, or even columns of data to find the minimum value according to their data type. In this article, We will learn about LEAST() Function in MySQL
    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
  • SQL General Functions
    SQL general functions are built-in tools provided by SQL databases to perform a variety of operations on data. These functions are crucial for manipulating, analyzing, and transforming data efficiently. In this article, We will learn about the what are the SQL General Functions along with the exampl
    5 min read
  • SQL MIN() Function
    The MIN() function in SQL is a powerful tool that allows us to determine the smallest or lowest value from a specified column or expression. It is widely used in data analysis to extract minimum values for decision-making, reporting, and business insights. This function automatically excludes NULL v
    8 min read
  • PLSQL | GREATEST Function
    The GREATEST function in PL/SQL is a tool for comparing multiple values and returning the largest one from a given list of expressions. Whether we are working with numbers, strings, or dates, the function determines the greatest value based on their data type. In this article, We will learn about th
    4 min read
  • SQL | Numeric Functions
    SQL Numeric Functions are essential tools for performing mathematical and arithmetic operations on numeric data. These functions allow you to manipulate numbers, perform calculations, and aggregate data for reporting and analysis purposes. Understanding how to use SQL numeric functions is important
    4 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