Apologies, not sure why it is showing like this. I have removed all commented lines .
SELECT DISTINCT
WP.WebPartFileName,
NodeAliasPath,
PageTemplateCodeName
FROM CMS_WebPart WP
INNER JOIN
(
SELECT
PageTemplateID,
PageTemplateCodeName,
T.N.value('@type', 'varchar(50)') as WebPartName
CROSS APPLY (SELECT CAST(replace(PageTemplateWebParts,'&','amp;') AS XML)) as X(X)
CROSS APPLY X.X.nodes('/page/*/webpart') T(N)
) TemplateWebParts ON WP.WebPartName = TemplateWebParts.WebPartName
left outer JOIN View_CMS_Tree_Joined T ON T.NodeTemplateID = TemplateWebParts.PageTemplateID where T.NodeTemplateID is null
ORDER BY NodeAliasPath