The object returned from
Drupal::entityQuery(), in Drupal 10, assumes the database has been created from Drupal 10. It will not work with a database created from Drupal 7 because Drupal 7 creates different tables which have different schemas.
With the object returned by
Drupal::database(), you could query entities from a Drupal 7 database, but you would need to adjust the query for the schema used from Drupal 7.
Since you are directly querying the database table, that is not different from querying a database table that is not for entities, as long as you keep in mind the schema used for those tables in Drupal 7.