diff --git a/synapse/storage/user_directory.py b/synapse/storage/user_directory.py
index 72a9071d0301fb7cf89c748bde7620b732a4848d..4de552c1bbc2023b2ece5bc2656c1e714c762d48 100644
--- a/synapse/storage/user_directory.py
+++ b/synapse/storage/user_directory.py
@@ -648,7 +648,7 @@ class UserDirectoryStore(BackgroundUpdateStore):
                 ) AS s USING (user_id)
             """
             join_args = (user_id,)
-            where_clause = "p.user_id IS NOT NULL"
+            where_clause = "(s.user_id IS NOT NULL OR p.user_id IS NOT NULL)"
 
         if isinstance(self.database_engine, PostgresEngine):
             full_query, exact_query, prefix_query = _parse_query_postgres(search_term)