move out sql queries from views