Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can I use CASE to label values in a SELECT result?

Quick answer

Put a CASE expression in your SELECT list, with a WHEN condition and THEN label for each category. SQLite checks searched CASE conditions from left to right and returns the result for the first true condition, so put narrower or more specific ranges before broader ones. Add ELSE for a fallback label. This changes the query result, not the stored stock values. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have a SQLite table with item names and stock counts. How can I show labels like “Out of stock” or “Low stock” in a query without changing the counts in the table?

Geez

Add a CASE expression to the SELECT list. For this example, assume the table is named inventory, the count column is stock_count, and the labels mean: zero is out of stock, one through five is low stock, and anything higher is available. Those cutoffs are example choices; replace them with the definitions that fit your use.

SELECT
  item_name,
  stock_count,
  CASE
    WHEN stock_count = 0 THEN 'Out of stock'
    WHEN stock_count <= 5 THEN 'Low stock'
    ELSE 'Available'
  END AS availability
FROM inventory;

The availability value is a label in the query result; it does not update the stored count. In SQLite, a CASE expression returns a value in the SELECT expression. [S1]

Follow-up question

Why does the order of those WHEN lines matter? Wouldn’t SQLite find the right label either way?

Geez

The conditions can overlap. A count of zero also satisfies stock_count <= 5, so if the low-stock condition came first, zero would get the “Low stock” label. SQLite tests searched CASE conditions from left to right and returns the THEN result for the first true condition. Put the more specific condition first, then the broader one. [S1]

For the example, that means checking for exactly zero before checking whether the count is at most five. If you want to make the low-stock rule explicit, you can describe it as counts from one through five in your chosen rule. Keep the zero check as its own condition.

Follow-up question

What would the output look like for a few sample counts? And what happens if a value doesn’t match any WHEN?

Geez

Using invented example rows and the assumed labels above, the selected columns would look like this:

item_name       stock_count  availability
Canvas bag      0            Out of stock
Desk lamp       3            Low stock
Notebook set    12           Available

The third row reaches ELSE because neither earlier condition is true. An ELSE is useful as a readable fallback; without a matching WHEN or ELSE, SQLite returns NULL for the CASE result. [S1] You can choose a fallback that suits your report, such as 'Check count', but that is a display choice—not a correction to the underlying data.

Follow-up question

How do I adapt this safely if our low-stock cutoff changes, and what should I check before using the query?

Geez

Treat the cutoffs as business rules you choose, not values supplied by CASE itself. For instance, if your agreed rule is that counts from one through three are low, change the cutoff condition to stock_count <= 3, keeping the zero condition first. Decide what label should apply to counts outside the named ranges. CASE selects a result for each row; it does not establish whether your chosen thresholds are appropriate.

Before relying on the display, check the column and table names, confirm the intended cutoff and boundary values with whoever owns the reporting rule, and check representative counts such as zero, the cutoff, and one above it. The sample assumes nonnegative counts and ordinary item rows; it does not establish how missing values or unusual data should be classified. Decide that explicitly if it matters, and add a matching condition or fallback label rather than assuming the example covers it.

A practical checklist

  • Replace inventory, item_name, and stock_count with your actual table and column names.

  • Write down what each label means and choose the cutoff values for your use.

  • Put exact or narrower conditions before broader conditions that could also match.

  • Choose an ELSE label, or deliberately account for the NULL result if no condition matches.

  • Check example boundary counts, including zero, the cutoff, and a value just above it.

A few more useful answers

Does CASE update the stock count in the table?

No. In this SELECT example, CASE supplies a value in the query result; it does not update stored data. [S1]

What happens if no WHEN condition matches and I omit ELSE?

SQLite returns NULL for that CASE result when there is no matching WHEN and no ELSE. [S1]

Can I change the label thresholds?

Yes. Replace the example conditions with your chosen rules, and keep potentially overlapping conditions in the order you want them considered.

Sources

References used for this guide.

  1. [S1] SQLite SQL Language Expressions: CASE expression
    sqlite.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work