What are SQL Joins and how tables are related to each other

What are SQL Joins and how tables are related to each other
What are SQL Joins and how tables are related to each other

Relational databases rely on relationships between their entities (tables and views).

However, in order to be able to call associated records from two or more tables / views above, we must somehow declare with which field they are connected to each other.

This is exactly what using SQL Joins does.

There are five types of Joins: Left Join, Right Join, Inner Join, Full Join, Cross join.

Let's explain the most basic ones one by one with examples.

To begin with, we will make 2 tables with data so that the difference between each type is easier to understand:

create table pelatis (
  id integer,
  id_xwra integer,
  onoma_pelati varchar(50));
create table xwres (
  id integer,
  onoma_xwras varchar(50));

insert into pelatis values
(1,1,'Stratos'),
(2,1,'Maria'),
(3,4,'Georgia'),
(4,10,'Nikos'),
(5,null,'Iasonas');

insert into xwres values
(1,'Ellada'),
(2,'Ispania'),
(3,'Italia'),
(4,'Agglia'),
(5,'Germania');

select * from pelatis;

select * from xwres;
What are SQL Joins and how tables are related to each other
01

Left Join

In Left Join we bring all the records of the first table even if there are no records matching the second table in the related field.

What are SQL Joins and how tables are related to each other
02

In the example we declare that we do a left join of the second table "xwres" in the field "id_xwres" of the first, with the field "id" of the second.

The result is that those records that have not been defined with a common id also appear, but with the "id" appearing null (i.e. empty).

select * from pelatis
left join xwres on pelatis.id_xwra = xwres.id;
What are SQL Joins and how tables are related to each other
03

Left Join with only the outer records

We apply this case when we want to find which records that exist in the first table are not associated in the second.

What are SQL Joins and how tables are related to each other
04

There are two ways for the same result the first is a normal one left join, using this time where in the field they are associated with when they are null in the second table.

select * from pelatis
left join xwres on pelatis.id_xwra = xwres.id
where xwres.id is null;

The same number of records will be returned if instead of join and is null we use correlated subquery exists (select).

select * from pelatis
where not exists (select xwres.id from xwres where xwres.id = pelatis.id_xwra);
What are SQL Joins and how tables are related to each other
05

Right Join

The right join works in reverse of the left join, i.e. we bring all the records of the second table even if there are no records matching the first table in the field they are associated with.

What are SQL Joins and how tables are related to each other
06

This time all records from the 'xwres' table will be displayed and those not associated with the 'pelatis' table will be displayed but as null in the field.

select * from pelatis
right join xwres on pelatis.id_xwra = xwres.id;
What are SQL Joins and how tables are related to each other
07

Inner Join

In the Inner Join we bring only the records that are associated in the field with both tables

What are SQL Joins and how tables are related to each other
08

By declaring inner before the join (although if we don't put anything it is the default) this time we will see the records that are associated, that is, only those customers who have a declared country.

select * from pelatis
inner join xwres on pelatis.id_xwra = xwres.id;
What are SQL Joins and how tables are related to each other
09

Full Join

In Full Join we bring all the records of the first table and all the records of the second table either related or not to each other in the field.

What are SQL Joins and how tables are related to each other
10

By executing the full join we will see that they will appear and customers who do not have a country and the customers they have and as many countries as do not have a customer.

select * from pelatis
full join xwres on pelatis.id_xwra = xwres.id;
What are SQL Joins and how tables are related to each other
11

Cross Join

With Cross Join we create a Cartesian product, that is, we combine the records of both tables with every possible combination.

select onoma_pelati,onoma|_xwras from pelatis
cross join xwres;
What are SQL Joins and how tables are related to each other
12

This was the analysis of Joins, their use needs a lot of care because many times we use Inner Join and we lose records that are not related to each other even though we wanted them. This leads to a wrong result.

Best practice is to use Left or Right Joins where Inner is not required.

Share it

Leave a reply