An inner join combines rows only when the joined tables have matching values. That makes it useful when the related record is required—but it can also hide a record that an API still needs to return. section 8.6 section 10.4
An invalid ACH file disappears
An API test posts an invalid ACH file, then requests the file list. The file is missing because the query inner-joins parsed file-header and file-control records. Exception handling can leave those parsed records unwritten, so the joins exclude the file. Changing those two joins to LEFT JOIN lets the file appear even when the parsed records are absent. section 8.6
Choose the join according to whether the related data must exist. Company details use LEFT JOIN for addresses and phone numbers: an incomplete or migrated company record may lack either, but the company should still be returned. Joining both optional tables can produce repeated company data, which the application then deduplicates. section 10.4
Listing 10.30 SQL query
SELECT art1.ach_files_id AS file_id,
art5.ach_records_type_5_id AS batch_header_id,
af.file_name AS filename,
COALESCE(c.name, abh.company_name, '') AS company_name, #1
abh.company_identification AS company_identification,
abcr.total_credit_entry_dollar_amount
AS total_credit_entry_dollar_amount,
abcr.total_debit_entry_dollar_amount
AS total_debit_entry_dollar_amount,
abcr.entry_addenda_count AS entry_addenda_count
FROM ach_files af
INNER JOIN ach_records_type_1 art1 USING ( ach_files_id )
INNER JOIN ach_records_type_5 art5 USING ( ach_records_type_1_id )
INNER JOIN ach_records_type_8 art8 USING ( ach_records_type_5_id )
INNER JOIN ach_batch_headers abh USING ( ach_records_type_5_id )
INNER JOIN ach_batch_control_records abcr USING ( ach_records_type_8_id )
LEFT JOIN companies c ON #2
abh.company_identification = c.ach_company_id #2
The batch-search query keeps inner joins for its ACH file and batch records. It uses a left join for company information because a batch may name a company that is not in the company table. section 10.5.1


