Capital gains tax set off

If a flat is mortgaged or is a collateral for an educational loan and once the flat is disposed off, the following clarifications are required:

Whether the educational loan repaid can be set off against the capital gains on the sale of the flat.

If yes kindly intimate the relevant section of the IT Act/IT Rules or any case laws?

If yes, then whether the Capital Gains then has to be paid on the differential amount only?

A related query is: in case the proceeds on sale of flat are to avoid capital gains tax, whether the whole proceeds are to be invested in NHAI/REC capital gains bonds or amount only to the extent of capital gains on indexation is to be invested in capital gain bonds to avoid capital gains?

A reply along with an illustration regarding capital gains calculation and amount to be invested in Capital Gain bonds.