Part - 12
Data Retrieval
Interview
Q A
..!
es th
ns
wi
tio
in qu
g
ar
Sh ter
oun
C
Krishan kumar
@Krishan kumar
How can you fetch
common records from
two tables?
What is an INNER JOIN?
An INNER JOIN is used in SQL to fetch records
that have matching values in both tables.
It returns only the rows where there is a match
between specified columns.
Why use INNER JOIN for this task?
Use of INNER JOIN
INNER JOIN is ideal for fetching common records
because it only includes rows that satisfy the
matching condition across both tables.
If there's no match, the rows are excluded from
the results.
Can you give an example of using it for better understanding?
Example Scenario:
Suppose we have two tables, TableA and TableB,
which both contain an ID column:
TableA:
ID Name
1 John
2 Alice
3 Bob
TableB:
ID Product
2 Laptop
3 Tablet
What are SQL query for this problem lets see.
SQL Query to Fetch Common
Records:
Result:
ID Name Product
2 Alice Laptop
3 Bob Tablet
Let’s provide us a Summary:
Summary:
INNER JOIN: Returns rows with matching
values in both tables based on the specified
condition.
Use Case: Ideal for identifying overlapping
data, like shared records between different
datasets.
for the jackpot o
ht f th
ig ed
Sw
ipe
r
😉
ay
wanna see some
Counter Questions
umar
krishan k
1. What happens if there are no
matching records?
If there are no matching values, an INNER JOIN
will return an empty result set.
This makes it different from LEFT JOIN or RIGHT
JOIN, which would still include non-matching
rows from one of the tables.
It is useful when you only need rows with exact
matches.
Next Question
2. Can we use multiple columns for
joining?
Yes, you can join tables using multiple columns
by specifying multiple conditions in the ON
clause.
For example,
ON TableA.ID = TableB.ID
AND TableA.Name = TableB.Name.
This helps to ensure more specific matches
between the tables.
ion
st
ue
tQ
x
Ne
3. How is INNER JOIN different from
OUTER JOIN?
An INNER JOIN returns only matching rows,
while an OUTER JOIN (like LEFT JOIN or
RIGHT JOIN) includes all rows from one table
and matching rows from the other.
OUTER JOIN helps in scenarios where you
want to keep all records from one table
regardless of matches.
ion
st
ue
t Q
x
Ne
4. Can we fetch common records
from more than two tables?
Yes, you can use multiple INNER JOIN clauses
to fetch common records from three or more
tables. Just add another INNER JOIN for each
additional table.
This is useful in complex queries where multiple
relationships exist.
re
mo
nts
wa
Ciku
5. What if the column names are
different in both tables?
You can still join the tables by specifying the
correct column names in the ON clause,
like:
ON TableA.ID_A = TableB.ID_B.
Using aliases for table names can also make the
query easier to read and manage.
you completed one interview question
with me,
can you do me a favour
Find This Useful
Time to hit that like button
and give it some love! 🤩
😉
Visit my Linkedin for such amazing Content
krishan kumar