Save the following code in a module.
Sub print_query_table_sql()
Dim s As Worksheet
Dim qt As QueryTable
Dim r As Range
Dim sql As String
Set s = ActiveSheet
Set qt = s.QueryTables(1)
sql = qt.sql
Set r = s.Cells(1).SpecialCells(xlLastCell).Offset(1, 1)
r = sql
End Sub
Select your worksheet that contains data from a database source, then run the above macro.
It will print out the SQL in the last cell. (Ctrl-End).