Interviews are fun. Most of the time the interviewer has all the interesting questions, but I often come across a few individuals who stomps the interviewer. I was recently called by one of the organizations where I regularly help with their hiring process. They were stumped by interviewing candidates with one of the questions related to recent executed queries. Let us see the question and its answer.
Interviewer: How to find all the queries in SQL Server? Can you find it with the help of DMV?
Candidate: I do know if there is any DVM which stores all the queries executed in SQL Server.
Interviewer: Oh yes, there is one way. Here is the script:
SELECT dest.TEXT AS [Query], deqs.execution_count [Count], deqs.last_execution_time AS [Time] FROM sys.dm_exec_query_stats AS deqs CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest ORDER BY deqs.last_execution_time DESC
Candidate: Yes, I am aware of this script, but this script will only give you recent queries from the cached plan. There is no guarantee that it will be accurate. It is just an indication and helps to do a basic diagnosis of the system. If you remove the plan from the cache, you will not see that row as a result of your query.
This is where the interviewer got confused and gave me a quick call. I totally agree with the candidate that this query is a good start to do basic diagnosis, but it depends on cached plan and only displays recently executed query from cache query plans.
Reference: Pinal Dave (https://blog.sqlauthority.com)