INNER JOIN
INNER JOIN returns one row for every pair of rows that match the ON condition, and nothing for rows without a partner. It's the join you'll write most often. "Show each payment with its course title" is an inner join: every payment has a course, and you want them side by side. This lesson covers the syntax, the shorthand JOIN, joining on two columns at once, filtering and grouping joined rows, and the most common inner join bug: matching on only part of a key.
In One Minute
FROM a INNER JOIN b ON conditionkeeps the pairs where the condition is true.JOINalone meansINNER JOIN.- Join on every column of the key:
ON e.student_id = p.student_id AND e.course_id = p.course_id. - A partial key multiplies rows: payments joined to enrollments on
student_idalone gives 22 rows, not 7. - For inner joins, a condition in
ONor inWHEREgives the same result.
The Basics
SELECT columns
FROM table_a
INNER JOIN table_b ON table_a.column = table_b.column;Each payment with its course title:
SELECT p.payment_id, c.title, p.amount_usd
FROM payments AS p
INNER JOIN courses AS c ON c.course_id = p.course_id
ORDER BY p.payment_id; payment_id | title | amount_usd
------------+-------------------+------------
1 | Java Basics | 450.00
2 | Java OOP | 250.00
3 | Java OOP | 250.00
4 | Java Collections | 550.00
5 | SQL Basics | 200.00
6 | JavaScript Basics | 225.00
7 | JavaScript Basics | 225.00
(7 rows)Every payment found its course, so all seven appear. Git Basics has no payments, so it found no partner and doesn't appear. Java OOP and JavaScript Basics appear twice because each was paid in two installments.
JOIN on its own is an inner join, so this is the same query, and it's how most people write it:
SELECT p.payment_id, c.title, p.amount_usd
FROM payments AS p
JOIN courses AS c ON c.course_id = p.course_id
ORDER BY p.payment_id;Writing the ON Condition
ON can be any true-or-false condition, but it should say how the rows belong together, usually "this table's foreign key equals that table's primary key". The order of the two sides doesn't matter: c.course_id = p.course_id and p.course_id = c.course_id are the same. Table aliases (p, c) keep it short; without them you'd write the full table names:
SELECT payment_id, title
FROM payments
JOIN courses ON courses.course_id = payments.course_id;A column that exists in only one table, like title, can go unqualified, but a column in both, like course_id, must be qualified or PostgreSQL reports column reference "course_id" is ambiguous.
Joining on Two Columns
When the key has two columns, the ON condition must match both. An enrollment is identified by (student_id, course_id). To show each payment with the status of the enrollment it pays for:
SELECT p.payment_id, p.amount_usd, e.status
FROM payments AS p
JOIN enrollments AS e
ON e.student_id = p.student_id
AND e.course_id = p.course_id
ORDER BY p.payment_id; payment_id | amount_usd | status
------------+------------+-----------
1 | 450.00 | completed
2 | 250.00 | completed
3 | 250.00 | completed
4 | 550.00 | active
5 | 200.00 | active
6 | 225.00 | active
7 | 225.00 | active
(7 rows)Seven payments, seven rows: each payment met exactly one enrollment. Now watch what happens with only half the key:
SELECT count(*) AS rows
FROM payments AS p
JOIN enrollments AS e ON e.student_id = p.student_id; rows
------
22
(1 row)Twenty-two rows from seven payments. Matching on student_id alone pairs each of Srihitha's five payments with all four of her enrollments (20 rows), and each of Navika's two with her one enrollment (2 rows). The query runs without complaint; the numbers are just wrong, and any sum over them would be four times too big for Srihitha.
Filtering and Grouping Joined Rows
After the join, WHERE, GROUP BY and ORDER BY work on the combined rows as if they were one table. Check payments with their course, in date order:
SELECT p.payment_id, c.title, p.paid_on, p.amount_usd
FROM payments AS p
JOIN courses AS c ON c.course_id = p.course_id
WHERE p.method = 'check'
ORDER BY p.paid_on; payment_id | title | paid_on | amount_usd
------------+-------------------+------------+------------
3 | Java OOP | 2025-12-03 | 250.00
7 | JavaScript Basics | 2025-12-19 | 225.00
(2 rows)For an inner join, putting p.method = 'check' in the ON clause instead gives exactly the same rows; the difference only matters for outer joins, as LEFT JOIN shows. Keep join conditions in ON and filters in WHERE, and the query says what you mean.
Columns from both tables can be compared, too. Payments smaller than their course's fee, which are installments:
SELECT p.payment_id, c.title, p.amount_usd, c.fee_usd
FROM payments AS p
JOIN courses AS c ON c.course_id = p.course_id
WHERE p.amount_usd < c.fee_usd
ORDER BY p.payment_id; payment_id | title | amount_usd | fee_usd
------------+-------------------+------------+---------
2 | Java OOP | 250.00 | 500.00
3 | Java OOP | 250.00 | 500.00
5 | SQL Basics | 200.00 | 400.00
6 | JavaScript Basics | 225.00 | 450.00
7 | JavaScript Basics | 225.00 | 450.00
(5 rows)And grouping gives totals per course, labeled with the title instead of an id:
SELECT c.title, count(*) AS payments, sum(p.amount_usd) AS total
FROM payments AS p
JOIN courses AS c ON c.course_id = p.course_id
GROUP BY c.course_id, c.title
ORDER BY c.course_id; title | payments | total
-------------------+----------+--------
Java Basics | 1 | 450.00
Java OOP | 2 | 500.00
Java Collections | 1 | 550.00
SQL Basics | 1 | 200.00
JavaScript Basics | 2 | 450.00
(5 rows)Grouping by c.course_id as well as c.title keeps two courses with the same title apart, and lets PostgreSQL accept any other c column in the SELECT list (the primary-key rule from GROUP BY).
Common Mistakes
Joining on part of a key. Payments to enrollments needs both student_id and course_id.
Joining the wrong columns. ON c.course_id = p.payment_id runs and returns nonsense. Join foreign key to primary key.
Ambiguous columns. Qualify every column that exists in both tables.
Expecting unmatched rows. An inner join drops them. Use LEFT JOIN to keep them.
Interview Corner
What does INNER JOIN return?
One row for every pair of rows, one from each table, for which the ON condition is true. Rows with no match in the other table don't appear.
Is JOIN the same as INNER JOIN?
Yes. JOIN without LEFT, RIGHT, FULL or CROSS is an inner join.
Why might a join return more rows than the table you started from?
Because a row matched several rows on the other side, often because the ON condition uses only part of a composite key.
Practice
Challenge: For every payment, show the student's first name, the course title and the amount, sorted by payment_id. You'll need two joins: payments to students, and payments to courses.
Show solution
SELECT p.payment_id, s.first_name, c.title, p.amount_usd
FROM payments AS p
JOIN students AS s ON s.student_id = p.student_id
JOIN courses AS c ON c.course_id = p.course_id
ORDER BY p.payment_id; payment_id | first_name | title | amount_usd
------------+------------+-------------------+------------
1 | Srihitha | Java Basics | 450.00
2 | Srihitha | Java OOP | 250.00
3 | Srihitha | Java OOP | 250.00
4 | Srihitha | Java Collections | 550.00
5 | Srihitha | SQL Basics | 200.00
6 | Navika | JavaScript Basics | 225.00
7 | Navika | JavaScript Basics | 225.00
(7 rows)Each JOIN adds one table's columns. Seven payments in, seven rows out, because every payment has exactly one student and one course. Joining Many Tables goes further.
Key Takeaways
| You want to | Write |
|---|---|
| Payments with course titles | FROM payments p JOIN courses c ON c.course_id = p.course_id |
| Join on a two-column key | ON e.student_id = p.student_id AND e.course_id = p.course_id |
| Totals per course name | ... GROUP BY c.course_id, c.title |
Was this tutorial helpful?