SQL Server’s Query Store captures all executed queries (or a subset based on your capture mode) along with their execution statistics. Viewing the complete list of captured queries helps you understand what’s been tracked and provides a starting point for performance analysis.
To view all queries captured in Query Store, query the sys.query_store_query and sys.query_store_query_text views:
SELECT
q.query_id,
qt.query_sql_text,
q.object_id,
OBJECT_NAME(q.object_id) AS object_name,
q.initial_compile_start_time,
q.last_compile_start_time,
q.last_execution_time
FROM sys.query_store_query q
JOIN sys.query_store_query_text qt ON q.query_text_id = qt.query_text_id
ORDER BY q.last_execution_time DESC;
This returns all queries with their IDs, SQL text, associated database objects, and timing information, sorted by most recent execution.
Filtering to Recent Queries
To see only queries executed within a specific timeframe:
SELECT
q.query_id,
qt.query_sql_text,
q.last_execution_time
FROM sys.query_store_query q
JOIN sys.query_store_query_text qt ON q.query_text_id = qt.query_text_id
WHERE q.last_execution_time >= DATEADD(day, -7, GETUTCDATE())
ORDER BY q.last_execution_time DESC;
This shows queries executed in the last 7 days.
Viewing Queries from Database Objects
To see only queries that originate from stored procedures, functions, or triggers:
SELECT
q.query_id,
qt.query_sql_text,
OBJECT_NAME(q.object_id) AS object_name,
q.last_execution_time
FROM sys.query_store_query q
JOIN sys.query_store_query_text qt ON q.query_text_id = qt.query_text_id
WHERE q.object_id > 0
ORDER BY q.last_execution_time DESC;
When object_id is greater than 0, the query came from a database object like a stored procedure, function, or trigger rather than being executed as ad-hoc SQL.
Viewing Ad-Hoc Queries Only
To see only ad-hoc queries (not from stored procedures, functions, or triggers):
SELECT
q.query_id,
qt.query_sql_text,
q.last_execution_time
FROM sys.query_store_query q
JOIN sys.query_store_query_text qt ON q.query_text_id = qt.query_text_id
WHERE q.object_id = 0
ORDER BY q.last_execution_time DESC;
The q.object_id = 0 bit filters to queries executed directly as SQL statements rather than from within database objects.
Including Execution Counts
To see how many times each query has been executed:
SELECT
q.query_id,
qt.query_sql_text,
q.last_execution_time,
SUM(rs.count_executions) AS total_executions
FROM sys.query_store_query q
JOIN sys.query_store_query_text qt ON q.query_text_id = qt.query_text_id
JOIN sys.query_store_plan p ON q.query_id = p.query_id
JOIN sys.query_store_runtime_stats rs ON p.plan_id = rs.plan_id
GROUP BY q.query_id, qt.query_sql_text, q.last_execution_time
ORDER BY total_executions DESC;
This joins to the runtime statistics to show execution counts, helping you identify frequently executed queries.
Understanding Query Store Query IDs
Each unique query structure gets a query_id. Queries with the same structure but different parameter values share the same query_id, as Query Store parameterizes queries automatically. This means:
SELECT * FROM Orders WHERE OrderID = 1
SELECT * FROM Orders WHERE OrderID = 2
Both variations would have the same query_id because they have identical structure, just different literal values.