I've written the following SQL to count the number of times the name 'Cthulhu' turns up for each tag on Stack Overflow (original here):
select t.TagName, count (*) 'Tainted'
from Posts p, Tags t, PostTags pt
where p.Body like '%cthulhu%'
and (pt.PostId = p.Id and t.Id = pt.TagId)
group by t.TagName
order by Tainted DESC, t.TagName ASC
It works, but I'm not used to cutting SQL manually; I'm more accustomed to using ORMs. I tried using
CONTAINS instead of
LIKE, but apparently
Body isn't set up for full-text search.
Could you please provide me some feedback - in particular, are there any best practices I'm missing, and whether there are standard formatting rules for SQL that would make it a bit easier on the eye?