Flatten nested JSON employee profiles into one row per skill in SQL
Company: Snowflake
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Each `profile_data` holds `name`, `department`, and a `skills` array of objects with `name` and `level`. Flatten it to one row per skill with columns `id`, `name`, `department`, `skill`, `proficiency`, ordered by `id` then `skill` ascending.
```hint Text versus JSON
PostgreSQL has one JSON operator that returns JSON and another that returns text. If your output shows names wrapped in double quotes, you picked the wrong one.
```
### Input Table(s)
`employees_json`
| column | type | meaning |
|---|---|---|
| id | INTEGER | Employee identifier (primary key) |
| profile_data | JSON | Employee profile, never NULL |
Each `profile_data` value is an object of this shape:
```json
{
"name": "Alice",
"department": "Engineering",
"skills": [
{"name": "Python", "level": 5},
{"name": "Go", "level": 3}
]
}
```
### Output Contract
Return one row per element of the `skills` array, with these columns in this order:
| column | type | meaning |
|---|---|---|
| id | INTEGER | The employee's `id` |
| name | TEXT | The profile's `name`, as plain text without JSON quotes |
| department | TEXT | The profile's `department`, as plain text |
| skill | TEXT | The skill object's `name` |
| proficiency | INTEGER | The skill object's `level` |
Order by `id` ascending, then `skill` ascending, then `proficiency` ascending.
### Example
`employees_json`
| id | profile_data |
|---|---|
| 1 | `{"name": "Alice", "department": "Engineering", "skills": [{"name": "Python", "level": 5}, {"name": "Go", "level": 3}]}` |
| 2 | `{"name": "Bob", "department": "Analytics", "skills": [{"name": "SQL", "level": 4}]}` |
| 3 | `{"name": "Chen", "department": "Data Science", "skills": [{"name": "Statistics", "level": 4}, {"name": "R", "level": 2}, {"name": "Python", "level": 4}]}` |
Expected result:
| id | name | department | skill | proficiency |
|---|---|---|---|---|
| 1 | Alice | Engineering | Go | 3 |
| 1 | Alice | Engineering | Python | 5 |
| 2 | Bob | Analytics | SQL | 4 |
| 3 | Chen | Data Science | Python | 4 |
| 3 | Chen | Data Science | R | 2 |
| 3 | Chen | Data Science | Statistics | 4 |
Six skills across three profiles give six rows. Within each employee the rows follow the skill name, so Alice's "Go" comes before "Python" even though the array lists "Python" first.
### Constraints and Clarifications
- The table has at most 100 rows, and `id` values are unique.
- `profile_data` always contains `name` (string), `department` (string) and `skills` (an array with at least one element), so every employee produces at least one row.
- Each skill has `name` (string) and `level` (integer from 1 to 5).
- If a profile lists the same skill name more than once, each entry is its own row, and those rows are ordered by `proficiency` ascending.
- The order of keys inside a JSON object does not matter.
Overview: Write a PostgreSQL query that unpacks a JSON profile column, pulls out each employee's name and department, and expands the nested skills array into one row per skill with its level. It tests JSON operators, lateral joins with set-returning functions and type casting.