There are table employees and departments. It is clear that the employee works in some department. But the moment is that he can work first in one department, then another, etc.
We have a third table in which the employee is stored, where the employee worked / works. Accordingly, there are columns: employee id, department id, and two dates - which one for which he worked / works in this department.
I want to get for all employees - in which department and from what date it works.
I took all of this from the mysql test database and there they do it in two queries:
1) first choose from the table max (starting date) and group by employee id and shove it all into a temporary table
2) and then join the resulting table with the department table to get the last place of work (department for each employee).
But isn't it too hard?
The procedure itself can be seen here (show_departments)