What Is SQL INNER JOIN?

SQL INNER JOIN returns only rows where the join condition matches in both tables, giving the intersection of their data. Use it to combine related columns spread across normalized tables. The basic form is SELECT cols FROM table1 INNER JOIN table2 ON table1.key = table2.key; unmatched rows are excluded.

Learning
June 10, 2025
Galaxy Team
Sign up for the latest notes from our team!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
SQL INNER JOIN returns rows that satisfy a match in both tables, delivering their intersection. Use SELECT … FROM A INNER JOIN B ON A.key = B.key to combine related data while discarding non-matching rows.

What Is SQL INNER JOIN?

SQL INNER JOIN keeps only rows that satisfy the join condition in both tables, effectively intersecting their data.

How does SQL INNER JOIN work?

INNER JOIN pairs each row in the left table with each row in the right table and returns the set where the ON predicate evaluates to TRUE. Rows without matches in either table are omitted, producing an intersection.

What is the basic syntax of INNER JOIN?

The core pattern is:
SELECT columns
FROM table1 INNER JOIN table2
ON table1.key = table2.key;.
The ON clause defines the equality or comparison that links both tables.

When should I use INNER JOIN?

Use INNER JOIN when you need data that exists in both tables, such as linking customers to their orders, employees to departments, or products to inventory entries. It enforces referential integrity in the result.

How does INNER JOIN differ from LEFT JOIN?

INNER JOIN drops non-matching rows, while LEFT JOIN keeps all rows from the left table and fills missing matches with NULLs. Choose INNER JOIN for intersections and LEFT JOIN for preserving unmatched left-side data.

Can I join more than two tables?

Yes. Chain multiple INNER JOIN clauses: table1 JOIN table2 ON… JOIN table3 ON…. Each additional join further narrows the result to rows present in all tables.

How do I filter with WHERE after a JOIN?

Apply WHERE after the JOIN to restrict the already-joined rows. Example: …INNER JOIN orders o ON c.id = o.customer_id WHERE o.created_at > CURRENT_DATE - 30.

How do table aliases make joins clearer?

Aliases shorten table names, improve readability, and disambiguate columns. Example: FROM customers c INNER JOIN orders o ON c.id = o.customer_id.

How do I join on multiple columns?

List each comparison in the ON clause with AND. Example: ON a.year = b.year AND a.month = b.month AND a.id = b.id.

How do I avoid duplicate columns in the result?

Select only the needed columns or use the USING clause (for identical column names) and reference columns once. Alternatively, qualify or rename duplicated columns with aliases.

How does INNER JOIN affect query performance?

INNER JOIN can leverage indexes on the join keys, making it fast. Poorly indexed keys lead to hash or nested-loop joins that scan more rows. Create composite or single-column indexes matching your ON predicates.

Best practices for writing INNER JOIN queries

Always use explicit JOIN syntax, qualify columns, leverage indexes, avoid SELECT *, and document assumptions. Keep join predicates in ON, and filtering predicates in WHERE for clarity.

Key takeaways

INNER JOIN intersects tables on matching keys, keeps only mutual data, relies on ON predicates, benefits from indexes, and is ideal for normalized relational designs.

Frequently Asked Questions (FAQs)

Does INNER JOIN work the same in MySQL, PostgreSQL, and SQL Server?

Yes, the ANSI INNER JOIN syntax behaves consistently across major relational databases. Minor differences may exist in execution plans and optimizer hints.

Is INNER JOIN the default JOIN?

Many engines treat JOIN without a keyword as INNER JOIN, but writing INNER JOIN explicitly avoids confusion and aligns with style guides.

Can I join a table to itself?

Yes. Use a self-join with two aliases of the same table to compare rows, such as finding employees and their managers.

How do I speed up slow INNER JOINs?

Create indexes on join keys, avoid type mismatches, limit result columns, and filter early with WHERE clauses. Inspect execution plans for guidance.

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Check out our other posts!

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
Truvideo Logo