Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- create table empleado (
- nombre_empleado varchar(20),
- departamento_id int
- );
- create table departamento (
- id int,
- nombre varchar(20)
- );
- insert into departamento values (31, 'ventas');
- insert into departamento values (32, 'logistica');
- insert into departamento values (33, 'informática');
- insert into departamento values (34, 'legal');
- insert into departamento values (35, 'marketing');
- select * from departamento;
- insert into empleado values ('Rodrigo', 31);
- insert into empleado values ('Cristian', 33);
- insert into empleado values ('Vicente', null);
- insert into empleado values ('Eugenia', 33);
- insert into empleado values ('Maximiliano', 34);
- insert into empleado values ('Patricio', 34);
- insert into empleado values ('Diego', null);
- select * from empleado;
- -- join --
- select *
- from empleado
- join departamento
- on empleado.departamento_id = departamento.id;
- -- left join
- select *
- from empleado
- left join departamento
- on empleado.departamento_id = departamento.id;
- -- right join
- select*
- from empleado
- right join departamento
- on empleado.departamento_id = departamento.id;
- -- full join
- select *
- from empleado
- full join departamento
- on empleado.departamento_id = departamento.id;
- -- right excluding join
- select *
- from empleado
- right join departamento
- on empleado.departamento_id = departamento.id where empleado.departamento_id is null;
- -- cambiamos nuestra perspectiva, a es departamento y b es empleado
- select *
- from departamento
- left join empleado
- on departamento.id = empleado.departamento_id where empleado.departamento_id is null;
- -- SEGUIMOS CON NUESTRA PERSPECTIVA ORIGINAL
- select *
- from empleado
- full join departamento
- on empleado.departamento_id = departamento.id
- where empleado.departamento_id is null or departamento.id is null;
- -- **************** --
- -- NUEVO EJERCICIO --
- -- **************** --
- create table maestro_pokemon
- (
- id_maestro_pokemon int,
- nombre_maestro_pokemon varchar(25)
- );
- create table pokemon
- (
- id_pokemon int,
- nombre_pokemon varchar(20),
- id_maestro_pokemon int -- aquí se hace la relación entre tablas. Los insert aclararán más este tema
- );
- insert into maestro_pokemon values(1,'Rodrigo');
- insert into maestro_pokemon values(2,'Diego');
- insert into maestro_pokemon values(3,'Patricio');
- insert into maestro_pokemon values(4,'Eugenia');
- insert into maestro_pokemon values(5,'Maximiliano');
- insert into maestro_pokemon values(6,'Salvador');
- insert into maestro_pokemon values(7,'Cristobal');
- insert into maestro_pokemon values(8,'Consuelo');
- insert into pokemon values (50, 'Meowth', 1);
- insert into pokemon values (51, 'Bulbasaur', 2);
- insert into pokemon values (52, 'Ivysaur', null);
- insert into pokemon values (53, 'Venusaur', 3);
- insert into pokemon values (54, 'Charmander', 4);
- insert into pokemon values (55, 'Charmeleon', 5);
- insert into pokemon values (56, 'Charizard', null);
- insert into pokemon values (57, 'Squirtle', 6);
- insert into pokemon values (58, 'Wartortle', null);
- insert into pokemon values (59, 'Nidoran', 7);
- insert into pokemon values (60, 'Pikachu', 1);
- -- JOIN
- select *
- from pokemon
- join maestro_pokemon
- on pokemon.id_maestro_pokemon = maestro_pokemon.id_maestro_pokemon;
- -- LEFT JOIN
- select *
- from pokemon
- left join maestro_pokemon
- on pokemon.id_maestro_pokemon = maestro_pokemon.id_maestro_pokemon;
- -- RIGHT JOIN
- select *
- from pokemon
- right join maestro_pokemon
- on pokemon.id_maestro_pokemon = maestro_pokemon.id_maestro_pokemon;
- -- FULL JOIN
- select *
- from pokemon
- full join maestro_pokemon
- on pokemon.id_maestro_pokemon = maestro_pokemon.id_maestro_pokemon;
- -- RIGHT EXCLUDING JOIN
- select *
- from pokemon
- right join maestro_pokemon
- on pokemon.id_maestro_pokemon = maestro_pokemon.id_maestro_pokemon
- where pokemon.id_maestro_pokemon is null;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement