Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CREATE TABLE Departamentos
- (
- Id int,
- Nombre varchar(20)
- );
- CREATE TABLE Empleados
- (
- Nombre varchar(20),
- DepartamentoId int
- );
- INSERT INTO Departamentos VALUES(31, 'Ventas');
- INSERT INTO Departamentos VALUES(32, 'Logistica');
- INSERT INTO Departamentos VALUES(33, 'Informatica');
- INSERT INTO Departamentos VALUES(34, 'Legal');
- INSERT INTO Departamentos VALUES(35, 'Marketing');
- INSERT INTO Empleados VALUES('Rodrigo', 31);
- INSERT INTO Empleados VALUES('Cristian', 33);
- INSERT INTO Empleados VALUES('Vicente', NULL);
- INSERT INTO Empleados VALUES('Eugenia', 33);
- INSERT INTO Empleados VALUES('Maximiliano', 34);
- INSERT INTO Empleados VALUES('Matilde', 34);
- INSERT INTO Empleados VALUES('Turuleca', NULL);
- /* Cláusula INNER JOIN */
- SELECT *
- FROM Empleados
- JOIN Departamentos
- ON Empleados.DepartamentoId = Departamentos.Id;
- /* lo mismo, pero más bonito (según el profesor) */
- SELECT
- E.Nombre as "Empleado",
- D.Nombre as "Departamento"
- FROM Empleados as E
- JOIN Departamentos as D
- ON E.DepartamentoId = D.Id;
- /* Cláusula LEFT JOIN */
- select *
- from empleados
- left join departamentos
- ON Empleados.DepartamentoId = Departamentos.Id;
- select
- E.nombre as "Empleado",
- D.nombre as "Departamento"
- from empleados as E
- left join departamentos as D
- ON E.DepartamentoId = D.Id;
- /* ************** Cláusula RIGHT JOIN */
- select *
- from empleados
- right join departamentos
- ON Empleados.DepartamentoId = Departamentos.Id;
- select
- E.nombre as "Empleado",
- D.nombre as "Departamento"
- from empleados as E
- right join departamentos as D
- ON E.DepartamentoId = D.Id;
- /* **************** Cláusula FULL JOIN */
- select *
- from empleados
- full join departamentos
- ON Empleados.DepartamentoId = Departamentos.Id;
- select
- E.nombre as "Empleado",
- D.nombre as "Departamento"
- from empleados as E
- full join departamentos as D
- ON E.DepartamentoId = D.Id;
- /* ************ Right Excluding JOIN */
- /* No existe en postgres */
- SELECT
- E.Nombre as "Empleado",
- D.Nombre as "Departamento"
- FROM Empleados E
- right JOIN Departamentos D
- ON E.DepartamentoId = D.Id where E.departamentoId is null;
- select Empleados.nombre as "Nombre", Departamentos.nombre as "Departamento"
- from Empleados
- right join Departamentos on Empleados.departamentoId = Departamentos.id
- where Empleados.departamentoId is null;
- select *
- from Empleados
- right join Departamentos
- on Empleados.departamentoId = Departamentos.id
- where Empleados.departamentoId is null;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement