Basic SQL Functions SQL Topic exerciseMediumVerified answerSQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

Rank Absolute Target Deviations

Calculate absolute_deviation as the absolute difference between actual_value and target_value.

  • Numeric functions
  • Sorting

Exercise brief

Understand the request

Data observability analyst A pipeline health report ranks records by distance from their target regardless of direction.

A pipeline health report ranks records by distance from their target regardless of direction. Calculate absolute_deviation as the absolute difference between actual_value and target_value.

Return

  • Return record_id, actual_value, target_value, and absolute_deviation.
  • Order largest deviation first, then record_id ascending.

Constraints

  • Use ABS rather than separate above-target and below-target branches.
  • Use record_id as the deterministic tie-breaker.

Data you will use

Review the relevant tables before deciding how to join, filter, or aggregate them.

function_cases

  • record_idINTEGER
  • first_nameTEXT
  • last_nameTEXT
  • labelTEXT
  • emailTEXT
  • backup_emailTEXT
  • phoneTEXT
  • raw_unitsTEXT
  • raw_quantityTEXT
  • raw_statusTEXT
  • actual_valueREAL
  • target_valueREAL
  • completed_unitsREAL
  • elapsed_hoursREAL

Hints, when you need them

Open one clue at a time so you still do the reasoning.

Hint 1

Subtracting values preserves direction; ABS removes that sign.

Hint 2

The largest anomaly has the largest absolute distance.

Hint 3

Order ABS(actual_value - target_value) descending, then record_id.

Verified SQL answer

Attempt the problem first, then compare structure and reasoning—not just syntax.

Reveal solution and explanation
SELECT record_id, actual_value, target_value, ROUND(ABS(actual_value - target_value), 1) AS absolute_deviation FROM function_cases ORDER BY absolute_deviation DESC, record_id;

Why this works

ABS converts signed variance into a direction-free magnitude, a common data-quality and anomaly-ranking pattern.

Success check

Over-target and under-target records share one comparable, correctly ordered distance measure.

Expected result

Use this output to verify values, aliases, ordering, and row count.

record_idactual_valuetarget_valueabsolute_deviation
1048010020
10711010010
106951005
10890955
1021041004
10387.5902.5
10198.41001.6
1051001000

Learn the concepts behind this answer

Strengthen your understanding with these targeted learning topics:

Continue practicing

SQL Practice Online

Open the interactive workspace and practice across SQL topics.