Is there any other way other than writing an sql query with multiple tables?
To find master org we use oracle seeded oe_sys_parameters API Example:-
nvl(oe_sys_parameters.value(‘MASTER_ORGANIZATION_ID’,mo_global.get_current_org_id()),0)