Diwali Deal : Flat 20% off + 2 free self-paced courses + $200 Voucher - SCHEDULE CALL
Do you want to learn how to use Inner Join in your SQL queries? If so, you've come to the right place! With this blog post, we'll go through step-by-step instructions on how to make join tables for data that resides in multiple databases and merge them into a single result set. We'll start by defining an inner join, discuss why it's important, then explain several types of joins including left and right outer joins as well as full or cross joins.
The INNER JOIN keyword is used to select records with values that match in both tables. Based on the join-predicate, the INNER JOIN combines the column values of two tables (table1 and table2) to create a new result table. To find all pairs of rows that meet the join-predicate, the query compares each row in table1 to each row in table2. The column values for each matched pair of rows A and B are combined into a result row when the join-predicate is satisfied.As an illustration, take the Sales Register. Any application typically stores sales data in the Sales Header or Sales Detail files. Data from the Customer master and the Item master must also be extracted. Even though each table has a primary key and a foreign key to help you connect them, extracting data from all of them requires a specific method or tool. This procedure or tool goes by the name Joins.
Any FROM clause can make use of an INNER JOIN operation. The most prevalent type of join is this one. When values in a field that is shared by both tables match, inner joins combine records from both tables.
Following are syntax for inner join SQL Query
SELECT column_name(s) FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name;
Following are the types of inner join in SQL.
Theta join
Natural join
EQUI join
Theta Join
A join that connects tables based on a relationship other than equality between two columns is called a theta join. Other than the "equal" operator, any operator could be used in a theta join.
Theta Join is used to join two tables based on some conditions. The condition can be on any attributes of the tables performing Theta join. Any comparison operator can be used in the condition. A ⋈θ B where θ is the condition for join.
Natural Join
A NATURAL JOIN is a JOIN operation that automatically generates an implicit join clause based on the columns that are shared by the two tables joining. Columns with the same name in both tables are called common columns. An inner, left-outer, or right-outer natural join are all possible. The INNER join is the default.
A natural join chooses rows from two tables whose values are the same in columns with the same name and type. The syntax NATURAL JOIN is used to request a natural join. A cross join is used if there are no similar columns. With a natural join, you should not use an ON clause.
Equi Join
The INNER JOIN is another name for it. When the join-predicate is used to combine the values of columns in two tables, such as table_a and table_b, a new result table is created by the EQUI JOIN.
In SQL, the EQUI JOIN executes a JOIN against a column of equality or the values of the matching column(s) that correspond to the associated tables. In our "where" clause, we use the equal sign (=) as a comparison operator to refer to equality in this case.
Inner Join Query
Let us take the standard database Adventureworks as our reference.
We will take the following tables as our reference.
The dataset looks like below
select * from [Sales].[SalesOrderHeader]
The output looks like below
select * from [Sales].[SalesOrderDetail]
The output looks like below
select * from [Sales].[Customer]
The output looks like below
The following query joins the SalesOrderHeader and SalesOrderDetail table.
select slordh.SalesOrderID, slordh.OrderDate, slordh.DueDate, slordd.OrderQty, slordd.ProductID, slordd.LineTotal from [Sales].[SalesOrderHeader] slordh inner join [Sales].[SalesOrderDetail] slordd on slordh.SalesOrderID=slordd.SalesOrderID
The output looks like below
The INNER JOIN keyword in SQL Query selects all rows from both tables as long as there is a match between the columns. If there are records in the "SalesOrderHeader" table that do not have matches in "SalesOrderDetail", they will not be shown!
The following SQL statement selects all orders with customer detail information:
select slordh.SalesOrderID, slordh.OrderDate, slordh.DueDate, slordd.OrderQty, slordd.ProductID, slordd.LineTotal, slordh.CustomerID, cust.AccountNumber from [Sales].[SalesOrderHeader] slordh inner join [Sales].[SalesOrderDetail] slordd on slordh.SalesOrderID=slordd.SalesOrderID inner join [Sales].[Customer] cust on slordh.CustomerID=cust.CustomerID
The output looks like below
The primary advantage of a join is its speed of execution. The user might not notice the improvement in performance. However, the database engine specifically names, indexes, and optimizes the columns, which means that the retrieval time will almost always be shorter than that of a subquery.
Due to the possibility of producing a number of tuples that are larger than the size of either table, joins typically cost a lot of money. However, unique tuples in other tables can be functionally determined by the join attributes of one table.
SQL Inner Join Performance
Following the best performance of inner join
SQL Testing Training
The above blog gives the you an insight about inner join. It details the definition of inner join, its purpose, advantages and disadvantages. It also gives a detail of the inner join types in sql. Learn about types of inner join in sql in deatil by checking out our comprehensive course on SQL.
What is Schema in SQL With Example: All You Need to Know
Data Definition Language (DDL) Commands in SQL
How To Create Database Table-All You Need To know
What does a Database Administrator do? A Detailed Study
Cyber Security
QA
Salesforce
Business Analyst
MS SQL Server
Data Science
DevOps
Hadoop
Python
Artificial Intelligence
Machine Learning
Tableau
Download Syllabus
Get Complete Course Syllabus
Enroll For Demo Class
It will take less than a minute
Tutorials
Interviews
You must be logged in to post a comment