The task is to display the name of the head of the department and the name of the department in which the employees completed the maximum number of projects.
I got such a request, but it does not display exactly what is needed.
Made it by example from here (second paragraph).
SELECT department_name, max_amount FROM ( SELECT dep_name AS department_name, COUNT(rel_prj_id) AS projects_amount FROM employees INNER JOIN departments ON emp_dep_id = dep_id INNER JOIN rel_prj_emp ON rel_emp_id = emp_id GROUP BY dep_name ) X INNER JOIN ( SELECT MAX(projects_amount) AS max_amount FROM ( SELECT dep_name AS department_name, COUNT(rel_prj_id) AS projects_amount FROM employees INNER JOIN departments ON emp_dep_id = dep_id INNER JOIN rel_prj_emp ON rel_emp_id = emp_id GROUP BY dep_name ) X ) Y ON projects_amount = max_amount Conclusion, despite the fact that there are only three projects: 
What is the problem - I think you will understand if you look at the database schema. Each department should correspond to several projects, but departments and projects are connected through the table of employees - and therefore in my request each department has more records than necessary.
I grouped by department name, but it turns out that in each group there are employees of this department. How to link the tables differently or group them differently - I did not invent it, I am in a stupor. If you go on the other hand, grouped by project id - in each group there will be the number of employees who worked on the project is also different.
It is necessary to solve the problem according to this scheme. I know that it can be simplified, but the task is educational.
Database Creation Script: http://pastebin.com/hnHnENpX .
Thank you in advance.

count(distinct rel_prj_id)will give the number of projects by department. Only with your data in all departments on 3 projects - Mike