ABS()

SQL · Math Functions

Theory

ABS returns the absolute value of a number, which is its distance from zero with the sign removed. ABS(-42) is 42, ABS(42) is 42 and ABS(0) is 0. It works on integers and decimals alike and returns NULL if the input is NULL.

In analytics, ABS is most useful when you care about the size of a difference but not its direction. If a sales target was ₹50,000 and actual sales were ₹47,500, then actual - target is -2,500, and ABS turns that into a 2,500 gap you can compare, sum or rank. It is also the building block of error metrics such as mean absolute error (AVG(ABS(actual - forecast))) and of tolerance checks like WHERE ABS(book_amount - bank_amount) <= 1 when reconciling two systems. ABS is spelled the same way in every major database, including SQLite.

Why we use it

Positive and negative differences cancel out when you add them, hiding real errors. ABS measures how far off something is regardless of direction, which is what most gap, error and tolerance checks need.

When to use it

Use ABS for variance size, forecast error, reconciliation tolerances and 'closest value' searches (ORDER BY ABS(x - target)). Keep the signed value when the direction matters, for example over- versus under-target.

How it works

  1. Read the input number
  2. Is the value NULL? Yes: Return NULL. No: Continue.
  3. Is the value negative? Yes: Flip the sign (-2500 becomes 2500). No: Keep it as it is.
  4. Return a non-negative number

Syntax

ABS(number)

Example

SELECT ABS(-42);
-- 42

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic