Learn with Abdou

INTERACTIVE LECTURES

Build and change a database

Guided class activity · apply the ideas from the full SQL lessons.

Preparing for Lab 10 · After practice, try the team challenge.

Opening your saved work…

Sign in to save across devices → · Download your guest work before switching accounts.

Validate the text before casting it

Step 6 of 9 · Database: Trail Gear: build rehearsal

READ & UNDERSTAND

CAST converts a value; it does not certify that the source was valid. SQLite can turn nonnumeric text into zero. A non-negative CHECK would then accept a made-up zero unless the load inspected the original text first.

This tiny practice file uses an explicit rule: daily_rate must be nonempty text containing only unsigned integer digits. That rejects negative values, decimal notation and unknown text. It is a rehearsal rule, not a universal currency parser. Use your client dictionary to define the real accepted formats and ranges.

Validate syntax and business meaning separately, and keep the source row and reason for anything excluded. Do not add OR IGNORE just to make a script finish: that can hide which constraint rejected which row. With default ABORT behavior, a constraint failure rolls back the failing INSERT statement, not earlier successful statements in the script.

Worked example
SELECT serial, daily_rate, CAST(daily_rate AS INTEGER) AS converted_rate FROM stg_items ORDER BY rowid;

The conversion preview includes the source text beside its numeric conversion. Compare unknown with an actual numeric zero.

The predicate identifies the negative and nonnumeric rate rows without discarding their original values.

Your question

List serial and daily_rate for source rows that violate the practice rate-text rule, ordered by rowid. Reject blanks and any character outside 0–9.

Explain your evidence

Which malformed value becomes zero after CAST? Why would checking only the converted value lose evidence?

Try a query to unlock the worked-answer discussion.

Loading…
Table & column names only · ↑ ↓ choose · Tab insert · Esc dismiss

CURRENT DATABASE

Quick schema · Trail Gear: build rehearsal

Press Esc or click outside to return to your query. PK = primary key · FK = declared foreign key.

This database has no tables yet. Create a table, then reopen the schema to see its columns.

Control Space also opens names. Enter adds a new line. Control or Command Enter runs your query.

Query results

Predict the result, then run your query.

Predict the result, then run your query.

Read-only activity · One statement per run · First 200 rows displayed · Checks compare the complete bounded result, not just this preview.

Explore this query freely in the playground ↗

Check the save status above. Account-saved work is available in My Progress. Explanations are for discussion, not automatically graded. No prerequisite locks.