select concat('[',result,']') as films_by_category from (
select GROUP_CONCAT('{', line, '}' SEPARATOR ', ') as result
from (
select CONCAT(
'"', category, '": ', most_rented_films
) as line
from (select c.name as category,count(f.title) as most_rented_films
from film f
inner join film_category fc using(film_id)
inner join category c using(category_id)
group by c.name
order by category
) as json_line
) as json_single_result
) as json_array
;
show status like 'Last_query_cost';