Write a SQL query to flatten hierarchical or nested data into a tabular format.
Assume the input is represented by divisions, teams, and members, where teams belong to divisions and members may belong to teams. Preserve divisions and teams even when their child rows are absent.
Output
- One row per team-member combination, plus rows for divisions or teams without children.
- Return
division_id, division_name, team_id, team_name, member_id, member_name, and member_role.
- Sort by division, team, and member identifiers, placing missing child rows last.
Output details
- Sort
division_id, team_id, and member_id in ascending order, placing NULL team_id and member_id values last.
- Preserve NULL values in
member_id, member_name, and member_role without replacement.
- Retain duplicate matching rows rather than deduplicating them.