Given an ER diagram of restaurants, orders, order_items and menus, write SQL to generate several result sets, applying each table's is_active flag in every join condition.
Asked in the technical phone screen stage. No window functions needed; the catch was the is_active filter on every join.
Output
- Return labeled rows for active restaurant menus, active orders, and active order items.
- Columns:
result_set, restaurant_id, restaurant_name, related_id, item_name, and amount.
- Sort by
result_set, restaurant_id, and related_id.
Output details
- Use the labels
active_order_items, active_orders, and active_restaurant_menus for the three result sets.
- Return rows in ascending order by
result_set, then restaurant_id, then related_id.
- For
active_order_items, set related_id to order_id, item_name to the menu item name, and amount to quantity * price.
- For
active_orders, set related_id to order_id and return NULL for item_name and amount.
- For
active_restaurant_menus, set related_id to menu_id, item_name to the menu item name, and amount to the menu price.
- Preserve duplicate rows across result sets rather than deduplicating them.