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

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

A SQL join combines rows from related tables using a matching condition. Choose INNER JOIN when you want only matching pairs; use an outer join when rows without a match must remain. The join condition and filter placement determine which records survive.

How SQL joins combine tables

A join compares rows from two inputs using a condition, usually a relationship between key columns. For example, a customer table may store each customer once, while an orders table stores that customer’s orders. Joining on the customer identifier lets a query return customer and order details together.

A join returns a row for each pair that satisfies its condition. If a key appears more than once on either side, it can produce multiple result rows; joins do not automatically deduplicate records. PostgreSQL 17 documents these join semantics and syntax in its table expressions reference.

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

Which join should you use?

Join type Rows retained Example question
INNER JOIN Only row pairs that match the condition. Which orders have a matching customer?
LEFT JOIN Every row from the left input, plus matching right-side rows. Right-side columns are NULL when there is no match. Which customers have orders, and which have none?
RIGHT JOIN Every row from the right input, plus matching left-side rows. Left-side columns are NULL when there is no match. Which entries in a right-side reference list lack a left-side match?
FULL OUTER JOIN Matching pairs and unmatched rows from both inputs. Which records appear in either source, including unmatched records?
CROSS JOIN Every possible pair of rows from the two inputs. What are all combinations of products and sizes?

In PostgreSQL, INNER is the default join type, and OUTER is optional in LEFT OUTER JOIN, RIGHT OUTER JOIN, and FULL OUTER JOIN. A cross join of inputs with N and M rows produces N × M rows, so its result can grow quickly.

#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.

How to join two tables with an example

This query returns every customer and any orders associated with them:

SELECT c.customer_id, c.customer_name, o.order_id
FROM customers AS c
LEFT JOIN orders AS o
  ON o.customer_id = c.customer_id;

The ON condition matches an order’s customer key to the customer identifier. Because the query uses LEFT JOIN, a customer remains in the result even if no order matches; the selected order field is then NULL. Replacing it with INNER JOIN would exclude customers without matching orders.

Aliases such as c and o make the query easier to scan. Qualifying selected columns with their table alias also avoids ambiguity when both tables have a column with the same name. Microsoft Learn recommends specifying join conditions in the FROM clause and using aliases and qualified names for clarity in its SQL Server joins guidance.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #2
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.

Where to put conditions: ON versus WHERE

ON determines which right-side rows count as matches. WHERE filters the result after the join. With an outer join, that distinction can determine whether unmatched left-side rows survive.

Keep left-side rows while matching only qualifying right-side rows

SELECT *
FROM t1
LEFT JOIN t2
  ON t1.num = t2.num
 AND t2.value = 'xxx';

This retains each t1 row. A t2 row is attached only when both conditions match; otherwise the t2 columns are NULL.

Filter the joined result afterward

SELECT *
FROM t1
LEFT JOIN t2
  ON t1.num = t2.num
WHERE t2.value = 'xxx';

The WHERE condition rejects rows where t2.value is NULL, including the NULL-extended rows created for unmatched t1 records. As a result, this query does not retain those unmatched rows. PostgreSQL explains the distinction between join conditions and later filters in its table expressions documentation.

Rank #3
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.

Find rows with no match

To find left-side records without a corresponding right-side record, use a left join and test a right-side key that cannot be NULL for a real match:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
SELECT t1.*
FROM t1
LEFT JOIN t2
  ON t2.num = t1.num
WHERE t2.num IS NULL;

This is often called an anti-match pattern. Choose a right-side column that is guaranteed non-NULL for matching records; testing a nullable column could misclassify a matched row as unmatched.

Choose the join condition deliberately

Use ON for explicit relationships

ON is the general way to state the relationship between tables, and it is the clearest choice when teaching or reviewing a query:

Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray
  • Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft
ON orders.customer_id = customers.customer_id

Use USING when the shared key has the same name

USING (customer_id) is a concise alternative when both inputs have a join column with the same name. PostgreSQL documents it as shorthand for equality comparisons on the listed columns. Check your database’s syntax and output-column behavior before using it in portable queries.

Avoid relying on NATURAL JOIN in changing schemas

NATURAL JOIN infers its condition from every column name shared by the two inputs. If a schema change adds another same-named column, the join can start matching on that column too, without the query text changing. Explicit ON conditions make the intended relationship visible.

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.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Check keys and row counts before deduplicating

If a join returns more rows than expected, first check whether the join key is unique on each side and what relationship the data actually represents:

Best Value
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.
  • One-to-one: each key occurs at most once in both inputs, so a match produces at most one pair.
  • One-to-many: a key occurs once on one side and multiple times on the other, producing one result row for each matching row on the many side.
  • Many-to-many: a key occurs multiple times on both sides, producing every matching combination.

Compare row counts before and after the join and inspect repeated keys. Do not apply DISTINCT simply to hide unexpected multiplication: it may conceal a wrong condition or discard legitimate combinations.

Check syntax for your database

Join support and syntax are not identical across SQL dialects. PostgreSQL 17 documents inner, left, right, full, and cross joins, along with ON and USING. The MySQL 8.4 JOIN Clause reference lists inner/cross, left, and right forms but does not list FULL OUTER JOIN. Do not assume that syntax will work in MySQL; consult the manual for the specific engine and version you use.

Microsoft documents inner, left, right, full outer, and cross logical joins for SQL Server in its joins reference. That documentation distinguishes the logical join expressed in SQL from the physical operation used to execute it.

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

Understand performance through the execution plan

The join keyword describes the result you want; it does not specify a single execution algorithm. Microsoft Learn states: “SQL Server implements both logical join operations (defined by Transact-SQL syntax) and physical join operations (the actual algorithms used to execute the joins).” Its SQL Server documentation describes nested loops, merge, and hash joins, as well as adaptive joins for SQL Server 2017 and later. The optimizer selects an approach using factors such as table size, available indexes, and data distribution.

There is no reliable rule that LEFT JOIN is inherently slower than INNER JOIN, or that one physical algorithm is always best. Review the execution plan for the database and version you run, and test with representative data. Row counts, indexes, data distribution, and engine version all affect the result.

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.