SQL joins

SQL joins made easy - All you need to know

Β·

4 min read

Intro πŸ‘‹πŸΎ

I don't know !! , but many people seems blocked when it comes to sql joins πŸ™ƒ. Many of us uses sql on daily basis but yet when it comes to joins we suck( this is a funny reality) . Why is that ? Is it because we don't know how to use basic sets ? Or just because the name "join" frighten us ? Well, we'll tackle that rapidly and you'll be glad this post came across your pathπŸ˜„.

Definition - What is sql join ?

Sql join simply as it is, it means coupling two more tables in one query to print out records related to these tables.

This allows the power of relational databases to be tapped for results that combine data from multiple tables in an efficient manner.

Type of joins

Several methods exist to link up two or more tables. Here are some in the list below :

  • Inner join : This is the most common join you'll find in Sql. This join return records when the condition is true in both tables.

  • Left (outer) join : This join returns all records from the left table even if the condition is not verified on the right table.

  • Right (outer) join : This join returns all records from the right table even if the condition is not verified on the left table.

  • Self join : This join enable us to make a join with the same table as if it was another table.

  • Cross join : This join is quite particular. It generates a paired combination of each row of the first table with each row of the second table. This type of join is also known as cartesian join.

  • Full (outer) join : This join returns all records when there is a match in either the left or right table.

  • Natural join : This is a type of inner join that provides the result based on the column with the same data types present in tables to be joined.

Examples

We are going to use two(02) tables for our examples: student & study.

  • student ( code, firstname , lastname, email)

student table.PNG

  • study ( code_student, city, code_department)

study table.PNG

INNER JOIN

Inner join.PNG Query

-- Format query
SELECT *
FROM A 
INNER JOIN B ON A.key = B.key;

-- Example
SELECT * FROM student AS st
INNER JOIN study AS sy ON st.code = sy.code_student;

LEFT JOIN

Left join.PNG Query

-- Format query
SELECT *
FROM A 
LEFT JOIN B ON A.key = B.key;

-- Example
SELECT * FROM student AS st
LEFT JOIN study AS sy ON st.code = sy.code_student;

LEFT OUTER JOIN

left outer join.PNG Query

-- Format query
SELECT *
FROM A 
LEFT JOIN B ON A.key = B.key
WHERE B.key IS NULL;

-- Example
SELECT * FROM student AS st
LEFT JOIN study AS sy ON st.code = sy.code_student
WHERE sy.key IS NULL;

RIGHT JOIN

right join.PNG Query

-- Format query
SELECT *
FROM A 
RIGHT JOIN B ON A.key = B.key;

-- Example
SELECT * FROM student AS st
RIGHT JOIN study AS sy ON st.code = sy.code_student;

RIGHT OUTER JOIN

right outer join.PNG Query

-- Format query
SELECT *
FROM A 
RIGHT JOIN B ON A.key = B.key
WHERE B.key IS NULL;

-- Example
SELECT * FROM student AS st
RIGHT JOIN study AS sy ON st.code = sy.code_student
WHERE sy.key IS NULL;

SELF JOIN

Query

-- Format query
SELECT *
FROM A 
INNER JOIN B
ON A.key = B.key;

-- Example
SELECT * FROM student AS st
INNER JOIN study AS sy ON st.code = sy.code_student;

FULL JOIN

full join.PNG Query

-- Format query
SELECT *
FROM A 
FULL JOIN B ON A.key = B.key;

-- Example
SELECT * FROM student AS st
FULL JOIN study AS sy ON st.code = sy.code_student;

FULL OUTER JOIN

full outer join.PNG Query

-- Format query
SELECT *
FROM A 
FULL JOIN B ON A.key = B.key
WHERE A.key IS NULL OR B.key IS NULL ;

-- Example
SELECT * FROM student AS st
INNER JOIN study AS sy ON st.code = sy.code_student
WHERE st.KEY IS NULL OR sy.KEY IS NULL ;

CROSS JOIN

In this example we will consider heavy meal for dinner. First let's create two tables.Then we populate them with some data.

-- Creation of the tables
CREATE TABLE complement ( complement VARCHAR(100));
CREATE TABLE sujet( sujet VARCHAR(100));

-- Inserting sample data
INSERT INTO complement
VALUES ("Plantain"),("Riz"),("Patate");
INSERT INTO sujet
VALUES ("sauce_tomate"), ("sauce_creme"), ("lΓ©gumes");

-- Select query 
SELECT * from complement;
SELECT * from sujet;

-- Cross join query
SELECT * FROM complement 
CROSS JOIN sujet;
---- or
SELECT * FROM complement, sujet;

Results of the cross join query πŸ‘‡πŸΎ

Cross join between tables.PNG

Hope you had a great journey here, see you later on my next post βœ¨πŸ˜ƒ.

Β