Professional Documents
Culture Documents
Answers
Last updated on June 25, 2021
SQL Joins are one of the most commonly used concepts while working with
multiple tables. For this reason, interview questions based on SQL joins are
frequently asked during the interviews involving SQL.
In this article, we have compiled top SQL joins interview questions (starting with
the basic ones) that you should prepare for the interviews. So let’s begin.
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
EmployeeSalary. Now, we may look to join these two tables in order to get
useful information like fetching the name of the employee getting the highest
salary. Since the name is present in the EmployeeDetails table and salary is
present in EmployeeSalary, so, we will join these two tables using joins.
EmployeeDetails
121 P1 8000
321 P2 9000
421 P3 9500
EmployeeSalary
SELECT *
FROM EmployeeDetails INNERJOIN EmployeeSalary
ON EmployeeDetails.EmpId = EmployeeSalary.EmpId;
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Ques.2. Explain the different types of Joins in SQL?
Ans. Some of the major joins in SQL are-
Inner Join – Inner join is used to return the records which are having
matching values in both the tables.
Left Join – Left join is used to concatenate all the rows of the left table
and the matching rows in the right table.
Right Join-Right join is used to concatenate all the rows of the right table
and the matching rows in the left table.
Full Join-Full join is used to return all the records of both the tables as
long as there is a matching record in either table.
Self Join-Self join is a join that is used to join a table to itself. In a self-
join, a table is considered as if it were two tables.
Cartesian Join-Cartesian join is used to return the number of rows in the
first table multiplied by the number of rows in the second table. It is also
referred to as cross join.
Ques.4. What is the difference between Full Join and Cross Join?
Ans. A Full Outer Join is a combination of a Left Outer and Right Outer Join. It
returns all rows in both tables that match the query’s WHERE clause, and in
cases where the ON condition is not satisfied, it puts NULL values.
Whereas, a Cross join produces a cartesian product between the two tables,
returning all possible combinations of all rows.
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Ques.5. What is Natural Join?
Ans. Natural join is used to create an implicit join clause based on the value of
common attributes in the two tables. Common attributes are the attributes that
have the same name in both tables. Natural join does not need any comparison
operator as in the case of equi join.
Example – In the below two tables the Employee and Department tables have a
common field ‘EmpNo’. So, in the case of natural joins, we don’t need to
mention the On clause over the ‘EmpNo’ field of both the tables.
568 KD Seattle
897 PK Austin
215 PP Miami
Employees
5466 HR 897
5219 IT 215
8795 HK 568
Department
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
SELECT Employees.Name, Department.DName
FROM Department
Natural JOIN Employees
Syntax-
SELECT *
FROM TableName1
JOIN TableName2
ON TableName1.ColumnName = TableName2.ColumnName;
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
SELECT *
FROM TableName1
JOIN TableName2
ON TableName1.ColumnName [>,<,>=,<= ] TableName2.ColumnName;
Ques.8. What is the difference between cross join and natural join?
Ans. A cross join produces a cross product or cartesian product of two tables
whereas the natural join is based on all the columns having the same name and
data types in both the tables.
1. Join combines attributes of the rows present in the two tables that share
some common fields or attributes.
2. Join is applicable when the two tables have at least one common
attribute.
3. There are many types of joins like INNER JOIN, OUTER JOIN, LEFT
JOIN, RIGHT JOIN, etc.
4. The length of the resultant rows is more as compared to the length of
rows of the tables involved.
1. Join combines rows of the tables that are present in the query.
2. Join is applicable when the number of columns present in the query is the
same and the corresponding attributes have the same domain.
3. There are two types of a union like UNION and UNION ALL.
4. The number of the resultant rows is more as compared to the number of
rows present in each table involved in the query.
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Ques.10. Write the SQL command to join 3 tables.
Ans. Consider the below 3 tables. Now in order to fetch EmpName, DeptName,
Gender information of all the employees, we will join the 3 tables.
Table – Employees
DeptId DeptName
101 HR
102 IT
Table – Department
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
GenderId Gender
01 Male
02 Female
Table – Gender
This completes our article on top SQL joins interview questions. We hope, these
questions will help you in your interviews.
Kuldeep Rana
Kuldeep is the founder and lead author of ArtOfTesting. He is skilled in
test automation, performance testing, big data, and CI-CD. He brings
his decade of experience to his current role where he is dedicated to
educating the QA professionals. You can connect with him on LinkedIn.
Interview, SQL
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com