← All SQL interview questions

Null-safe order totals with discounts and shipping

MediumNULL / NOT NULL HandlingZomatoMeesho

The checkout table leaves discount and shipping_fee as NULL when they do not apply, which silently breaks arithmetic totals.

From checkout_lines, return line_id, gross_amount, discount_applied (0 when missing), shipping_charged (0 when missing) and net_payable = gross − discount + shipping, treating missing values as 0. Also return coupon_label, which should read No coupon when coupon_code is NULL.

Sample data

Table: checkout_lines

12 row(s) · showing first 8

line_idINTgross_amountNUMERIC(10,2)discountNUMERIC(10,2)shipping_feeNUMERIC(10,2)coupon_codeTEXT
11200.00100.0040.00SAVE100
2899.00NULL40.00NULL
32450.50250.00NULLFLAT250
4349.00NULLNULLNULL
55600.00560.000.00TEN
6749.9950.00NULLFIFTY
71899.00NULL60.00NULL
83200.00320.0040.00TEN

Practising alone only goes so far — our live cohorts pair SQL depth with dashboards, agentic analytics projects and mock interviews.

Explore the program