Altium: Rules and Query syntax
This is how I handle queries, and I find it to be pretty effective.

In your PCB window, right click on the component you want to identify with a query and right click and and select "Find similiar objects". 

This will allow you to narrow down what you are looking for and if you select "Create Expression" at the bottom, it will create an expression in the PCB Filter panel which you can then edit to refine or just copy and paste elsewhere like a rule.

The benefits of this method is that you can see if your query was correct. 

So right click your D7 and select Find similar objects and then make sure that ObjectKind is set to same, and that Name is also set to same.

Make sure the Create Expression is checked, and Mask is selected from the pulldown (this will highlight the results of your query so you can check if it was correct).

Just a note, if you use this method, the queries can be rather long sometimes, so it is good to review the Altium Query language because you can find some useful stuff in there.