All four server engines can show recent SQL, but each stores something different: last batch, last statement, ring buffer, or dictionary cache. Only PostgreSQL shows it for your own sessions without extra privileges; MySQL's history needs performance_schema enabled.
Where they differ
Every server engine keeps recent SQL somewhere, but shapes differ: current statement, last statement, or a history ring buffer. SQLite records nothing.
How each engine writes it
SQL Server
SELECT text
FROM sys.dm_exec_connections c
CROSS APPLY sys.dm_exec_sql_text(c.most_recent_sql_handle)
WHERE c.session_id = 52;
most_recent_sql_handle holds the last batch text even after it finished. Query Store (sys.query_store_runtime_stats) gives longer history per database.
PostgreSQL
SELECT pid, state, query
FROM pg_stat_activity
WHERE pid = 12345;
Shows the last statement for idle sessions and the current one for active. Install pg_stat_statements for aggregate history with timings.
MySQL
SELECT THREAD_ID, SQL_TEXT
FROM performance_schema.events_statements_history
ORDER BY EVENT_ID DESC
LIMIT 10;
The ring buffer keeps the last N statements per thread (needs the statement instruments enabled, default in 5.7+). events_statements_current for the live one.
SQLite
-- no server memory of statements; your app is the source of truth
Nothing is recorded. Wrap sqlite3_trace_v2 in your application or use a debug build to log executed SQL.
Oracle
SELECT q.sql_text
FROM v$session s
JOIN v$sql q ON q.sql_id = s.prev_sql_id
WHERE s.sid = 123;
prev_sql_id/prev_child_number is the last executed statement; sql_id is the currently running one. Needs dictionary access, and v$sql may age entries out.
Questions that come up
Why is the text truncated?
SHOW PROCESSLIST truncates at 100 chars; use SHOW FULL PROCESSLIST or performance_schema. SQL Server's sys.dm_exec_sql_text returns the full batch.
Does this show prepared statements?
Mostly the text without values. SQL Server shows the parameterized statement sent through sp_executesql, PostgreSQL's query column shows $1, $2 placeholders but never the bound values, and Oracle's v$sql shows the bind names (:1, :name).
How long is 'last'?
Server-dependent: PostgreSQL and SQL Server keep the last statement per live session, MySQL keeps a bounded history buffer, Oracle caches until aged out of the shared pool.