Skip to main content

Why Join is important in SQL #DOTC_Mdb

Why Join is Important in SQL.

 

Hello All,

Today I will discuss why SQL Joins is important in database , before going the importance of Join in SQL  Please go through my previous blogs i.e. SQL and Classification of SQL so it’s easy for you to understand joins.

 https://dheeraj60.blogspot.com/2020/05/importance-of-sql-and-classification-of.html

https://dheeraj60.blogspot.com/2020/05/sql-commands-uses-and-benefits-which.html

Why Join is important:

The SQL Joins clause is used to combine records from two or more tables in a database. A JOIN is a means for combining fields from two tables by using values common to each. To stitch the database back together to make it easy to read and use database joins are used. They match rows between tables. In most cases we're matching a column value from one table with another. So In Simple word we can say Joins is basically used to get the data from one or more tables to get the required data and Joins can be of different types which can be used for trend analysis in any organization.

Let’s Understand different kind of Oracle joins that allow you to query data from two or more tables and get the required output.

Oracle SQL joins used to combine columns from two or more tables by comparing the values of related columns . The related columns can be the primary columns of the first table and foreign key column of second table.

Important : Main advantage of a join includes that it executes faster. The retrieval time of the query using joins almost always will be faster than that of a subquery. By using joins, you can maximize the calculation burden on the database i.e., instead of multiple queries using one join query

In my previous blog I already explained primary key and foreign key .

Note : Without using primary key and foreign key we can also get result but logically we should use primary and foreign key to relate the columns and get the output in proper way.

In Oracle or other RDBMS Database supports below Joins.

1.      Equi Join

2.      Self Join

3.      Left Join

4.      Right Join

5.      Cross Join

6.      FULL Join

We will define and use above joins when we created at least two tables and some data in table.

Creating two tables by using SQL to understand joins by using SQL DDL create command.

 

CREATE  TABLE STUDENT

(ID INT PRIMARY KEY , 

 Name Varchar2(20)  

);

 

If you install My SQL in your System Varchar2 will not support so you can simply use Varchar instead of Varchar2 . Varchar2 supports in Oracle , Sybase , DB2 etc database . Since My SQL is free ware so for learning you can use varchar.

 

In this table first column id -- ---  int is data type and primary key is constraint

2nd column Name --- Data type is Varchar2(20)

 

CREATE  TABLE STUDENT_DETAILS

(ID INT  , 

 ADDRESS Varchar2(20)  

);

 

In this table I simply put data types for both the columns. int is for number and varchar2
is for character.

 

 

---- Now we insert the data in table STUDENT by using one simple command of SQL DML i.e. INSERT

 

---- Inserting data in STUDENT TABLE

 

INSERT INTO STUDENT (ID,NAME) VALUES (1, 'Ashish');

INSERT INTO STUDENT (ID,NAME) VALUES (2, 'Prashant');

INSERT INTO STUDENT (ID,NAME) VALUES (3, 'Deepak');

INSERT INTO STUDENT (ID,NAME) VALUES (4, 'Krishna');

 

Note : For int data type no need to put any quotes but for character we need to put single quotes.

 

-- Inserting data in STUDENT_DETAILS TABLE

 

INSERT INTO STUDENT_DETAILS (ID,Address) VALUES (1, 'MDB');

INSERT INTO STUDENT_DETAILS (ID,ADDRESS) VALUES (2, 'DBG');

INSERT INTO STUDENT_DETAILS (ID,ADDRESS) VALUES (5, 'PAT');

INSERT INTO STUDENT_DETAILS (ID,ADDRESS) VALUES (6, 'MZB');

 

We have inserted 4 values in both values with some similar ID and  other ID so that we can see the output which matched or not matched by using several Joins;

 

Let see the table values of both table by using select statement

 

mysql> select * from student;

+----+----------+

| ID | Name     |

+----+----------+

|  1 | Ashish   |

|  2 | Prashant |

|  3 | Deepak   |

|  4 | Krishna  |

+----+----------+

 

 

mysql> select * from student_details;

+------+---------+

| ID   | ADDRESS |

+------+---------+

|    1 | MDB     |

|    2 | DBG     |

|    5 | PAT     |

|    6 | MZB     |

+------+---------+

 

 

 

1.      Equi Join:  Equi Join is basically used to find the matched records from the tables .

 

Syntax and output :

 

mysql> select a.id,a.name , b.id, b.address from student a , student_details b where a.id=b.id;

 

+----+----------+------+---------+

| id | name     | id   | address |

+----+----------+------+---------+

|  1 | Ashish   |    1 | MDB     |

