Actually Available
An easy SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.
- Domain
- SQL
- Difficulty
- easy
- Seniority
- L3
- Asked in
- Yelp
Problem
How many products are currently marked as in stock, the figure an inventory analyst checks against the warehouse's physical count?
The catalog is big. The shelf is smaller.
Tables
products
Columns: product_id INTEGER, product_name TEXT, category TEXT, price REAL, rating REAL, in_stock INTEGER
| product_id | product_name | category | price | rating | in_stock |
|---|---|---|---|---|---|
| 1001 | Basic Device 1X | Books | 17.52 | 2.7 | 1 |
| 1050 | Deluxe Bundle 2X | Clothing | 25.05 | 4.4 | 1 |
| 1099 | Pro Unit 3X | Home & Kitchen | 32.58 | 2.1 | 1 |
| 1148 | Ultra Tool 4X | Collectibles | 40.11 | 3.8 | 1 |
| 1197 | Essential Set 5X | Toys | 47.64 | 1.5 | 1 |
| 1246 | Classic System 6X | Beauty | 0 | 3.2 | 1 |
| 1295 | Smart Gadget 7X | Automotive | 62.7 | 4.9 | 0 |
| 1344 | Eco Kit 8X | Garden | 70.23 | 2.6 | 1 |
| 1393 | Turbo Pack 9X | Music | 77.76 | 4.3 | 0 |
| 1442 | Premium Widget 10X | Electronics | 85.29 | 2 | 1 |
Expected output
| in_stock_count |
|---|
| 158 |
Worked solution and explanation
What's actually being tested
This is a filtered count wearing an inventory-reconciliation costume. The whole question hinges on three words in the prompt: "marked as in stock." You are not counting the catalog; you are counting the subset where a flag is set. The trap is that in_stock is an INTEGER 0/1 flag that is never NULL, so COUNT(in_stock) counts every row exactly like COUNT(*) does. Candidates who miss this hand back the catalog size: 200 products in stock when only 158 actually are, so the reconciliation is off by every one of the 42 out-of-stock items.
Building the query
Step 1: Filter to the flagged rows
in_stock holds 0 or 1, so WHERE in_stock = 1 keeps exactly the in-stock products. Don't lean on WHERE in_stock (truthy-integer shorthand) or WHERE in_stock = TRUE: the column is an integer, and those forms make you depend on engine-specific truthiness instead of stating the value you mean. Compare against the literal 1.
Step 2: Count what survived the filter
COUNT(*) tallies the rows the WHERE clause let through. Because in_stock is never NULL here, COUNT(*) and COUNT(in_stock) agree, but COUNT(*) is the honest expression of intent: "how many rows match?" Alias it in_stock_count so the single-cell result reads as an answer, not a mystery scalar.
SELECT COUNT(*) AS in_stock_count
FROM products
WHERE in_stock = 1The WHERE does the work; the COUNT just tallies what's left.
- Catalog size (wrong)
SELECT COUNT(*) AS in_stock_count FROM productsreturns 200, every row regardless of stock. It answers a question nobody asked.- In-stock count (right)
SELECT COUNT(*) AS in_stock_count FROM products WHERE in_stock = 1returns 158, only the flagged rows. It answers the actual question.
Common follow-up questions
- Now give me in-stock AND out-of-stock counts in one result. (Moves from a `WHERE` filter to conditional aggregation: `SUM(CASE WHEN in_stock = 1 THEN 1 ELSE 0 END)` alongside the 0 case, computed in a single pass.)
- Break the in-stock count down by category. (Adds `GROUP BY category` while keeping the `in_stock = 1` filter; tests filter-then-group ordering.)
- What if `in_stock` could be NULL for products never audited? (Tests three-valued logic: `WHERE in_stock = 1` silently drops NULLs, and whether the candidate knows that is correct here or whether a different question needs `COALESCE`.)