A left outer join, written LEFT JOIN in these queries, keeps rows from the left side even when no matching row exists on the right. The missing right-side fields are null. Use it when a related record is useful but not guaranteed: an INNER JOIN would exclude the left-side row altogether. section 8.6 section 10.4
Keep an invalid ACH file visible
An invalid ACH file may have no parsed file header or control record. A file-list API originally used INNER JOIN for those records, so a test requesting the uploaded file received no result. Changing those two relationships to LEFT JOIN lets the file remain in the response without requiring parsed records to exist. section 8.6
Listing 10.24 Query to return company details
SELECT c.company_id, c.ach_company_id, c.name, c.tax_id_type,#1 c.tax_id_number, c.duns, c.logo, c.website, c.industry, #2 ca.company_address_id, ca.address_type, ca.address_type, #2 ca.address_line_1, ca.address_line_2, ca.address_line_3, #2 ca.address_line_4, ca.city, ca.state, #2 ca.zip_code, ca.zip_code_4, #2 cp.company_phone_id, cp.phone_type, #3 cp.phone_number, cp.extension #4 FROM companies AS c LEFT JOIN company_addresses AS ca USING (company_id) #4 LEFT JOIN company_phones AS cp USING (company_id) #5 WHERE company_id = %s #5
The company-detail query starts with companies and LEFT JOINs addresses and phone numbers. A company can therefore be returned even if either kind of contact information is absent. section 10.4
Keeping the company row does not make the response ready to send unchanged. Joined addresses and phone numbers can produce repeated company data across rows, so application logic collects distinct related records. PostgreSQL JSON aggregation offers another approach, at the cost of more complex, less portable SQL. section 10.4
The same choice helps batch searches retain results when a company's name is absent from the companies table. COALESCE then selects the stored company name, falls back to the batch header's name, and finally uses an empty string. section 10.5.1
Listing 10.47 Updates to get more exception details
SELECT
ae.ach_exceptions_id AS id,
af.created_at AS created_at,
ae.exception_code AS exception_code,
aec.exception_description AS description,
acr.unparsed_record AS unparsed_record,
COALESCE(c.name, abh.company_name, '') AS company_name, #1
aro.recovery_option AS recovery_option #1
FROM ach_exceptions AS ae
INNER JOIN ach_exception_codes AS aec USING (exception_code)
INNER JOIN ach_files AS af USING (ach_files_id)
INNER JOIN ach_combined_records AS acr
ON ae.ach_files_id = acr.ach_files_id
AND ae.record_number = acr.sequence_number
LEFT JOIN ach_recovery_options AS aro #2
USING (exception_code) #2
LEFT JOIN ach_batch_headers AS abh #2
USING(ach_records_type_5_id) #2
LEFT JOIN companies AS c #2
ON abh.company_identification = c.ach_company_id #2
WHERE ae.ach_files_id = %s
AND ae.ach_exceptions_id = %s
Exception details LEFT JOIN optional recovery options, batch headers, and companies. The response can retain the exception even when those details are unavailable; the interface can display “None Provided” for a missing recovery option. section 10.7.3





