Skip to main content

SQL Joins and its importance in Oracle Database #Diksha Oracle Training Centre

 

SQL Joins and its importance in Oracle Database.

 

 Hello Everyone,

Today I will discuss why SQL Joins is important in any database , Before starting Join please go through my below blogs so it’s easy for you to write different types of Joins .

https://dikshaoracletraining.blogspot.com/2020/09/sql-and-classification-of-sql-in-oracle.html

https://dikshaoracletraining.blogspot.com/2020/09/basic-sql-commands-in-oracle-database.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. Join used to 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.

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.

Below are the types of Join.

  

1.      Equi Join / Natural Join

2.      Self Join

3.      Left Outer Join

4.      Right Outer Join

5.      FULL Outer Join

6.     Cross Join / Cartesian Product

 

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

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

 CREATE  TABLE STUDENT

(ID Number PRIMARY KEY ,

 Name Varchar2(20) 

);

 Creating Second Table :

CREATE  TABLE STUDENT_DETAILS

(ID NUMBER  ,

 ADDRESS Varchar2(20)

);


Now we are inserting some dummy 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');

INSERT INTO STUDENT (ID,NAME) VALUES (5, 'Vivek');

INSERT INTO STUDENT (ID,NAME) VALUES (6, 'Sanjeev');

INSERT INTO STUDENT (ID,NAME) VALUES (7, 'Simmy');

INSERT INTO STUDENT (ID,NAME) VALUES (8, 'Smita');


-----Now Inserting dummy data in STUDENT_DETAILS TABLE

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

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

INSERT INTO STUDENT_DETAILS (ID,ADDRESS) VALUES (3, 'Delhi');

INSERT INTO STUDENT_DETAILS (ID,ADDRESS) VALUES (4, 'Noida');

INSERT INTO STUDENT_DETAILS (ID,ADDRESS) VALUES (10, 'Ahmedabad');

INSERT INTO STUDENT_DETAILS (ID,ADDRESS) VALUES (11, 'Chennai');

INSERT INTO STUDENT_DETAILS (ID,ADDRESS) VALUES (12, 'Patna');

We have inserted dummy data in both tables  values with some similar ID and  other ID so that we can see the output which matched or not matched by using several types of Join.


1.   Equi Join/Inner Join:  Equi Join is basically used to find the matched/Common records from 2 or more tables .

Syntax and output :

select a.id,a.name , b.id, b.address

from student a , student_details b

where a.id=b.id;

Output of above query is as below:


 

 

Here we can see only matched and common records as output from both the tables. Other unmatched record will not display.

Note: Here we use a and b as alias for tables for 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 by using 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 :

SELECT A.id AS ID1, B.ID AS ID2, A.name

FROM Student A, student B

WHERE A.ID <> B.ID

ORDER BY A.name;

 

Output of Above Query is as below.



 

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

Syntax and Output:

 

SELECT

         a.id id,a.name,

         b.id,b.address

     FROM

        student a

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

 

Output of the above query is as below :

 


 

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. In such type of cases we use left Outer Join.

 

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

 

SELECT

      a.id id,a.name,

      b.id,b.address

           FROM

           student a

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

Below is the output of above query :



 

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 Outer Join.

  

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

 Syntax and Output:

SELECT

    a.id id, a.name,

    b.id, b.address

FROM

    student a

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

 

Below is the output of above query.

 


 

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.

  

6 . Cross Join :  If we take records from both tables and will not put any condition that is known as Cross Join . 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. Suppose One table have 10 records and another table contains 5 records and we use cross join then output will be 10*5=50 records.

 

Syntax and Output :

 select a.id , a.name, b.id , b.address

from student a , student_details b;

 

Output of the above query is as below.



 

Oracle join is mainly used to combine columns from two or more tables based on values of the related columns. 

Note that you can use join a table to itself to query hierarchical data using an inner join, left join, or right join. Join is very important in SQL and mostly used to fetch records from multiple tables.

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 related to SQL. I suggest you to use Joins in more than 2 tables to get the result as per your requirement. This blog is important for candidate who is preparing for SQL interview as lots of question will be from SQL Join topics.

Thanks

Comments

  1. Easily, the article is actually the best topic on this registry related issue. I fit in with your conclusions and will eagerly look forward to your next updates. Just saying thanks will not just be sufficient, for the fantasti c lucidity in your writing. I will instantly grab your rss feed to stay informed of any updates.
    IT Training

    ReplyDelete

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