CREATE VIEW v_students_institutions AS 
SELECT u.id, i.city_id, p.institution_id, u.place_id, u.name, u.email, p.name AS place_name,
i.name AS institution_name, c.name AS city_name, u.created_at, 
(SELECT COUNT(*) FROM course_user AS cu WHERE cu.user_id = u.id AND cu.status=3) AS courses_finished, 
(SELECT COUNT(*) FROM course_user AS cu WHERE cu.user_id = u.id AND cu.status=2) AS courses_pending, 
(SELECT COUNT(*) FROM course_user AS cu WHERE cu.user_id = u.id AND cu.status=1) AS courses_current
FROM users AS u 
JOIN model_has_roles AS mhr ON u.id = mhr.model_id 
LEFT JOIN places AS p ON u.place_id = p.id 
LEFT JOIN institutions AS i ON p.institution_id = i.id 
LEFT JOIN cities AS c ON i.city_id = c.id 
WHERE u.status != 0 AND mhr.role_id = 2;