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.

State difference between Cross join and Natural Join.

Answer»


  • Natural Join: 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.


  • Cross Join: A Cross Join is also referred to as a Cartesian join or Cartesian product. The result set of a cross join is equal to all of the rows from the first table multiplied by all of the rows from the second table. It applies to all columns.

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/

  • https://www.interviewbit.com/blog/sql-books/


2.

Distinguish between nested subquery, correlated subquery, and join operation.

Answer»


  • Join Operation: A join operation is a binary operation that combines the data or rows from two or more tables based on a field they have in common. There are different types of joins, such as INNER JOIN, FULL JOIN, LEFT JOIN, RIGHT JOIN, etc.


  • Subquery: A query can be enclosed within another query, so the outer query is called the main query, and the inner query is called a subquery.


    • Nested Query: Nested queries execute the inner query first, and only once. An outer query is then executed based on the results of the inner query. The inner query is therefore used to execute the outer query.


    • Correlated Query: The outer query is executed first, and for every row of the outer query, the inner query is executed. Thus, values from the outer query are used in the inner query.




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»

  • Cross Join: A Cross Join is also referred to as a Cartesian join or Cartesian product. The result set of a cross join is equal to all of the rows from the first table multiplied by all of the rows from the second table. It applies to all columns.

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


  • 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:

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.


  • Join: Essentially, joins allow you to combine data into new columns horizontally. A join clause is an SQL command used in order to combine records from multiple tables or retrieve data from these tables based on the existence of a common field (column) between them. In JOIN, the columns of the joining tables may differ. Here is a visual representation of how a join looks. See how the columns from Tables A and B have been combined to generate the result.

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

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, TableName2
WHERE 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, Table2
WHERE Table1.ColumnName = Table2.ColumnName;

OR

SELECT column_list  FROM Table1, Table2
WHERE Table1.ColumnName = Table2.ColumnName;

OR

SELECT * FROM Table1 JOIN Table2 ON Table1.ColumnName = Table2.ColumnName;
Example: Consider the tables Employee and State.

Employee Table

Emp_NameState_ID
Asha Bisht1
Rohit Sharma1
Karan Tacker2
Karan Oberoi3
Nikhil Bhardwawaj3

State Table

State_IDState_Name
1Madhya Pradesh
2Bangalore
3Uttarakhand
4Rajasthan

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: 

Emp_NameState_Name
Asha BishtMadhya Pradesh
Rohit SharmaMadhya Pradesh
Karan TackerBangalore
Karan OberoiUttarakhand
Nikhil BharadwajUttarakhand

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

Emp_IDEmp_Name
1Khushboo Ahuja
2Kartik Sharma
3Milli Desai

Employment

Emp_IDEmp_Profile
1Content Writer
2Business Development Executive
3Marketing Manager

Now, consider the following query.

SELECT * FROM Employee NATURAL JOIN Employment;

Output:

Emp_IDEmp_NameEmp_Profile
1Khushboo AhujaContent Writer
2Kartik SharmaBusiness Development Executive
3Milli DesaiMarketing Manager

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

Student_IDStudent_Name
1Asha Bisht
2Rohit Sharma
3Karan Tacker

Class Table

Class_IDClass_Name
1Maths
2Science
3English
4Physical Education
5Computer 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_IDClass_ID
11
13
24
22
34
31

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:

Customer_IDFirst_NameLast_NameEmail
1SashaFranksashafrank@gmail.com
2IshaJoshiishajoshi@gmail.com
3Danish Khandanishkhan@gmail.com

Mobile Table:

Mobile_IDCustomer_IDOrderPlaced_Date
112022-03-24
232021-05-22
322022-01-05
412020-10-14
522021-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.