UNION Example — corresponding SQL

This SQL approximates the previous SPARQL query:

SELECT Products_0.partNo AS part,
Products_0.description AS descr,
U1.given,
U1.family,
U1.posn,U0.rma,
Addresses_1.city AS billCity,
Addresses_1.state AS billState,
U0.shipCity,
U0.shipState
FROM (
SELECT Orders_0.customer AS cust,
Orders_0.product AS prod,
NULL AS rma,
IF(Addresses_0.city IS NULL OR Addresses_0.state IS NULL, NULL, Addresses_0.city) AS shipCity,
IF(Addresses_0.city IS NULL OR Addresses_0.state IS NULL, NULL, Addresses_0.state) AS shipState
FROM Orders AS Orders_0
LEFT OUTER JOIN Addresses AS Addresses_0 ON Orders_0.shippingAddress=Addresses_0.id
WHERE Orders_0.customer IS NOT NULL AND Orders_0.product IS NOT NULL
UNION ALL
SELECT Returns_0.customer AS cust,
Returns_0.product AS prod,
Returns_0.RMAnumber AS rma,
NULL AS shipCity,
NULL AS shipState
FROM Returns AS Returns_0
WHERE Returns_0.customer IS NOT NULL AND Returns_0.product IS NOT NULL AND Returns_0.RMAnumber IS NOT NULL
) AS U0
INNER JOIN Products AS Products_0 ON U0.prod=Products_0.id
INNER JOIN (
SELECT Customers_0.id AS num,
Customers_0.givenName AS given,
Customers_0.familyName AS family,
NULL AS posn,
Customers_0.billingAddress AS addr
FROM Customers AS Customers_0
WHERE Customers_0.id IS NOT NULL AND Customers_0.givenName IS NOT NULL AND Customers_0.familyName IS NOT NULL AND Customers_0.billingAddress IS NOT NULL
UNION ALL
SELECT Employees_0.id AS num,
Employees_0.givenName AS given,
Employees_0.familyName AS family,
Employees_0.position AS posn,
Employees_0.homeAddress AS addr
FROM Employees AS Employees_0
WHERE Employees_0.id IS NOT NULL AND Employees_0.givenName IS NOT NULL AND Employees_0.familyName IS NOT NULL AND Employees_0.position IS NOT NULL AND Employees_0.homeAddress IS NOT NULL
) AS U1 ON U1.num=U0.cust
INNER JOIN Addresses AS Addresses_1 ON U1.addr=Addresses_1.id
WHERE Products_0.partNo IS NOT NULL AND Products_0.description IS NOT NULL AND Addresses_1.city IS NOT NULL AND Addresses_1.state IS NOT NULL;