Ok, you can assign categories to a page on the Properties tab - categories tab and on the form tab. Both assignments are correct - it is just 2 different views on the same thing, i.e. if you change something on categories tab - it will be reflected on the form tab and vice versa.
You did assign on the form tab - it is fine! If you switch between form tab and properties -> categories - should see the same list of categories.
So what was said above about WHERE is correct, but I have an idea why it still showing all the categories:
The reason it is showing all categories because it does it globally. i.e. yes your document is not assigned to a given category but some other documents on your site are assigned to this category, so you need to get documents and categories from given part of your tree.
Here is the global query that tells how many document assigned to each category on all of the sites:
SELECT CMS_Category.CategoryID, CMS_Category.CategoryDisplayName, CMS_Category.CategoryName, count(DocumentID) as NumberOfAssignedDocuments
FROM CMS_Category INNER JOIN CMS_DocumentCategory ON CMS_Category.CategoryID = CMS_DocumentCategory.CategoryID
WHERE DocumentID IN (SELECT DocumentID FROM View_CMS_Tree_Joined)
GROUP BY CMS_Category.CategoryID, CMS_Category.CategoryDisplayName, CMS_Category.CategoryName
The condition that I mention above needs some tuning, i.e. you dont care about all document/categories on your site you care only about specific subset of your documents for example documents under /specialpages/ and categories for this subset of documents - lets restrict th categories:
(EXISTS(SELECT categoryid FROM cms_documentcategory as dc
JOIN View_CMS_Tree_Joined vw on dc.DocumentID = vw.DocumentID and NodeAliasPath like '/SpecialPages/%'
where dc.CategoryID = CMS_Category.CategoryID))