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. |
State difference between Cross join and Natural Join. |
Answer»
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
|
|
| 2. |
Distinguish between nested subquery, correlated subquery, and join operation. |
Answer»
|
|
| 3. |
Explain CTE (Common Table Expression) SQL. |
|
Answer» 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. |
|
| 4. |
State difference between Full Join and Cross Join. |
Answer»
Syntax: SELECT * FROM Table_1 CROSS JOIN Table_2;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
Syntax: SELECT * FROM Table1 FULL OUTER JOIN Table2 ON Table1.column_name = Table2.column_name; |
|
| 5. |
Is it required that the Join condition be based on equality? |
|
Answer» 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. |
|
| 6. |
What makes a Union clause different from a Join clause? |
|
Answer» To combine data from two or more tables, you may use joins and UNIONS in your queries.
|
|
| 7. |
What is a Non-Equi Join? |
|
Answer» A Non-Equi join entails pulling data from multiple tables by using an INNER join. This type of join matches the columns of two tables based on an inequality using operators such as <, <=, >, >=, !=, BETWEEN, etc. Syntax: SELECT * FROM TableName1, TableName2WHERE TableName1.columnName [> | < | >= | <= | != | BETWEEN ] TableName2.columnName; |
|
| 8. |
What is an Equi Join? |
||||||||||||||||||||||||||||||||||
|
Answer» Equi Joins are a type of INNER Joins where a join is performed between two or more tables using a common column between them. Using the equality sign ( = ), it compares the data in two columns, if the data is the same, it retrieves it. Syntax: SELECT * FROM Table1, Table2WHERE Table1.ColumnName = Table2.ColumnName; OR SELECT column_list FROM Table1, Table2WHERE Table1.ColumnName = Table2.ColumnName; OR SELECT * FROM Table1 JOIN Table2 ON Table1.ColumnName = Table2.ColumnName;Example: Consider the tables Employee and State. Employee Table
State Table
Now, lets perform Equi-join using the equality operation and the WHERE clause as follows; SELECT Emp_Name, State_Name FROM Employee, State WHERE Employee.State_ID = State.State_ID;Output:
|
|||||||||||||||||||||||||||||||||||
| 9. |
Explain natural join. |
||||||||||||||||||||||||||||
|
Answer» Natural Joins are a type of join that combines tables based on columns that share the same name and have the same datatype. Ideally, there should be a common attribute (column) among two tables in order to perform a natural join. Syntax: SELECT * FROM TableName1 NATURAL JOIN TableName2;Example: Consider two tables Employee and Employment. Employee
Employment
Now, consider the following query. SELECT * FROM Employee NATURAL JOIN Employment;Output:
|
|||||||||||||||||||||||||||||
| 10. |
How should data be structured to support Join Operations in a many-to-many relationship? |
||||||||||||||||||||||||||||||||||
|
Answer» The many-to-many relationship is a bit more complex than the one-to-many relationship. Example: Consider a Student table and a Class table, both of which have many-to-many relationships: any student can be enrolled in many classes, and any class can have many students. It means an individual student may have many classes, and there may be many students in each class. Student Table
Class Table
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
|
|||||||||||||||||||||||||||||||||||
| 11. |
How should data be structured to support Join Operations in a one-to-many relationship? |
||||||||||||||||||||||||||||||||||
|
Answer» Among the most common types of database relationships are the ones involving one-to-many relationships. Consider the example of the Customer and Mobile table. Customer Table:
Mobile Table:
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. |
|||||||||||||||||||||||||||||||||||