Q19 PostgreSQL Domain 2 · 25–30% of exam

An AI application stores document embeddings in Azure Database for PostgreSQL Flexible Server. Which SQL query finds the 5 most similar documents to a given embedding using cosine distance?

  1. A SELECT * FROM docs ORDER BY embedding <-> $1 LIMIT 5
  2. B SELECT * FROM docs ORDER BY embedding <=> $1 LIMIT 5 (correct answer)
  3. C SELECT * FROM docs ORDER BY embedding <#> $1 LIMIT 5
  4. D SELECT TOP 5 * FROM docs WHERE cosine(embedding, $1) > 0.8

Shortcut: The <=> operator in pgvector computes cosine distance. <-> is Euclidean (L2), <#> is inner product (negative dot product). ORDER BY ASC finds nearest neighbours.