The similarity matrix solution

Initial SQL query:
"SELECT a.docid, b.docid, sum(a.count * b.count) FROM Frequency a, Frequency b WHERE a.term = b.term GROUP BY a.docid, b.docid"

then used R to calculate the results,

Then figured out "right" way in SQL is:
SELECT count FROM(SELECT a.docid AS daid, b.docid AS dbid, sum(a.count * b.count) AS count FROM Frequency a, Frequency b WHERE a.term = b.term GROUP BY a.docid, b.docid) WHERE daid='10080_txt_crude' and dbid='17035_txt_earn'
solved the key word search

SQL query:
SELECT *
FROM(
  SELECT a.docid AS daid, b.docid AS dbid, sum(a.count * b.count) AS count
  FROM (
    SELECT *
    FROM frequency
    UNION
    SELECT 'q' as docid, 'washington' as term, 1 as count
    UNION
    SELECT 'q' as docid, 'taxes' as term, 1 as count
    UNION
    SELECT 'q' as docid, 'treasury' as term, 1 as count) a,
    (SELECT *
    FROM frequency
    UNION
    SELECT 'q' as docid, 'washington' as term, 1 as count
    UNION
    SELECT 'q' as docid, 'taxes' as term, 1 as count
    UNION
    SELECT 'q' as docid, 'treasury' as term, 1 as count) b
  WHERE a.term = b.term
  GROUP BY a.docid, b.docid)
WHERE daid='q'
ORDER BY count DESC