Q:

mysql count and join 4 tables

I have the following table structure:
Table folder
id | title | is_hidden 
Table object
id | name | is_hidden | folder_id
Table label
id | name
Table label_to_folder
id | label_id | folder_id

Each object always belongs to a folder.  
Each folder may belong to one or more labels (this is defined by the label_to_folder table)

I'm trying to get the most used (popular) labels, and I achieve it (I think :P) with the following query:
SELECT COUNT(lf.label_id) AS cnt, lf.label_id, l.name FROM label_to_folder lf JOIN label l ON lf.label_id=l.id  GROUP BY lf.label_id ORDER BY cnt DESC limit 8 
What I'm having trouble with, is extent the above query so it does not count hidden folders (folder.is_hidden=1) or empty folders (an empty folder is one with no objects at all or all the folder's objects are hidden)  
Any help appreciated.
EDIT 
sqlfiddle: http://sqlfiddle.com/#!9/65526/1
The result there includes folder with id 5, but all its folders are empty - that's what I'm trying to eliminate.

A:

You get used folders thus:
select f.id
from folder f
where f.is_hidden = 0
and exists 
(
  select * 
  from object o
  where o.folder_id = f.id
  and o.is_hidden = 0
)

Then you can add this to your query:
where lf.folder_id in (<above subquery>)

