Overview: Developing Your SQL Interview Skills
The Structured Query Language, or SQL, is a vital tool in the field of data administration and assessment. Because of this, being proficient in SQL is highly valued, and aspirants to work in data often have challenges when answering SQL interview questions. Being experienced in SQL is essential whether you want to work as a software engineer, database administrator, or data analyst. This blog post seeks to simplify complex join scenarios by offering clear explanations and useful examples to enable you to confidently take on these difficulties. The subtleties of inner, left, right, and full outer joins—particularly when working with null values and different data sets—will be the main topic of our discussion as we dig into certain SQL interview questions that frequently cause applicant confusion.
Learning Joins: The Basis of SQL Inquiries
Let’s quickly review the basic ideas behind SQL joins before getting into the specific problems. Rows from two or more tables can be combined using joins based on a shared column. To properly retrieve data from relational databases, one must understand the difference between inner, left, right, and full outer joins.
- Inner Join: Only rows that match in both tables are returned by an inner join.
- Left Join: All rows from the left table and the matching rows from the right table are returned by a left join, also known as a left outer join. NULL values are returned for the columns in the right table if there is no match.
- Right Join: All rows from the right table and the matching rows from the left table are returned by a right join, also known as a right outer join. The columns in the left table return NULL values if there is no match.
- Full Outer Join: All rows are returned by the full outer join when there is a match in either the left or right table.
These concepts are fundamental to answering many SQL interview questions effectively.
Question 1: Using Nulls to Evaluate different SQL Join Results
Below we have two tables i.e. table1 and table2 with only one column ‘id’.
Write SQL query to find the total number of record return in Inner Join, Left Join and Right Join and Full Join.
table1
id
1
1
1
2
2
3
null
table2
id
1
1
2
2
2
2
4
null
null
Inner Join : Number of Records returned by Inner Join
An inner join will return only the rows where the id
values match in both tables.
select count(*) as inner_join_records from table1
inner join table2 ON table1.id = table2.id;

- Matching id values are 1 and 2.
- 1 occurs 3 times in table1 and 2 times in table2, so 3 * 2 = 6 rows.
- 2 occurs 2 times in table1 and 4 times in table2, so 2 * 4 = 8 rows.
- Total rows: 6 + 8 = 14 rows.
- Null values from both table will be ignored.
Left Join : Number of Records returned by Left Join
A left join will return all rows from table1 and the matching rows from table2.
select count(*) as left_join_records from table1
left join table2 ON table1.id = table2.id;

- id 1 from table1 matches twice in table2: 3 * 2 = 6 rows.
- id 2 from table1 matches four times in table2: 2 * 4 = 8 rows.
- id 3 from table1 has no match in table2: 1 row with nulls from table2.
- null from table1 has no match in table2: 1 row with nulls from table2.
- Total rows: 6 + 8 + 1 + 1 = 16 rows.
Right Join : Number of Records returned by Right Join
A right join will return all rows from table2 and the matching rows from table1.
select count(*) as right_join_records from table1
right join table2 ON table1.id = table2.id;

RIGHT JOIN table2 ON table1.id = table2.id;
- · id 1 from table2 matches 3 times in table1: 2 * 3 = 6 rows.
- id 2 from table2 matches 2 times in table1: 4 * 2 = 8 rows.
- id 4 from table2 has no match in table1: 1 row with nulls from table1.
- null from table2 matches null from table1: 1 row.
- second null from table2 has no match in table1: 1 row with nulls from table1.
- Total rows: 6 + 8 + 1 + 1 +1 = 17 rows.
Full Outer Join : Number of Records returned by FULL OUTER Join
A full outer join returns all rows when there is a match in either table1 or table2.
select count(*) as full_outer_join_records from table1
FULL OUTER join table2 ON table1.id = table2.id;
- All rows from the left join and right join will be combined, removing duplicate rows.
- Total rows: 16+17-14= 19 rows.
Question 2: Records returned by different SQL joins if the table have more than One Column
We have given two tables i.e. Table A and Table B with two columns id and col1 in Table A and id and col2 in Table B.
Write SQL query to return number of records in INNER JOIN, LEFT JOIN and RIGHT JOIN. Join condition is based on id column i.e. A.id = B
Table A
id col1
1 val1
1 val2
1 val3
0 val4
0 val5
Table B
id col2
1 val1
1 val2
0 val3
Inner Join : Number of Records returned by Inner Join
select count(*) as inner_record
from tableA inner join tableB
ON tableA.id = tableB.id;

