TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: MySQL ABS Function with Simple Examples
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
MySQL Tutorial

MySQL ABS Function with Simple Examples

Last updated: Sep 28, 2023 8:41 pm
By Meenakshi Agarwal
Share
5 Min Read
MySQL ABS Function with Simple Examples
SHARE

This tutorial explains MySQL ABS() which is a Math/Trig function. It takes a number as input and determines its absolute (positive) value. We’ll describe the usage of this method with the help of simple examples.

Contents
SyntaxMySQL ABS() Simple ExamplesABS() for ExpressionUsing ABS() with Tables

1. ABS() Syntax
2. ABS() Simple Examples
3. ABS() for Expression
4. ABS() with Tables

Let’s now go through each of the sections one by one.

MySQL ABS() Function

As stated initially, ABS() is a built-in MySQL function that does some math calculations to derive the absolute value of a number.

In Maths terms, an absolute value represents the length of a number on the number line from zero irrespective of the direction from where the 0th number rests. The ABS value can never be negative. For example, the ABS value of (2 + (-7)) is five.

So, let’s now see the details and check out how can we use it.

Syntax

Below is the signature of this method:

ABS(number);

Below are the descriptions of the parameters in the above function.

+----------+-------------------------------------+
| # Param | # Description                        |
+----------+-------------------------------------+
| number   |  A literal number or an expression. |
+----------+-------------------------------------+

If the number is a negative value, then the ABS() method converts it to a positive one. When it is either zero or positive, then ABS() makes no difference.

The return type of the value is similar to the input argument. The below diagram shows the graphical representation of MySQL ABS() function:

MySQL ABS function diagram

Let’s now consider some examples of ABS().

MySQL ABS() Simple Examples

The below example displays the output of calling the ABS() function on three different values:

SELECT 
    ABS(-4), 
    ABS(2), 
    ABS(4);

After running this statement, the output comes as:

+---------+--------+--------+
| ABS(-4) | ABS(2) | ABS(4) |
+---------+--------+--------+
| 4	  | 2	   | 4      |
+---------+--------+--------+

We’ve not applied ABS() on decimal numbers, let’s check that out:

SELECT
    ABS( -13      ),
    ABS( -13.7    ),
    ABS( -13.79   ),
    ABS(  13.79   ),
    ABS(  20 * -1 );

After we run the given statement, it gets us the following output:

13	13.7	13.79	13.79	20

ABS() for Expression

We can determine the absolute value of expr statements, such as the following:

SELECT ABS(8+3);

After running this statement, the output comes as:

+----------+
| ABS(8+3) |
+----------+
|       11 |
+----------+

One more example is:

SELECT ABS(-8+2);

After running this statement, the result comes as:

+-----------+
| ABS(-8+4) |
+-----------+
|         4 |
+-----------+

As you can see, we get a different result than we’d see if we print the expression itself (without calling the ABS()). Let’s check what that would give us:

SELECT -8+4;

The output goes like this:

+------+
| -8+4 |
+------+
|   -4 |
+------+

Also Read: MySQL FIND_IN_SET Function with Simple Examples

Using ABS() with Tables

This function gives you a positive magnitude number whether you pass a positive or negative value. So far, we’ve seen it in action in conjunction with a bare SELECT clause. Now, we’ll see how we can operate it on tables.

CREATE TABLE EMPL
    (
        empl_id int NOT NULL,
        empl_name VARCHAR(40),
        empl_title VARCHAR(20),
        empl_rank float
    );

INSERT INTO EMPL
    (
        empl_id, empl_name, empl_title, empl_rank
    )
VALUES
    (1, 'OLIVIA', 'Designer', -15.98),
    (2, 'AMELIA', 'Programmer', 0.378),
    (3, 'EMILY', 'S/W Tester', 89.764),
    (4, 'ISABELLA', 'N/W Engineer', 51.99);

SELECT * FROM EMPL;

After creating the EMPL table as above, we get the following output of the last SELECT statement.

1 OLIVIA    Designer      -16
2 AMELIA    Programmer    0
3 EMILY	    S/W Tester    90
4 ISABELLA  N/W Engineer  52

Now, let’s apply the ABS function on the emp_rank column and see how it changes the values.

SELECT empl_rank, abs(empl_rank) FROM EMPL;

After running the query with ABS, the output changes as follows:

empl_rank ABS(empl_rank)
-15.98	  15.979999542236328
0.378	  0.3779999911785126
89.764	  89.76399993896484
51.99	  51.9900016784668

We hope that after wrapping up this tutorial, you will feel comfortable using the MySQL ABS() method. However, you may practice more with examples to gain confidence.

Also, to learn SQL from scratch to depth, read our step-by-step MySQL tutorial.

You Might Also Like

MySQL vs MongoDB Comparison

Concatenate Strings in an SQL Query With Examples

The Difference between UPSERT & Insert

SQL Programming Test in 2024

SQL Table Creation: The Missing Manual

Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article MySQL DATE_ADD Function with Examples MySQL DATE_ADD Function with Simple Examples
Next Article MySQL DROP TABLE with Simple Examples MySQL DROP TABLE with Simple Examples

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x