Ravindra BagaleCourses & study guides Track your progress

6. SQL Data Types Analysts Actually Meet

6.5 Practice: Spot Wrong Types in Messy Bank Data (Read-Only)

Why. Before a manager sees a total, you should find the rows that would make it wrong.

What. This practice uses only SELECT, so nothing in the database changes. You compare the messy raw_amount text with the clean amount column and list every row that does not match.

How. Flag a row when raw_amount is NULL, or when its CAST value is different from amount.

Steps

  1. Run the first example to list every problem row with a reason.
  2. Count the problem rows with the second example.
  3. Write the row ids in your notes and decide how each one should be fixed by the data owner.
  4. Do not run UPDATE or DELETE in this practice.

Examples

Example 1 · dataset: northstar_bank

SELECT txn_id, raw_amount, amount,
       CASE
         WHEN raw_amount IS NULL THEN 'missing text'
         ELSE 'text does not match amount'
       END AS problem
FROM transactions
WHERE raw_amount IS NULL
   OR CAST(raw_amount AS REAL) <> amount
ORDER BY txn_id;

Output

txn_idraw_amountamountproblem
21,200.501200.5text does not match amount
1356,00056000.0text does not match amount
15N/A91000.0text does not match amount
2047000.0missing text

Meaning. Four rows need attention. Two have commas in the text, one has the text N/A, and one has no text at all. None of them should be trusted for a total.

Example 2 · dataset: northstar_bank

SELECT COUNT(*) AS problem_rows
FROM transactions
WHERE raw_amount IS NULL
   OR CAST(raw_amount AS REAL) <> amount;

Output

problem_rows
4

Meaning. The count gives one number you can report, for example: 4 of 25 imported rows need cleaning.

Ravindra Bagale's Tip

Keep the raw imported column and add a clean column next to it. Then you can always explain where a number came from.