Counting what is recorded

null handling

Counting what is recorded

Walmart SQL Interview Question

Walmart's leadership asked what share of the catalog is on discount. Before working out the share, you need two counts.

Return a single row with the total number of products as total_products and the number of products that have a discount recorded as discounted_products.

Asked of

  • Data Analyst
  • Analytics Engineer
  • Data Engineer
  • Data Scientist
  • ML Engineer

productsTable15 rows

Column NameType
product_idBIGINT
product_nameVARCHAR
categoryVARCHAR
brandVARCHAR
priceDOUBLE
discount_pctBIGINT
stock_quantityBIGINT
launch_dateDATE
is_activeBOOLEAN

productsExample Input

product_idproduct_namecategorybrandpricediscount_pctstock_quantitylaunch_dateis_active
1Wireless MouseElectronicsLogitech24.99101502023-02-14true
2Mechanical Keyboard ProElectronicsKeychron119NULL402023-05-01true
3Noise Canceling HeadphonesElectronicsSony299.991502022-11-20true
4Standing DeskFurnitureFlexispot349NULL122023-08-09true
5Ergonomic Chair ProFurnitureSteelcase1299552021-06-30true
6Desk LampFurnitureNULL39.5NULL802024-01-15true
7USB-C HubElectronicsAnker45202002023-03-22true
8Notebook SetStationeryMoleskine18NULL02022-09-01false

Example Output

total_productsdiscounted_products
84

Explanation

There are 8 products in the example. Only 4 of them have a discount recorded: the Wireless Mouse, Noise Canceling Headphones, Ergonomic Chair Pro and USB-C Hub. The other 4 have no discount, so they count toward the total but not toward the discounted products.

The example above is a small slice of the data. Your query runs against the full tables.