InterviewSolution
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.
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 ManagerNameFROM Employees e JOIN Employees m ON e.ManagerId = m.Emp_ID Output:
|
|||||||||||||||||||||||||||||||||||||||||||||||||
| 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.
Here's the Employment table.
Here’s the EmpDetail.
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:
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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»
Example: Let's take a look at two tables. Here’s the Tb1_Employee table.
Here's the Tb2_Employment table.
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_DateFROM Tb1_Employee LEFT JOIN Tb2_Employment ON Tb1_Employee.Emp_ID=Tb2_Employment.Emp_ID; Output:
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_DateFROM Tb1_Employee RIGHT JOIN Tb2_Employment ON Tb1_Employee.Emp_ID=Tb2_Employment.Emp_ID; Output:
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6. |
State the difference between inner join and left join. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Answer»
Example: Let's take a look at two tables. Here’s the Tb1_Employee table.
Here's the Tb2_Employment table.
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_DateFROM Tb1_Employee INNER JOIN Tb2_Employment ON Tb1_Employee.Emp_ID=Tb2_Employment.Emp_ID; Output:
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_CountryFROM Tb1_Employee LEFT JOIN Tb2_Employment ON Tb1_Employee.Emp_ID=Tb2_Employment.Emp_ID; Output:
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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:
|
|
| 9. |
What is the importance of SQL joins in database management? |
|
Answer» SQL joins are important in database management for the following reasons:
|
|
| 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.
Here's the Employment table.
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:
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||