SQL INNER JOIN: Matching Records Across Multiple Tables

The INNER JOIN is the most common join in database computing. It queries records from two or more tables and returns only the rows that satisfy the join condition in both tables. Any unmatched records on either side are completely omitted from the result set.


Standard INNER JOIN Syntax

SELECT a.column1, b.column2
FROM table_a a
INNER JOIN table_b b ON a.key = b.key;

Hands-on Query: Students & Department Names

-- Join students and departments on dept_id
SELECT 
    s.student_id,
    s.first_name,
    d.dept_name,
    s.gpa
FROM students s
INNER JOIN departments d ON s.dept_id = d.dept_id;
student_id first_name dept_name gpa
1 Aman Engineering 3.85
2 Pooja Business 3.92
3 Rohan Engineering 3.75
Notice What is Missing:
1. Vikram (student with NULL dept_id) is excluded because NULL matches nothing.
2. Arts department (dept_id 30 with no students) is excluded because no student references it.
To retain unmatched rows, we use OUTER JOINS!

Next, let us learn how to keep all rows from the primary table using SQL LEFT OUTER JOIN!

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!