- id 1 occurs 3 times in Table A and 2 times in Table B: 3 * 2 = 6 rows.
- id 0 occurs 2 times in Table A and 1 time in Table B: 2 * 1 = 2 rows.
- Total rows: 6 + 2 = 8 rows.
Left Join : Number of Records returned by Left Join
select count(*) as left_record
from tableA left join tableB
ON tableA.id = tableB.id;
- id 1 from Table A matches twice in Table B: 3 * 2 = 6 rows.
- id 0 from Table A matches once in Table B: 2 * 1 = 2 rows.
- Total rows: 6 + 2 = 8 rows.
Right Join : Number of Records returned by Right Join
select count(*) as right_record
from tableA right join tableB
ON tableA.id = tableB.id;
- id 1 from Table B matches 3 times in Table A: 2 * 3 = 6 rows.
- id 0 from Table B matches 2 times in Table A: 1 * 2 = 2 rows.
- Total rows: 6 + 2 = 8 rows.
FAQ: SQL Interview Questions
Q: What is the difference between INNER JOIN and LEFT JOIN?
A: An INNER JOIN only returns the rows in both tables that match according to the join criterion. A LEFT JOIN, also known as an LEFT OUTER JOIN, yields all of the rows from the left table as well as the rows from the right table that match. For the columns in the right table, 1 NULL values are returned if there is no match.
Q: How do NULL values affect SQL joins?
A: NULL values can significantly impact join results. When using INNER JOIN, rows with NULL values in the join columns are generally excluded. In LEFT JOIN and RIGHT JOIN, NULL values in the non-matching table’s columns are returned when there’s no match. FULL OUTER JOIN will include all rows from both tables, with NULLs where there are no matches. It’s crucial to understand how your specific database system handles NULLs in joins.
Q: What is the purpose of a FULL OUTER JOIN?
A: A FULL OUTER JOIN is used to retrieve all rows from both tables, regardless of whether there’s a match. It’s useful when you need to see all data from both tables and identify any unmatched rows.
Q: How do I handle duplicate rows in SQL joins?
A: Duplicate rows can occur when there are multiple matching rows in one or both tables. You can use DISTINCT to remove duplicate rows from the result set, or you can use aggregation functions (e.g., COUNT, SUM, AVG) to group and summarize the data. Understanding the data and the desired result is key to handling duplicates effectively.
Q: What are common mistakes to avoid when writing SQL join queries in interviews?
A: Common mistakes include:
- Ignoring the ON clause, or join condition.
2. Using join types incorrectly (for example, using INNER JOIN when LEFT JOIN is required).
3. Not taking NULL values into consideration.
4. Inappropriate handling of duplicate rows.
5. Not comprehending the information and the desired result.
6. Failing to explain the reasoning behind the query.
Q: How can I improve my SQL join skills for interviews?
A: To improve your SQL join skills:
- Write various kinds of joins using a range of datasets to improve your SQL join skills.
2. Examine examples of complex join situations, such as ones that involve NULL values.
3. Test your abilities with online SQL practice tools.
4. Recognize how different join kinds affect performance.
When practicing, talk out to yourself about your thinking process.
Q: What is the difference between WHERE and ON clauses in SQL joins?
A: The ON clause is used to specify the join condition, which determines how rows from two tables are related. The WHERE clause is used to filter the result set after the join has been performed. It’s crucial to use the ON clause for join conditions and the WHERE clause for filtering results.
Q: Are there any performance considerations when using joins?
A: Yes, joins can impact query performance, especially with large datasets. INNER JOINs are generally more efficient than OUTER JOINs. Ensure you have proper indexes on join columns to speed up query execution. Avoid unnecessary joins and filter data as early as possible in the query.
Q: How to handle self joins?
A: Self joins are when a table is joined to itself. In these cases, you will need to alias the table to differentiate between the two instances of the table. Self joins are useful when you need to compare rows within the same table.
Q: What are cross joins?
A: Cross joins also known as cartesian products, return every possible combination of rows from the joined tables. If table A has x rows and table B has y rows, the cross join will return x * y rows. These are useful when you need to create all possible combinations of data, but should be used with caution as they can generate very large result sets.