|  2 | Prashant |    2 | DBG     |

+----+----------+------+---------+

 

Here we can see we got only 2 records as it’s matched records in both table id is 1 and 2. Other unmatched record will not display.

 

Note : Here we use a and b as alias for tables to our convenient as it’s easy to use in condition . We can also directly use table name instead of alias but it’s better to use alias if we join n number of tables then it’s easy for us to put condition via alias.

 

2.      Self Join : Join the table itself is known as Self join .  table join to itself to query hierarchical data using an inner join, left join, or right join is also known as self Join.

 

 

Syntax and Output :

 

mysql> SELECT

             a.id ,

             a.name,

            b.id,

            b.address

            FROM

           student a

          INNER JOIN student_details b  ON a.id = b.id;

+----+----------+------+---------+

| id | name     | id   | address |

+----+----------+------+---------+

|  1 | Ashish   |    1 | MDB     |

|  2 | Prashant |    2 | DBG     |

+----+----------+------+---------+

 

Here we also see the 2 output , but in self join it depends we can join table itself , inner join , left join and right join . As per data we use inner join tha’s why the output is 2 and similar to equi join.

 

 

Note : Here we use a and b as alias for tables to our convenient as it’s easy to use in condition . We can also directly use table name instead of alias but it’s better to use alias if we join n number of tables then it’s easy for us to put condition via alias.

 

3.      Left Join:  The left Join is basically to get all the records from Left table and  only matched records from right table .

 

 

Syntax and Output:

 

mysql> SELECT

         a.id id,

         a.name,

         b.id,

        b.address

     FROM

        student a

    LEFT JOIN student_details b ON a.id = b.id;

+----+----------+------+---------+

| id | name     | id   | address |

+----+----------+------+---------+

|  1 | Ashish   |    1 | MDB     |

|  2 | Prashant |    2 | DBG     |

|  3 | Deepak   | NULL | NULL    |

|  4 | Krishna  | NULL | NULL    |

+----+----------+------+---------+

4 rows in set (0.07 sec)

 

Here we see the output from 2nd table i.e student_details only matched record is showing and other values are null but in left table Student It’s showing all the values . This is Left Join

 

 

Note : Here we use a and b as alias for tables to our convenient as it’s easy to use in condition . We can also directly use table name instead of alias but it’s better to use alias if we join n number of tables then it’s easy for us to put condition via alias.

 

 

4 . Right Join : The Right Join is basically to get all the records from Right table and  only matched records from left  table just vice versa we can say after seeing the Left Join .

 

Syntax and Output:

 

mysql> SELECT

             a.id id,

             a.name,

             b.id,

             b.address

            FROM

           student a

          RIGHT JOIN student_details b ON a.id = b.id;

+------+----------+------+---------+

| id   | name     | id   | address |

+------+----------+------+---------+

|    1 | Ashish   |    1 | MDB     |

|    2 | Prashant |    2 | DBG     |

| NULL | NULL     |    5 | PAT     |

| NULL | NULL     |    6 | MZB     |

+------+----------+------+---------+

4 rows in set (0.00 sec)

 

Here we see the all records from right table i.e. Student_details table and only matched records from left table Student as other records it’s showing null . This is Right Join.

 

 

Note : Here we use a and b as alias for tables to our convenient as it’s easy to use in condition . We can also directly use table name instead of alias but it’s better to use alias if we join n number of tables then it’s easy for us to put condition via alias.

 

 

 

5 .  Full Join : Full Join used to get all the records from left and right table

 

Syntax:

 

SELECT

    a.id id,

    a.name,

    b.id,

    b.address

FROM

    student a

FULL OUTER JOIN  student_details b ON a.id = b.id;

 

Here we get all the records from with the matching rows from both tables available. If it’s no match, the missing  will have nulls values.

 

 

Note : Here we use a and b as alias for tables to our convenient as it’s easy to use in condition . We can also directly use table name instead of alias but it’s better to use alias if we join n number of tables then it’s easy for us to put condition via alias.

 

6 . Cross Join :  If we take records from both tables and will not put any condition that is known as Cross Join . Also known as Cartesian Product as we not given any condition so it will multiply the records from Table A and Table B.

 

Syntax and Output :

 

mysql> select a.id , a.name, b.id , b.address from student a , student_details b;

+----+----------+------+---------+

| id | name     | id   | address |

+----+----------+------+---------+

|  1 | Ashish   |    1 | MDB     |

|  2 | Prashant |    1 | MDB     |

|  3 | Deepak   |    1 | MDB     |

|  4 | Krishna  |    1 | MDB     |

