If I understand you correctly, I think you want to be able to find out in MapBasic what table a query is based on.
In your queries I would advise adding the into clause to the end of the select statement so you know what the table name is you are dealing with. For eg.
Select * from earth where population >100 into qryName
To determine whether a table is a query or not, you can use the following:
Print TableInfo(qryName, TAB_INFO_TYPE)
If this returns 1 its a base table and if it returns 2 the table is a query.
If the layer is a query, then you can use
Print TableInfo(qryName, TAB_INFO_MAPPABLE_TABLE)
This will tell you which layer the query is based on. But it only works with mappable queries. This is from the MapBasic reference guide:
String result indicating the name of
the table containing graphical
objects. Use this code when you are
working with a table that is actually
a relational join of two other tables,
and you need to know the name of the
base table that contains the graphical
I think for you case this should be fine.