I had this problem because I had a semicolon (;) at the end of my query. This messes up how QGIS was loading the query. I found this out when I learned that you can view logs in QGIS while trying to figure this out. Nice!
This was what I saw in the logs:
2022-06-09T18:15:28 WARNING Unable to execute the query.
The error message from the database was:
ERROR: syntax error at or near ";"
LINE 1: ... on ST_DWithin(cs.wkb_geometry, s.wkb_geometry, 2) limit 10;
^
.
SQL: SELECT * FROM (SELECT row_number() over () AS _uid0_, * FROM (select cs.* from class_segments as cs join shorelines as s on ST_DWithin(cs.wkb_geometry, s.wkb_geometry, 2) limit 10;
) AS _subq_0_
) AS "subQuery_0" LIMIT 1
which clued me into the fact that the semicolon was interrupting the query that was being injected.
You can view logs panel with the little message icon at the bottom right of the QGIS window.
So, when I changed
select cs.* from class_segments as cs join shorelines as s on ST_DWithin(cs.wkb_geometry, s.wkb_geometry, 2);
to
select cs.* from class_segments as cs join shorelines as s on ST_DWithin(cs.wkb_geometry, s.wkb_geometry, 2)
It worked!