Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

How can you join a table to itself?

Answer»

Another type of join in SQL is a SELF JOIN, which connects a table to itself. In order to perform a self-join, it is necessary to have at least one column (say X) that serves as the primary key as well as one column (say Y) that contains values that can be matched with those in X. The value of Column Y may be null in some rows, and Column X need not have the exact same value as Column Y for every row.

Example: Consider the table Employees.

Emp_IDEmp_NameEmp_ProfileEmp_CountryManagerId
101Ashish KaktanContent WriterGermany104
104Raj ChoudharyData AnalystIndia108
105Vivek OberoiSoftware EngineerIndia101
108Shantanu KhandelwalDevelopment ExecutiveEurope101
109Khanak DesaiMarketing ManagerMexico 

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. 

SELECT e.Emp_ID, e.Emp_Name, m.FullName as ManagerName
FROM Employees e JOIN Employees m ON e.ManagerId = m.Emp_ID

Output:

Emp_IDEmp_NameManagerName
101Ashish KaktanRaj Choudhary
104Raj ChoudharyShantanu Khandelwal
105Vivek OberoiAshish Kaktan
108Shantanu KhandelwalAshish Kaktan
109Khanak DesaiNull

2.

Write an SQL query to join three tables.

Answer»

At times, you might need to retrieve data from three or more tables at once. A multi-table join requires consecutive JOIN operations: the first and second tables are joined to form a virtual table and then the third table is joined to this virtual table. Let's take a look at three tables. 

Here’s the Employee table.

Emp_IDEmp_NameEmp_No
101Ashish Kaktan9450425345
102Raj Choudhary8462309621
103Vivek Oberoi7512309034
104Shantanu Khandelwal9020330023
105Khanak Desai8451004522

Here's the Employment table. 

Emp_IDEmp_ProfileEmp_Email
101Content Writerashish@scaler.com
104Data Analystshantanu@scaler.com
105Software Engineerkhanak@scaler.com
109Development Executiveakshay@scaler.com
108Marketing Managernikita@scaler.com

Here’s the EmpDetail.

Emp_CountryEmp_EmailEmp_JoinDate
Germanyashish@scaler.com2021-04-20
Indiashantanu@scaler.com2022-12-11
Indiakhanak@scaler.com2022-01-03
Europeakshay@scaler.com2023-02-15
Mexiconikita@scaler.com2020-05-23
SELECT Emp_Name, Emp_No, Emp_Profile, Emp_Country, EmpJoinDate,
FROM Employee e INNER JOIN Employment m ON
e.Emp_ID = m.EMP_ID INNER JOIN EmpDetail d on
d.Emp_Email = m.Emp_Email;

Output:

Emp_NameEmp_NoEmp_ProfileEmp_CountryEmp_JoinDate
1019450425345Content WriterGermany2021-04-20
1049020330023Data AnalystIndia2022-12-11
1058451004522Software EngineerIndia2022-01-03

3.

Can you explain nested join in SQL?

Answer»

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. 


4.

What is a hash join in SQL?

Answer»

Just like any other join, the hash join requires two inputs, which are the probe input (inner table) and the build input (outer table). A hash join involves the use of a hash table to identify rows matching between two tables. The hash join is the option when no other join is preferred (possibly due to the absence of sorting or indexing etc). Hash joins are best when joining large data sets that are unsorted and non-indexed.


5.

State difference between left join and right join.

Answer»

  • Left Join: It returns datasets that have matching records in both tables (left and right) plus non-matching rows from the left table. By using a left join, all the records in the left table plus the matching records in the right table are returned.

Example: Let's take a look at two tables. Here’s the Tb1_Employee table.

Emp_IDEmp_NameEmp_No
101Ashish Kaktan9450425345
102Raj Choudhary8462309621
103Vivek Oberoi7512309034
104Shantanu Khandelwal9020330023
105Khanak Desai8451004522

Here's the Tb2_Employment table. 

Emp_IDEmp_ProfileEmp_CountryEmp_Join_Date
101Content WriterGermany2021-04-20
104Data AnalystIndia2022-12-11
105Software EngineerIndia2022-01-03
108Development ExecutiveEurope2023-02-15
109Marketing ManagerMexico2020-05-23

Let’s now perform LEFT JOIN on these two tables using a SELECT statement, as shown below: 

SELECT Tb1_Employee.Emp_Name, Tb1_Employee.Emp_No, Tb2_Employment.Emp_Profile, Tb2_Employment.Emp_Join_Date
FROM Tb1_Employee LEFT JOIN Tb2_Employment
ON Tb1_Employee.Emp_ID=Tb2_Employment.Emp_ID;

Output:

Emp_NameEmp_NoEmp_ProfileEmp_Join_Date
Ashish Kaktan9450425345Content Writer2021-04-20
Raj Choudhary8462309621NullNull
Vivek Oberoi7512309034NullNull
Shantanu Khandelwal9020330023Data Analyst2023-02-15
Khanak Desai8451004522Software Engineer2020-05-23

  • Right Join: It returns datasets that have matching records in both tables (left and right) plus non-matching rows from the right table. By using a left join, all the records in the right table plus the matching records in the left table are returned.

Example: Let’s now perform RIGHT JOIN on these two tables using a SELECT statement, as shown below: 

