Welcome to Stackoverflow.
To obtain the data you need, you only have to join the tables ( JOIN
) with the reference keys and group them when necessary.
Let's start joining tables:
SELECT
e.Nombre,
e.Paterno,
e.Materno,
d.Nombre Departamento,
p.Descripcion Puesto,
c.Salario
FROM
empleado e
INNER JOIN
(
SELECT Id_Emp, MAX(Fecha_Inicio), Id_Depto, Id_Puesto
FROM labora GROUP BY Id_Emp, Id_Depto, Id_Puesto
) l
ON e.Id_Emp = l.Id_Emp
INNER JOIN departamento d ON l.Id_Depto = d.Id_Depto
INNER JOIN puesto p ON l.Id_Puesto = p.Id_Puesto
INNER JOIN contiene c ON p.Id_Puesto = c.Id_Puesto AND d.Id_Depto = c.Id_Depto
GROUP BY e.Id_Emp
I have not tried the query, but it should work.
Although I see some perhaps complicated points in your data design:
- You will notice that I have used a sub-query to filter by the maximum date in the table
labora
. I am not convinced that this table has been conceived as a kind of history where the dates in which an employee occupied a position and a department are kept at the same time.
- I do not know why to know the salary it is necessary to refer to two columns (it seems a somewhat rigid and very precarious conception that would become hellish when it happens that a person from the same department and position can have a different salary from another).