How to override/implement a module's hook
You must create a custom module and add a custom hook function to it. The function name will be modulename_query_alter ().  Drupal maintains a list of all included hooks by searching the enabled modules for functions following that pattern.

Instructions for creating one are here.  This is the basic means for introducing custom behaviors to your site. The hook system is discussed here and you should look at using the hook_query_alter function here.

This will get you started and no doubt lead to further questions.