SELECT Tb1_Employee.Emp_Name, Tb1_Employee.Emp_No, Tb2_Employment.Emp_Profile, Tb2_Employment.Emp_Join_Date
FROM Tb1_Employee RIGHT JOIN Tb2_Employment
ON Tb1_Employee.Emp_ID=Tb2_Employment.Emp_ID;

Output:

Emp_NameEmp_NoEmp_ProfileEmp_Join_Date 
Ashish Kaktan9450425345Content Writer2021-04-20
Shantanu Khandelwal9020330023Data Analyst2022-12-11
Khanak Desai8451004522Software Engineer2022-01-03
NullNullDevelopment Executive2023-02-15
NullNullMarketing Manager2020-05-23

6.

State the difference between inner join and left join.

Answer»

  • Inner Join: This join generates datasets that contain matching records in both tables (left and right). By using an inner join, only the rows that match between each of the tables are returned; all non-matching rows are removed.

Example: Let's take a look at two tables. Here’s the Tb1_Employee table.

Emp_IDEmp_NameEmp_No
101Ashish Kaktan9450425345
102Raj Choudhary8462309621
103Vivek Oberoi7512309034
104Shantanu Khandelwal9020330023
105Khanak Desai8451004522

Here's the Tb2_Employment table.

Emp_IDEmp_ProfileEmp_CountryEmp_Join_Date
101Content WriterGermany2021-04-20
104Data AnalystIndia2022-12-11
105Software EngineerIndia2022-01-03
108Development ExecutiveEurope2023-02-15
109Marketing ManagerMexico2020-05-23

Let’s perform INNER JOIN on these two tables using a SELECT statement, as shown below: 

SELECT Emp_Name, Emp_No, Emp_Profile, Emp_Country, Emp_Join_Date
FROM Tb1_Employee INNER JOIN Tb2_Employment
ON Tb1_Employee.Emp_ID=Tb2_Employment.Emp_ID;

Output: 

Emp_NameEmp_NoEmp_ProfileEmp_CountryEmp_Join_Date
Ashish Kaktan9450425345Content WriterGermany2021-04-20
Shantanu Khandelwal9020330023Data AnalystIndia2022-12-11
Khanak Desai8451004522Software EngineerIndia2022-01-03

  • Left Join: It returns datasets that have matching records in both tables (left and right) plus non-matching rows from the left table. By using a left join, all the records in the left 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: 

SELECT Tb1_Employee.Emp_Name, Tb1_Employee.Emp_No, Tb2_Employment.Emp_Profile, Tb2_Employment.Emp_Country
FROM Tb1_Employee LEFT JOIN Tb2_Employment
ON Tb1_Employee.Emp_ID=Tb2_Employment.Emp_ID;

Output:

Emp_NameEmp_NoEmp_ProfileEmp_Country
Ashish Kaktan9450425345Content WriterGermany
Raj Choudhary8462309621NullNull
Vivek Oberoi7512309034NullNull
Shantanu Khandelwal9020330023Data AnalystIndia
Khanak Desai8451004522Software EngineerIndia

7.

Explain merge join in SQL.

Answer»

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.


8.

What are the different types of Joins in SQL?

Answer»

There are various types of join statements you can use depending on the use case you have. Specifically, there are four types of joins as follows:



  • 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 left 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 left table.


  • Left Join: This is also referred to as Left outer join. All records from the left table will be returned, as well as any records that match from the right table.


9.

What is the importance of SQL joins in database management?

Answer»

SQL joins are important in database management for the following reasons:


  • A method of stitching a database back together to make it easier to read and use.

  • Additionally, they maintain a normalized database. Data normalization helps us keep data redundancy low so that when we delete or update a record, we will have fewer data anomalies in our application.

  • Joins have the advantage of being faster, and as a result, are more efficient.

  • It is almost always faster to retrieve the data using a join query rather than one that uses a subquery.

  • By utilizing joins, it is possible to reduce the workload on the database. For example, instead of multiple queries, you can use one join query. So, you can better utilize the database's ability to search, filter, sort, etc.


10.

What are joins in SQL?

Answer»

A join clause is a SQL command used to combine records from multiple tables or retrieve data from these tables based on the existence of a common field (column) between them. A join condition and SELECT statement can be used to join the tables. Using the SQL JOIN clause, records can be fetched from two or more tables in a database and combined. In general, they are used when users need to retrieve data from tables that contain many-to-many or one-to-many relationships between them.

Example: Let's take a look at two tables. Here’s the Employee table.

Emp_IDEmp_NameEmp_No
101Ashish Kaktan9450425345
102Raj Choudhary8462309621
103Vivek Oberoi7512309034
104Shantanu Khandelwal9020330023
105Khanak Desai8451004522

Here's the Employment table. 

Emp_IDEmp_ProfileEmp_CountryEmp_Join_Date
101Content WriterGermany2021-04-20
104Data AnalystIndia2022-12-11
105Software EngineerIndia2022-01-03
108Development ExecutiveEurope2023-02-15
109Marketing ManagerMexico2020-05-23

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 WHERE  Employee.Emp_ID = Employment.Emp_ID;

Output:

Emp_IDEmp_NameEmp_NoEmp_ProfileEmp_Country 
101Ashish Kaktan9450425345Content WriterGermany
104Shantanu Khandelwal9020330023Data AnalystIndia
105Khanak Desai8451004522Software EngineerIndia