Outer Join

There are instances where information will need to be extracted from a joined set of data tables where keys do not match. The following example demonstrates an SQL query to find customers who have not placed a sales order this year.

An inner join would only return data on customers that have corresponding data in the Transactions table. However, in this scenario there will be no matching data in the Transactions table as no data exists.

The solution is an outer join query as this type of query will return all customer data, even if it doesn't match data in the Transactions table. Here a left join has been performed, but an extra clause has been added to only return customer data where the trans_cust_id is null. Thus picking up those companies that have not placed an order:


cust_namecust_fullnametrans_grn_widgets_salestrans_blu_widgets_sales
Colcon LtdMr Cecil Collins
Hamshaw LtdMiss Hannah Hamshaw
Immingham SuppliesMrs Irene Ingols
Jedson Manufacturing LtdMr Jamie Jedson
Kimbolton Carriers LtdMr Ken Kendrick
Lemmy's Wines and Spirits LtdMr Liam Lee
Mulgroon's Balloons LtdMiss Miranda Mulgroon
Norma's Cakes LtdMrs Norma Nixon
Oswald Furnishings LtdMr Oliver Oswald
Tyneside Machines LtdMr Tim Timpkins
Venga Coverings LtdMr Vinnie Venga
Xavier Coast to Coast LtdMrs Xena Xavier
Ambletons PLCMr Arnold Ambleton
Beckley Banners LtdMr Bertie Beckley
Cedric Collier LtdMr Cedric Collier
Duke Demolition LtdMr Derek Duke
Emmerson Enterprises LtdMr Eddie Emmerson
Fentham Farms LtdMiss Fiona Finley
Gurnock Garage LtdMr Garry Gumton
Henry Hammerton LtdMr Henry Hammerton
Local Landmarks LtdMrs Lena Lilly
Motorman LtdMr Matthew Marsh
Noreen Nightingale LtdMs Noreen Nightingale
Oakham Optometrists LtdMr Orville Owen
Purvis & Purvis LtdMr Paul Purvis
Quakerman's Quills LtdMr Quincy Quant
Renley of Roehampton LtdMr Roger Renley
Williams of Walsham LtdMr Wayne Williams
MySQL Code

SELECT c.cust_name, CONCAT(c.cust_salutation, ' ',c.cust_firstname,' ',c.cust_surname) AS cust_fullname,
t.trans_grn_widgets_sales, t.trans_blu_widgets_sales
FROM customer AS c
LEFT JOIN transactions AS t
ON c.cust_id = t.trans_cust_id
WHERE t.trans_cust_id IS NULL
T-SQL (MS SQL)

SELECT c.cust_name,
c.cust_salutation + ' ' + c.cust_firstname + ' ' + c.cust_surname AS cust_fullname,
t.trans_grn_widgets_sales, t.trans_blu_widgets_sales
FROM customer AS c
LEFT OUTER JOIN transactions AS t
ON c.cust_id = t.trans_cust_id
WHERE t.trans_cust_id IS NULL


Click the button below to advance to the next stage


Next: >> Excluding Data


Or use the above 'SQL Queries' dropdown menu to jump to any stage.