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

Use SUBSTRING for a Fixed-Width Batch Prefix

Return record_id, raw_units, and its first two characters as batch_prefix.

  • String functions
  • Sorting

Exercise brief

Understand the request

Ingestion operations analyst A legacy feed embeds a two-character batch prefix in a zero-padded text field.

A legacy feed embeds a two-character batch prefix in a zero-padded text field. Return record_id, raw_units, and its first two characters as batch_prefix.

Return

  • Preserve leading zeroes in batch_prefix.
  • Order by record_id ascending.

Constraints

  • Use a substring function with a one-based start position.
  • Do not cast raw_units before extracting the prefix.

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

SQL substring positions begin at 1 in the supported variants.

Hint 2

The source is text, so leading zeroes remain available.

Hint 3

Extract two characters starting at position 1.

Verified SQL answer

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

Reveal solution and explanation
SELECT record_id, raw_units, SUBSTR(raw_units, 1, 2) AS batch_prefix FROM function_cases ORDER BY record_id;

Why this works

Substring extraction is safer before numeric conversion when fixed-width text carries meaningful leading zeroes.

Success check

Every prefix is exactly the first two source characters, including leading zeroes.

Expected result

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

record_idraw_unitsbatch_prefix
10100700
10201201
10300300
10404204
10500000
10600900
10701501
10810010

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.