SQL Joins Interview Questions: Click Here
SQL Joins Interview Questions: Click Here
© Copyright by Interviewbit
Contents
In this article, we will cover the most common SQL Joins interview questions and
answers that have been grouped based on their difficulty level, both for freshers and
experienced candidates. To make sure that you become a master at answering these
questions, you should practice them repeatedly.
Content
101 Germany 2021-04-20
Writer
So ware
105 India 2022-01-03
Engineer
Development
108 Europe 2023-02-15
Executive
Marketing
109 Mexico 2020-05-23
Manager
Let us now join these two tables together using a SELECT statement, as shown below.
SELECT Emp_ID, Emp_Name, Emp_No, Emp_Profile, Emp_Country FROM Employee, Employment WHE
Output:
Ashish Content
101 9450425345 Ge
Kaktan Writer
Shantanu
104 9020330023 Data Analyst In
Khandelwal
Khanak So ware
105 8451004522 In
Desai Engineer
Inner Join: This method returns datasets that have the same values in both
tables.
Full Join: This is also referred to as a full outer join. It combines all rows from
the le table and the right table to create the result set. It means that a query
would return records from both tables, even if they had NULL values. If there are
no matching rows, the result-set (joined table) will display NULL values.
Right Join: This is also referred to as the Right outer join. All records from the
right table will be returned, as well as any records that match from the le table.
Le Join: This is also referred to as Le outer join. All records from the le table
will be returned, as well as any records that match from the right table.
Merge join produces a single output stream resulting from the joining of two sorted
datasets using an INNER, FULL, or LEFT join. It is the most effective of all the
operators for joining data. Specifically, merge join requires that both inputs be sorted
as well as matching meta-data in the joined columns. Users can't join columns of
different data types together. Users are not permitted to combine a column with a
numeric data type with a column with a character data type.
Content
101 Germany 2021-04-20
Writer
So ware
105 India 2022-01-03
Engineer
Development
108 Europe 2023-02-15
Executive
Marketing
109 Mexico 2020-05-23
Manager
Let’s perform INNER JOIN on these two tables using a SELECT statement, as shown
below:
Output:
Ashish Content
9450425345 Germany
Kaktan Writer
Shantanu
9020330023 Data Analyst India
Khandelwal
Khanak So ware
8451004522 India
Desai Engineer
Le Join: It returns datasets that have matching records in both tables (le and
right) plus non-matching rows from the le table. By using a le join, all the
records in the le table plus the matching records in the right table are returned.
Example: Let’s now perform LEFT JOIN on these two tables using a SELECT
statement, as shown below:
Output:
Ashish Content
9450425345 Germany
Kaktan Writer
Raj
8462309621 Null Null
Choudhary
Vivek
7512309034 Null Null
Oberoi
Shantanu
9020330023 Data Analyst India
Khandelwal
Khanak So ware
8451004522 India
Desai Engineer
Example: Let's take a look at two tables. Here’s the Tb1_Employee table.
Content
101 Germany 2021-04-20
Writer
So ware
105 India 2022-01-03
Engineer
Development
108 Europe 2023-02-15
Executive
Marketing
109 Mexico 2020-05-23
Manager
Let’s now perform LEFT JOIN on these two tables using a SELECT statement, as
shown below:
Output:
Ashish Content
9450425345 2021-04-20
Kaktan Writer
Raj
8462309621 Null Null
Choudhary
Vivek
7512309034 Null Null
Oberoi
Shantanu
9020330023 Data Analyst 2023-02-15
Khandelwal
Khanak So ware
8451004522 2020-05-23
Desai Engineer
Right Join: It returns datasets that have matching records in both tables (le
and right) plus non-matching rows from the right table. By using a le join, all
the records in the right table plus the matching records in the le table are
returned.
Example: Let’s now perform RIGHT JOIN on these two tables using a SELECT
statement, as shown below:
Output:
Ashish Content
9450425345 2021-04-20
Kaktan Writer
Shantanu
9020330023 Data Analyst 2022-12-11
Khandelwal
Khanak So ware
8451004522 2022-01-03
Desai Engineer
Development
Null Null 2023-02-15
Executive
Marketing
Null Null 2020-05-23
Manager
A JOIN is one of the mechanisms that we use to combine the data of more than one
table in a relational database, and a Nested Join is one of the simplest methods
involving the physical joining of two tables. In essence, a Nested Join uses one joining
table as an outer input table while the other one serves as an inner input table. With a
Nested Loop Join, one row from the outer table is retrieved and then the row is
searched for in the inner table; this process is repeated until all the output rows from
the outer table have been searched for in the inner table. Nested Loop Join may
further be sub-categorized into Indexed Nested, Naive Nested and Temporary Index
Nested Loop Join.
Development
109 [email protected]
Executive
Output:
Content
101 9450425345 Germany
Writer
So ware
105 8451004522 India
Engineer
Ashish Content
101 Germany
Kaktan Writer
Raj
104 Data Analyst India
Choudhary
Vivek So ware
105 India
Oberoi Engineer
Shantanu Development
108 Europe
Khandelwal Executive
Khanak Marketing
109 Mexico
Desai Manager
For instance, we might wish to display results that only include employees with their
managers. By using table aliases and a self-join, this can be accomplished easily.
Output:
Shantanu
104 Raj Choudhary
Khandelwal
Shantanu
108 Ashish Kaktan
Khandelwal
Mobile Table:
1 1 2022-03-24
2 3 2021-05-22
3 2 2022-01-05
4 1 2020-10-14
5 2 2021-08-29
A customer can own many mobile phones, but a mobile belongs to one customer
only. Therefore, we've defined the Foreign Key column (Customer_ID) in the Mobile
table allowing us to perform SELECT queries with JOIN clauses fairly easily.
Student_ID Student_Name
1 Asha Bisht
2 Rohit Sharma
3 Karan Tacker
Class Table
Class_ID Class_Name
1 Maths
2 Science
3 English
4 Physical Education
5 Computer Science
In this case, we cannot add the primary key of one table to that of another, or to both
of them, as this only stores a single relationship, while what we really need is multiple
relationships. Thus, we use a concept called a bridging table or joining table. The
joining tables are those that are placed between two other tables in a many-to-many
relationship and are intended to hold a record for each combination of the two other
tables. It may seem like quite a chunk of work, but the process is simple and provides
a much better data structure. In this case, we will create a new table called
ClassEnroll maintaining two columns, one for each of the primary keys of the other
table. Those columns store separate records for every class and student
combination.
ClassEnroll Table
Student_ID Class_ID
1 1
1 3
2 4
2 2
3 4
3 1
Emp_ID Emp_Name
1 Khushboo Ahuja
2 Kartik Sharma
3 Milli Desai
Employment
Emp_ID Emp_Profile
1 Content Writer
3 Marketing Manager
Output:
Khushboo
1 Content Writer
Ahuja
OR
OR
Employee Table
Emp_Name State_ID
Asha Bisht 1
Rohit Sharma 1
Karan Tacker 2
Karan Oberoi 3
Nikhil Bhardwawaj 3
State Table
State_ID State_Name
1 Madhya Pradesh
2 Bangalore
3 Uttarakhand
4 Rajasthan
Now, lets perform Equi-join using the equality operation and the WHERE clause as
follows;
Output:
Emp_Name State_Name
Union: In SQL, the term UNION is used to combine the results of more than one
SELECT statement. Union allows you to combine data into new rows vertically.
UNION requires that all queries have the same number of columns and order of
columns. Here is a visual representation of how a union looks. See how the
columns from Tables A and B have been combined to generate the result.
No, joins can have non-equi conditions as well. Join clauses can be used with
common comparison operators, such as <, <=, >, >=, !=, BETWEEN. For example,
listing records, listing unique pairs, and identifying duplicate records are just a few
situations where non-equi joins can prove to be useful.
In the case of two lists, one consisting of 4, 5, 6, and the other consisting of a, b, and c,
the Cartesian product between the two lists would be as follows:
4a, 5b, 6c
4a, 5b, 6c
4a, 5b, 6c
Full Join: This is also referred to as a full outer join. In a full outer join, all rows of
tables are combined to form a result set. It basically means that a query would
return a result set from both tables, even if they had NULL values. A result-set
(joined table) will display NULL values if both tables do not contain any
matching rows.
Syntax:
Generally, Common Table Expressions (CTEs) are temporary named result sets that
you can use to refer to within an UPDATE, INSERT, SELECT, or DELETE statement. The
CTEs can be defined by adding a WITH before an UPDATE, INSERT, DELETE, SELECT,
OR MERGE statement. Several CTEs may be used within the WITH clause, separated
by commas.
Conclusion
SQL joins are a simple concept that everyone should be able to grasp, even if writing
codes and handling them isn't hard. There is only one decision to make: which join to
use. What you need to know is what join you should use, which is what makes joining
so challenging at times. If you wish to become better at making such decisions, you
should gain as much experience as possible, following which you will be more
prepared to read and comprehend SQL Joins, understand data, and decide which
join type to use. In this article, you will find a number of frequently asked SQL Joins
interview questions and answers that will help you prepare convincing answers.
At the end of the day, you should know that the goal is to provide a concise, yet well-
illustrated answer that demonstrates your knowledge of the subject matter without
going on and on. The best approach would be for you to give an example of a use
case you have encountered while using this feature. Practice mock interviews and
solve SQL interview questions before your actual interview, as this will boost your
confidence and help you prepare for the different scenarios you may face. As
previously noted, they are not attempting to certify you, but rather gauge your level
of proficiency within your field. In that regard, bear in mind that you should act
confidently, professionally, and courteously during the interview. Remember,
interviews can be long and tiresome, so do not forget to smile!
Recommended Resources
https://www.interviewbit.com/sql-interview-questions/
https://www.interviewbit.com/blog/characteristics-of-sql/
https://www.interviewbit.com/blog/sql-projects/
https://www.interviewbit.com/sql-mcq/
Css Interview Questions Laravel Interview Questions Asp Net Interview Questions