designwithfocus

Data & databases

How to Use INNER JOIN So Only Matching Rows Stay

By Design With Focus Team 6 min read
Two small tables with arrows linking the matching rows

Short answer

INNER JOIN keeps rows that match in both tables and drops the rest. PostgreSQL's current docs say that for each row on the left, the result has a row for each right-hand row that satisfies the join condition. INNER is the default, so JOIN without a type is an inner join. A left, right, or full join is the form that adds unmatched rows with nulls. The worked rows later in this guide are an example, not a benchmark.

Last verified against the sources listed below.

Key takeaways

  • An inner join returns a combined row only when the ON condition is true for that pair.
  • In PostgreSQL, INNER is optional. A plain JOIN is an inner join.
  • LEFT, RIGHT, and FULL are outer joins. They keep unmatched rows and fill the other side with nulls.
  • ON takes a boolean expression. USING is the shortcut when both tables share the column name.
  • Qualify column names with the table. The tutorial calls that good style so a later duplicate name does not break the query.
  • The sample orders below are made up so you can see which keys survive. They are not a benchmark.

How to use inner join is to return only the pairs of rows that match in both tables. In the current PostgreSQL docs, which the site labels PostgreSQL 19 after a release note dated 19 November 2026, INNER JOIN means: for each row of the left table, keep a result row for each right-hand row that satisfies the condition.

INNER is optional and it is the default. A plain JOIN is an inner join. LEFT, RIGHT, and FULL are the outer forms, and they add unmatched rows filled with nulls.

What do you need before the query?

Two tables, a shared key, and a boolean condition you can write in ON.

A relational database stores the rows in tables. The join does not merge the tables on disk. It builds a result for that query.

You need permission to read both tables, and the columns in the condition need to be comparable. PostgreSQL’s tutorial says it is good style to qualify every column name, so a later duplicate name does not break the query.

The docs also show an older spelling: list both tables in FROM and put the test in WHERE. They say that form and JOIN ON return the same rows. JOIN is easier to read because the match is not mixed with other filters.

How do you write the statement?

One action per step. The rows in the example are made up. This is an example, not a benchmark.

  1. Name the columns you want in SELECT, with the table name in front of each one.
  2. Put the driving table after FROM.
  3. Write INNER JOIN, the second table, and ON with the match, such as orders.customer_id = customers.customer_id.
  4. Run the statement and check that keys with no partner are absent.
SELECT orders.order_id, customers.name
FROM orders
INNER JOIN customers
  ON orders.customer_id = customers.customer_id;
order_id customer_id item
1 10 mug
2 20 pen
3 99 lamp
customer_id name
10 Ada
20 Bo
30 Cy

The inner join keeps order 1 with Ada and order 2 with Bo. Order 3 drops out because customer 99 is not in customers, and Cy drops out because no order uses customer 30.

PostgreSQL’s own illustration, table t1 inner-joined to t2 on a shared num, keeps the matching numbers and drops the rest. The same page’s weather example drops Hayward because that city has no match.

Count the output before you trust it. Two tables of three rows do not imply three output rows, or six, or nine, because the length is the number of true pairs. In this example that number is two. If Ada had two orders, she would appear twice, once per order.

The PostgreSQL tutorial dates the explicit JOIN and ON spelling to SQL-92. The older comma form is still accepted, and the tutorial says the rows match. Prefer JOIN when you want the match beside the table names rather than inside a WHERE clause that also filters dates or status.

How does ON differ from USING?

ON is any boolean expression. USING is only for shared column names.

SQLite’s SELECT documentation says that with an ON clause, only rows for which the expression is true are included. PostgreSQL says the same: a pair matches when ON evaluates to true. USING (customer_id) is legal when both tables use that name. NATURAL INNER JOIN matches on every shared name, which is a wider net than a single key.

Microsoft’s FROM page describes INNER as returning all matching pairs and discarding unmatched rows from both tables, and it says this is the default when no join type is written. Oracle’s join page calls an inner join a simple join that returns only rows that satisfy the condition. The PostgreSQL spelling is the one to type against the current docs. The other two pages are a check that “matching rows only” is the usual meaning, not a PostgreSQL quirk.

What fails, and what do you change?

Most surprises are unmatched keys, duplicate matches, or an unqualified column name.

What you see What to change
A row you wanted is missing The condition was false for that row. Inner join drops it. Use a left join if the left row must remain, or fix the key.
More rows than you expected One left row matched many right rows. Check whether the key is unique on the side you assumed.
A column name is ambiguous Both tables have that name. Qualify it, as the tutorial shows with weather.city.
The result looks like every combination The condition is missing or always true. CROSS JOIN, and INNER JOIN ON TRUE, build that product.
USING will not parse The column name is not on both sides. Switch to ON and name each side.

Outer joins are the documented escape when you must keep non-matches. PostgreSQL says a left outer join starts with the inner join, then adds each unmatched left row with nulls on the right. That is a different result. Do not add those null rows if the report is supposed to list only real pairs.

An image compressor shrinks a picture in the browser. It does not run SQL. The join stays in the database, in the statement above.

Frequently asked questions

What does INNER JOIN keep?

It keeps pairs of rows that satisfy the join condition, and it discards rows that do not match. PostgreSQL's table-expression page says that for each row of the first table, the result includes a row for each row of the second table that meets the condition with it. Microsoft's FROM clause page says the same thing in shorter words: all matching pairs are returned, and unmatched rows from both tables are discarded. If a key exists on only one side, that row is not in an inner join result.

Is JOIN the same as INNER JOIN?

In PostgreSQL, yes. The words INNER and OUTER are optional. INNER is the default, and LEFT, RIGHT, and FULL imply an outer join. Writing JOIN without a type is an inner join. SQLite's SELECT page treats INNER JOIN and JOIN as the same family of operator when it explains ON and USING. Oracle's SQL reference calls an inner join a simple join and says it returns only the rows that satisfy the join condition. The keyword INNER is documentation for the reader, not a different operation.

When should you use ON instead of USING?

Use ON when the condition is any boolean expression, including columns with different names. PostgreSQL says a pair of rows matches when the ON expression evaluates to true. USING is the shorthand when both sides share a column name. NATURAL joins on every shared name, which widens if someone adds a column later. The tutorial also shows the older comma form with the test in WHERE, and it says those results match explicit JOIN.

Why did a row I expected disappear?

Because that row had no partner that made the condition true. The PostgreSQL tutorial shows this with a weather row for Hayward: there is no matching city, so the inner join ignores it. A left outer join puts that row back and leaves the city columns null. If the report must list every order, including one with an unknown customer, switch to an outer join or fix the key. The absence is the defined result, not a broken server.

Can one left row match many right rows?

Yes. The definition is per matching pair, not one output row per left row. If two customers somehow shared a key, or two order lines pointed at one product, both pairs would appear. That is why a result can be longer than either input. Duplicates are a data question: check whether the join column is unique on the side you thought was unique. Qualifying names, as in orders.customer_id, also prevents a later added column from making the query ambiguous.

Sources