October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

SQL Joins, Quickly Explained: Match Rows Without Losing the Ones You Need

A practical SQL joins refresher: choose the right join type, write a clear match condition, and avoid surprises from duplicate matches or outer-join filters.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A SQL join combines rows from two table expressions by matching them against a condition. Choose the join type by deciding which unmatched rows should remain: only matches, unmatched rows from one side, unmatched rows from both sides, or every possible pairing.

How a join matches rows

A join creates output rows from rows in two inputs. Its join condition determines which pairs count as matches. For example, a city table and a weather table might be joined by comparing the city name in one table with the city field in the other.

As an Amazon Associate I earn from qualifying purchases.

In PostgreSQL, the join condition is commonly written with ON. Give columns their table names or aliases when both inputs have columns with the same name, such as id; this makes it clear which column the query uses. PostgreSQL’s join tutorial introduces joins and aliases.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Which join type keeps the rows you need?

Join type Rows returned
INNER JOIN Only row pairs that satisfy the join condition.
LEFT JOIN or LEFT OUTER JOIN Matching pairs and every row from the left input. For a left row without a match, right-side columns are NULL.
RIGHT JOIN or RIGHT OUTER JOIN Matching pairs and every row from the right input. For a right row without a match, left-side columns are NULL. You can express the same preservation by swapping the inputs and using a left join.
FULL JOIN or FULL OUTER JOIN Matching pairs and unmatched rows from both inputs, with NULL in columns from the side with no match.
CROSS JOIN Every possible pair of input rows. With N rows on one side and M on the other, it produces N × M pairs.

These definitions follow the PostgreSQL SELECT reference and PostgreSQL 13 table-expression reference. SQL implementations can differ, so check the documentation for your database when portability matters.

Choose the condition syntax deliberately

Use ON for an explicit match rule

ON accepts a Boolean expression describing when rows match. It is the clearest choice when the related columns have different names or when the relationship is more specific than equality between same-named keys.

SELECT city.name, weather.temperature
FROM city
JOIN weather ON weather.city = city.name;

Use USING for a shared equality key

When both inputs have a same-named column that should match by equality, USING (key) is a concise alternative. PostgreSQL returns the listed join column once, rather than showing one copy from each input.

SELECT *
FROM orders
JOIN customers USING (customer_id);

Be cautious with NATURAL

NATURAL JOIN matches on every column name shared by the two inputs. That can make a query’s behavior change if a schema later gains another same-named column. Prefer an explicit ON or USING condition when the intended relationship should be visible and stable.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Understand row counts and outer-join filters

One row can produce several matches

A join does not automatically deduplicate. If one row on the left matches three rows on the right, it produces three joined rows. Check whether the join key is unique on the side you expect to match once; otherwise, the additional pairs may be correct or may reveal a data or query assumption to investigate.

A filter can remove rows an outer join preserved

An outer join first determines matches using its join condition and supplies NULL values for the unmatched side. A later WHERE condition on a right-side column can reject those null-extended rows, making the result behave like an inner join for that condition. PostgreSQL documents the distinction between the join condition and conditions applied afterward in its SELECT reference.

A cross join multiplies the inputs

Use CROSS JOIN only when all combinations are intended. Its output count is the product of the input row counts, so even moderate inputs can produce a large result.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Join a table to itself with aliases

A self-join uses one table twice in different roles. Aliases distinguish those roles—for example, an employee row and that employee’s manager in an employee table.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
SELECT staff.name AS employee, manager.name AS manager
FROM employee AS staff
LEFT JOIN employee AS manager
  ON staff.manager_id = manager.id;

Here, staff and manager refer to separate instances of the same table. A left join keeps employees even when no manager row matches.

A practical check before relying on a join

  • Confirm which unmatched rows must remain, then choose the join type.
  • Make the intended relationship explicit with ON, or use USING for a deliberate same-named equality key.
  • Check whether the match key is unique where you expect one-to-one matches; multiple matches multiply result rows.
  • For an outer join, inspect later WHERE conditions that refer to the nullable side.
  • Qualify shared column names with aliases, and avoid implicit matching when a schema change could alter the common columns.
  • Verify syntax and behavior against your database’s documentation; the examples and cited semantics here are PostgreSQL-based.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.