|  1 | Ashish   |    2 | DBG     |

|  2 | Prashant |    2 | DBG     |

|  3 | Deepak   |    2 | DBG     |

|  4 | Krishna  |    2 | DBG     |

|  1 | Ashish   |    5 | PAT     |

|  2 | Prashant |    5 | PAT     |

|  3 | Deepak   |    5 | PAT     |

|  4 | Krishna  |    5 | PAT     |

|  1 | Ashish   |    6 | MZB     |

|  2 | Prashant |    6 | MZB     |

|  3 | Deepak   |    6 | MZB     |

|  4 | Krishna  |    6 | MZB     |

+----+----------+------+---------+

16 rows in set (0.00 sec)

 

As we not put any condition in select statement simply take table so output is 4 * 4 =16 as in both table record is 4.

 

 

Note : Here we use a and b as alias for tables to our convenient as it’s easy to use in condition . We can also directly use table name instead of alias but it’s better to use alias if we join n number of tables then it’s easy for us to put condition via alias.

 

Important : Why Join prefers over Sub query. Main advantage of a join includes that it executes faster. The retrieval time of the query using joins almost always will be faster than that of a subquery. By using joins, you can maximize the calculation burden on the database i.e., instead of multiple queries using one join query

 

 

 

Note : If you install My SQL in your System Varchar2 will not support so you can simply use Varchar instead of Varchar2 . Varchar2 supports in Oracle , Sybase , DB2 etc database . Since My SQL is free ware so for learning you can use varchar.

 

 

Please go through this blog carefully and understand why Join is so important in SQL and also all the types joins and it’s uses in SQL  if you have any issue let me know. In next blog I will come with different  topic. I suggest you to use Joins in more than 2 tables to get the result as per your requirement. As mentioned in Blog Join is also faster than sub query.  This blog is important for candidate who is preparing for SQL interview as lot’s of question will be from join topic.

 

Thanks.





Comments

Post a Comment

Popular posts from this blog

SQL and Classification of SQL in Oracle Database #Diksha Oracle Training Centre

  SQL and Classification of SQL in Oracle Database.   SQL is Structured Query Language , which is used for storing, manipulating and retrieving data stored in a relational database .SQL is the standard language for RDBMS. All the Relational Database Management Systems (RDMS) like Oracle, MySQL, Sybase, Informix, IBM DB2 and Microsoft SQL Server use SQL as their standard database language. Oracle is one of the more secured database as compared to other databases. Importance of   SQL : SQL and PL/SQL is a backend process where all data is stored and retrieved in GUI which created either by any programming languages like Java, C++, PHP etc. so we need to have very secure database so that there will be no impact for users. SQL allows users to access data in the relational database management systems. SQL is used to communicate with a database.SQL and PL/SQL allows users to create and drop databases tables , views , stored procedures , functions , packages , trigger etc. SQL allows

Materialized View uses and Benefits in Database #DOTC_Mdb

Materialized View uses and Benefits in Database. Hello Everyone, Today we will discuss about Materialized view as it’s play important role in database. We already discussed about Simple Views and complex views in my previous blog. Before Materialized view go through my previous blog which related to simple view. https://dheeraj60.blogspot.com/2020/05/benefits-of-creating-and-using-view-in.html As we know View is not a database object and not like table which is stored in database, but view can be created from base table a view is an SQL statement that’s stored in the database. This statement, or view, has a name.A view looks and acts a lot like a table. It has columns and rows, and can be included in SELECT queries just like a table. In other word we can say View is a virtual/logical table which is basically used for security purpose. Let’s understand   about   Materialized view : A materialized view is a view that stores the results of the view’s query. Whenever you query the ma

Top 50 Interview Questions On SQL and PL/SQL #DOTC_Mdb

                    Top 50 Interview Questions On SQL and PL/SQL. Today we will Discuss Top 50 interview questions and answers of SQL and PL/SQL which is frequently asked in interview.     Question 1: What is SQL and Classification of SQL? Answer SQL is a Structure Query Language which is vastly used in RDBMS database like Oracle, Sybase, DB2 , Microsoft SQL server etc.   Classification of SQL is as below: DDL (Data Definition Language):  Commands are  create , alter , drop , truncate etc DML (Data Manipulation Language) : Commands are  insert,update and delete . TCL (Transaction Control Language ) : Commands are  Commit , Rollback and Save point. DCL (Data Control Language) : Commands are Grant , Revoke Question 2:    What is meant by Joins? What are the types of join? Answer Joins are basically used to extract/get data from multiple tables using some common columns or conditions and also by using alias to fulfill the condition.   There are various types of Joins as li