Query Territories Assigned to Account or Lead in Salesforce

Introduction

Sometimes it may beed needed to query Territories assigned to Lead or Account.

Solution
SELECT ObjectId, AssociationCause, SobjectType, Territory2Id FROM ObjectTerritory2Association 
Field NameDetails
AssociationCausePossible values are:
Territory2AssignmentRule—Territory assignment rule association
Territory2Manual—Manual association
ObjectId Lookup Refers ToAccount or Lead
SobjectTypeAccount or Lead
Territory2Id Territory Id