Software Testing Class

SQL JOIN in Database Testing

sql join queries - learn database testing

The JOIN is very much misunderstood term for beginners while learning the SQL commands. In the interview, at least one question is to ask about the SQL JOIN. So in this article, I am trying to simplify the things for new SQL learners & make it easy to understand the SQL JOIN. At the end of this post, you should in a position to write your SQL JOIN query independently. So first start from the basics of JOIN.

What is SQL JOIN?

At the expert level, JOIN is more common SQL commands used in day to day life. JOIN is used to retrieving the records from multiple tables. SQL allows you to take JOIN on multiple tables from the same database as well as different databases from the same server.

Basically the tables are interrelated with each other using Primary & foreign keys. So these keys are used in the JOIN to interlink two tables.

Why should we use JOIN?

Many times you are thinking “Why to use SQL JOIN” as the same task can be done using different queries. In the database, queries are executed one by one & the result of the successive query can be used for the next query. If we use the JOIN queries then instead of processing multiple queries SQL server process only a single query which reduces the SQL server overhead. The main advantage of SQL JOIN is improved performance. Also using multiple queries lead to more data transfer from SQL server to the application which reduces the performance.

Types of SQL JOIN

Before we start learning SQL JOIN, we will see the list of different types of SQL JOIN:

Let’s go ahead & learn different types of JOIN queries with a practical examples. The following are the three tables, Customers Product & Order.

sql join 3 tables

SQL INNER JOIN Keyword

The INNER JOIN selects all rows from both tables as SQL query matches the specified condition.

SQL INNER JOIN Syntax

SELECT column_name(s)
FROM Table1
JOIN Table2
ON Table1.column_name=Table2.column_name;

or

SELECT column_name(s)
FROM Table1
INNER JOIN Table2
ON Table1.column_name=Table2.column_name;

In the query you can use JOIN or INNER JOIN, both are the same.

INNER JOIN Query Example

Suppose, the dealer wants the list of order details like Product name, Unit Price, Quantity & Price. So, in this case, you can use INNER JOIN to get the records from both Product & Order tables. The ProductId is common in both tables. So in the INNER JOIN condition is added on ProductId & only matching records from Product & Order tables are returned.

SELECT T2.OrderID, T1.ProductID, T1.Name, T1.UnitPrice, T2.Quantity, T2.Price
FROM Product AS T1
INNER JOIN Order AS T2 ON T1.ProductID = T2.ProductID
ORDER BY T2.OrderID

Following is the result upon executing the above SQL INNER JOIN query:

SQL LEFT JOIN Keyword

LEFT JOIN returns all records/rows from the left table and from right table returns only matched records. Where no matches have been found in the table on the right, NULL is returned.

SQL LEFT JOIN Syntax

SELECT column_name(s)
FROM Table1
LEFT JOIN Table2
ON Table1.column_name=Table2.column_name;

or

SELECT column_name(s)
FROM Table1
LEFT OUTER JOIN Table2
ON Table1.column_name=Table2.column_name;

LEFT JOIN query Example

SELECT T2.OrderID AS OrderID, T1.ProductID, T1.Name, T1.UnitPrice, T2.Quantity AS Quantity, T2.Price AS Price
FROM Product AS T1
LEFT OUTER JOIN Order AS T2 ON T1.ProductID = T2.ProductID
ORDER BY T1.ProductID

Following is the result upon executing the above SQL LEFT OUTER JOIN query. You can see all records from the Product table & NULL values are displayed for all records where no matching records found in the Order table.

SQL RIGHT JOIN Keyword

RIGHT JOIN is the exact opposite to LEFT JOIN, it returns all records/rows from the right table and from left table returns only matched records. Where no matches have been found in the table on the left, NULL is returned.

SQL RIGHT JOIN Syntax

SELECT column_name(s)
FROM Table1
RIGHT JOIN Table2
ON Table1.column_name=Table2.column_name;

or

SELECT column_name(s)
FROM Table1
RIGHT OUTER JOIN Table2
ON Table1.column_name=Table2.column_name;

RIGHT JOIN query Example

SELECT T2.OrderID AS OrderID, T1.ProductID, T1.Name, T1.UnitPrice, T2.Quantity AS Quantity, T2.Price AS Price
FROM Product AS T1
RIGHT OUTER JOIN Order AS T2 ON T1.ProductID = T2.ProductID
ORDER BY T1.ProductID

Following is the result upon executing the above SQL RIGHT JOIN query. You can see all records from the Order table & NULL values are displayed for all records where no matching records found in the Product table. But if all records are matched then it will left table then no NULL records are returned in result set.

SQL FULL OUTER JOIN Keyword

FULL OUTER JOIN combines left outer join and right outer join. This join returns all records/rows from both the tables. If there are no columns matching in both tables, it returns NULL values.

SQL FULL OUTER JOIN Syntax

SELECT column_name(s)
FROM Table1
FULL OUTER JOIN Table2
ON Table1.column_name=Table2.column_name;

FULL OUTER JOIN query Example

Suppose we want to get all Order records against all the Product records, then we can use the FULL OUTER JOIN script shown below to get all records from both tables.

SELECT T2.OrderID AS OrderID, T1.ProductID, T1.Name, T1.UnitPrice, T2.Quantity AS Quantity, T2.Price AS Price
FROM Product AS T1
FULL OUTER JOIN Order AS T2 ON T1.ProductID = T2.ProductID
ORDER BY T1.ProductID

Following is the result upon executing the above SQL FULL OUTER JOIN query:

Conclusion on SQL JOIN


⇓ Subscribe Us ⇓


If you are not regular reader of this website then highly recommends you to Sign up for our free email newsletter!! Sign up just providing your email address below:


 

Check email in your inbox for confirmation to get latest updates Software Testing for free.


  Happy Testing!!!
 
Exit mobile version