Filter Numeric Values Stored as Strings (SQL)
SQL coding challenge · Difficulty: easy · Topic: Filtering · +70 XP
Table: transactions
+----------+---------+
| Column | Type |
+----------+---------+
| tx_id | INT | | customer | VARCHAR | | amount | VARCHAR |
+----------+---------+
tx_id is the primary key. amount is stored as TEXT, not as a number. Some of
those strings are valid numbers and some are not.
Problem
-------
Return only the rows whose amount is a valid number, converted to a number.
Counts as VALID
'100' an integer
'250.50' a decimal
'-75' a negative number
' 500 ' a number with surrounding whitespace
Counts as INVALID
'abc' text
'N/A' text
'unknown' text
'' an empty string
'1,000' a thousands separator
'12abc' digits followed by text - a PREFIX of digits is not enough
'+42' a leading plus; this problem allows a minus sign only
'-' a sign with no digits
'3.' a dot with no digits after it
'.5' a dot with no digits before it
NULL missing
'007' IS valid and becomes 7.00.
Return: tx_id, customer, amount
Order: tx_id ASC
Requirements
------------
1. Column names must be exactly tx_id, customer and amount.
2. amount in the output must be the NUMBER, not the original string:
' 500 ' becomes 500.00. Cast it to DECIMAL(12,2).
Example Input
-------------
+-------+----------+-----------+
| tx_id | customer | amount |
+-------+----------+-----------+
| 1 | Alice | '100' | | 2 | Bob | 'abc' | | 3 | Carol | '250.50' | | 4 | Dan | NULL | | 5 | Eve | ' 500 ' | | 6 | Frank | 'N/A' | | 7 | Grace | '-75' | | 8 | Henry | 'unknown' |
+-------+----------+-----------+
Expected Output
---------------
+-------+----------+--------+
| tx_id | customer | amount |
+-------+----------+--------+
| 1 | Alice | 100.00 | | 3 | Carol | 250.50 | | 5 | Eve | 500.00 | | 7 | Grace | -75.00 |
+-------+----------+--------+
Explanation
-----------
Rows 1, 3, 5 and 7 hold numbers, so they survive:
'100' -> 100.00
'250.50' -> 250.50
' 500 ' -> 500.00 the surrounding spaces are ignored
'-75' -> -75.00
Rows 2, 4, 6 and 8 hold 'abc', NULL, 'N/A' and 'unknown'. None is a number, so
they are dropped.
Why not just CAST everything?
-----------------------------
MySQL does not raise on a bad cast - CAST('abc' AS DECIMAL) quietly returns 0.
So casting first and filtering afterwards keeps every invalid row and turns it
into a 0. You have to decide whether the STRING looks numeric before you cast.
What this SQL challenge teaches you
“Filter Numeric Values Stored as Strings (SQL)” is a easy-level SQL challenge focused on Filtering. Working through it gives you hands-on practice with REGEXP, CAST, filtering, data cleaning, validation — the kind of transformation you are asked to write in real data engineering work and in technical interviews. You can solve it directly in the browser: the dataset is pre-loaded, so you write the query or DataFrame code, run it, and compare your output against the expected result immediately.
Concepts covered
- REGEXP
- CAST
- filtering
- data cleaning
- validation
How to approach it
If you get stuck, work through these steps in order before looking at a full solution — each one narrows the problem down:
- CAST('abc' AS DECIMAL) does not fail in MySQL - it returns 0. Decide whether the string looks numeric BEFORE casting.
- Use REGEXP anchored with ^ and $. Without the anchors, '12abc' matches on its leading digits and slips through.
How to practise it on PySpark.in
Open the challenge, write your SQL query in the editor and press Run to execute it against the sample dataset. Submitting checks your output against every test case, including hidden ones, so you find out straight away whether your logic holds up. You can retry as often as you like, and each solved challenge adds to your XP.
Related SQL challenges
- Find All Products That Are Out of Stock
- Filter Numeric Values Stored as Strings
- Top 3 Products per Category
- Running Total Revenue
- Find Duplicate Emails
- Median Salary per Department
Frequently asked questions
Do I need to install Spark or a database to solve this?
No. The SQL environment runs in your browser with the sample data already loaded, so there is nothing to install or configure.
Is this challenge free?
Yes - the problem, the sample dataset, the hints and unlimited test runs are free.
What level is it?
It is rated easy and covers Filtering.