×

sql · level 3 · unit 7 · lesson 3

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

turns a JSON list into rows, and back again.

Each recipe stores its tags as a JSON list, such as ["spicy", "vegan"]. To count tags, each tag needs its own row. json_each does that. Write it after a comma in FROM, as json_each(r.tags) AS t, and every item becomes a row, in t.value.

The other way builds JSON for an app. gathers many rows into one sorted list. builds named values.

Together, you can unpack, count and repack data in one query.

SELECT t.value AS tag, COUNT(*) AS recipes
FROM recipes AS r, json_each(r.tags) AS t
GROUP BY t.value
ORDER BY recipes DESC, tag;

json_each runs once for every recipe row, so each recipe's tags line up with its name.