SQL interview question: Select Every Other Row

Select Every Other Row

A data engineer needs to sample every other record for an A/B testing control group.

Select every other row (rows with odd row numbers when ordered by order_id).

Return order_id, customer_name, and order_date. Use ROW_NUMBER() with modulo operator (% 2 = 1).

Skills: ROW_NUMBER with modulo operator

Tables

orders 15 rows
  • order_id integer PK
  • customer_id integer → customers
  • order_date text
  • total_amount real
  • status text
order_idcustomer_idorder_datetotal_amountstatus
11012024-01-20249.99delivered
21012024-03-151999.99delivered
41012024-09-0579.99delivered
customers 12 rows
  • customer_id integer PK
  • name text
  • email text
  • city text
  • signup_date text
customer_idnameemailcitysignup_date
101Alice Mitchell[email protected]San Francisco2024-01-15
102Bob Chen[email protected]New York2024-02-20
103Carol Davis[email protected]Los Angeles2024-03-10

Keep going