Write advanced Transact-SQL queries