Interview: shoes show a 0% discount, then the report crashes

Integer division truncates the rate, and a zero price then raises division by zero. NULLIF, COALESCE, COUNT skipping NULL, and IS NOT DISTINCT FROM.

More on Ticket interviews

Keep learning

Overview

Give Away NULL → 0 discount / price * 100 both integers NULLIF(price, 0)::float 1000 / 10000 is 0 in integer arithmetic, and 0 * 100 is still 0. A 50% discount truncates to 0 the same way. The giveaway row never gets that far: 0 / 0 raises division by zero. The shoes rates are right. The giveaway still divides by zero, so the whole GROUP BY fails and the report 500s. NULLIF turns a zero price into NULL. Dividing by NULL is NULL, not an error, and AVG skips those rows. COALESCE shows 0 for a product whose every rate was NULL.

Shoes show a 0% discount. Then the report crashes.

Integer division turned 10% into 0%. The float cast fixed shoes, then a zero price crashed the report. integer division division by zero COUNT and NULL Discounts show 0%, and the fixed report crashes. Cast the operand and wrap the price in NULLIF(price, 0).

Discount rate is 0% on shoes. Product report is down.

$1,000 off $10,000 shows 0%. $2,500 off $5,000 also shows 0%. Someone cast price to float. Shoes now look right. The all-products report now fails with division by zero. shown rate shoes, real rate 10% and 50% after the cast giveaway price Restated: the rate is truncated, and a zero-price row crashes the “fixed” query.

One expression, two failures

price and discount are integers. The division happens before the multiply. Give Away rows have price 0. One of them aborts the aggregate for every product. Failure 1 · integers Failure 2 · after the cast 0 / 0.0 raises an error. The whole report fails.

The type of the division, then the zero

Is the formula looking at the wrong columns? read discount_rate.sql What does PostgreSQL do with two integers? try 1000 / 10000 alone It truncates to 0. So 0 × 100 stays 0. Why did the float cast cause an error? read by_product.sql Shoes now divide correctly. But Give Away has price 0, and 0 / 0.0 errors. What should a zero price contribute? ask finance, then check NULL No rate at all. NULLIF makes it NULL, and AVG skips NULL.

Name the type, the zero, and the display value

Casts an operand of the division, where truncation happens. Turns the crash into NULL and says AVG skips it. Keeps the giveaway row on the report. Pins all three numbers in a test. NULLIF(price, 0) 0 becomes NULL no truncation skips NULL rows COALESCE(…, 0) blank → 0 Each step fixes one failure.

No AI from here

Why is 1000 / 10000 * 100 equal to 0, while 1000 / 10000.0 * 100 is 10? The operand types pick the division. Two integers truncate toward zero. So 1000 / 10000 is 0, and × 100 never sees 0.1. A decimal literal or ::float on an operand gives 0.1. Casting the result is too late: (1000 / 10000)::float is 0. truncation happens at / cast an operand a decimal literal is enough (1000 / 10000)::float * 100 1000 / 10000::float * 100 Why does 1 / NULL succeed when 1 / 0 fails? error vs NULL NULLIF(value, 0) the query keeps going ERROR. Whole statement stops. 1 / NULL NULL. Query returns its rows. The hat has one sale and COUNT(customer) is 0. Is the aggregate broken? The hat buyer is unknown, so customer is NULL. AVG, SUM, MIN and MAX also skip NULL. COUNT(*) − COUNT(customer) is the number of unknown customers. COUNT(*) vs COUNT(column) aggregates skip NULL unknown customers Use customer IS NOT DISTINCT FROM :name. The long OR form works too, but is easier to get wrong. IS NOT DISTINCT FROM OR form is the long way 'Bill' vs 'Bill' 'Bill' vs NULL NULL vs NULL WHERE drops anything that is not true. COALESCE(AVG(...), 0) shows 0 for Give Away. What did finance actually agree to? A display choice: no rate exists, so print 0 instead of blank. It is not an average of zeros. AVG never saw those rows. If finance wants “unknown”, drop COALESCE and show NULL. Say it aloud: a displayed 0 and a computed 0 differ. display choice vs arithmetic AVG never saw those rows

Finance wants a number in every cell

What do you ship in the report? NULLIF, then COALESCE the average to 0 Shoes show 10 and 50. Give Away shows 0, and nothing can divide by zero. Correct shoes Report stays up Misleading zero WHERE price > 0, and drop Give Away No error, shoes right. But the product vanishes, and finance files a new ticket. Cast the whole expression after the division Integers still divide first, so shoes stay 0. A zero price still errors. Store price as numeric and stop Worth doing, but a stored 0 still divides by zero. Not enough for tonight.

Have one ready

Tell me about a report that was wrong without throwing an error. Shown number vs expected number. Which operator truncated, or which NULL vanished. The cast, NULLIF, or comparison you changed. Test rows: a zero, a NULL, a fraction.

Did you show these?

1000 / 10000 is 0 before the multiply Cast an operand, not the result NULLIF turns a zero price into NULL COUNT(*) vs COUNT(column) IS NOT DISTINCT FROM for NULL parameters Kept the giveaway row

More courses

  • Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
  • Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
  • React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
  • System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
  • NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
  • Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
  • Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
  • AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
  • Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
  • TypeScript in depth — The type system as a design tool. Inference, narrowing, generics, and the errors that mean the model is wrong — not a catalog of utility types.
  • CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
  • Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
  • Docker — What an image is, why layers cache, and how containers find each other on a network.
  • Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
  • Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
  • AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
  • Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.