LPS-43172 fix wrong query (includes tests)