I have 3 tables in my DB (MySQL).
categories (name:string)
items (name:string, category_id:int)
votes (value:int, item_id:int, created_at:datetime)
>
You could probably use group by in this case and remove the join. I always screw up when using group by but Something like
SELECT COUNT(votes.item_ID) AS score,
(SELECT ItemTitle FROM items WHERE items.item_id = votes.item_id) as Title
FROM votes
WHERE votes.created_at > #{1.week.ago}
Group By Title
Order By score
Limit 5
AS
"as" allows you to give something a name.
Notice above the as score, this gives the result from count(votes.item_id) the column name of score since it did not have a column name before. You could also use this if you want to call something by another name in the rest of the query.
If you took off the as score it would come back as a column with no title and no way to access it by name, only by number.
JOIN
A join will merge 2 tables as 1 temp table and return this table. There are inner, outer, left, right and cross joins. Each with its own advantages but all have the same problem of being slow. Look into sub queries to replace most joins.
You will also want to avoid using Select *, list out all the things you need.
The best way to figure these out is to just run them all and see what they return and read what they are supposed to do w3Schools Joins