Company: Pocketpills Backend Engineer Intern Oncampus_18july
Difficulty: medium
A retail team stores its customers and their purchase history in two tables. For every customer, they want to look past the latest purchase and see the one before it. Write a single SQL query that, for every customer who has placed at least two orders , returns that customer's second most recent order. An order is "more recent" than another order of the same customer when its order_date is later. If two orders of the same customer share the same order_date , the one with the larger order_id is treated as the more recent of the two. Under this rule every customer's orders have a strict newest-to-oldest ranking, so "second most recent" is always a single order. Schema CREATE TABLE Customers ( customer_id INTEGER PRIMARY KEY, customer_name TEXT NOT NULL ); CREATE TABLE Orders ( order_id INTEGER PRIMARY KEY, customer_id INTEGER NOT NULL, order_date TEXT NOT NULL, amount INTEGER NOT NULL ); order_date is stored as text in YYYY-MM-DD form, so ordinary string comparison orders dates correctly