I am brand and new with my current job i have see the following SQL sentence.
SELECT t.name store,p.id, p.name,count(1)
FROM regular_shippings sh
JOIN
products p ON matching columns.
ANOTHER JOINS
WHERE criteria
AND anotherCriteria
GROUP BY p.id
HAVING count(1) > 0
ORDER BY t.name,count(1) desc;
My first impression is counting the records in
the select
also in the
having by
also in the
order by
Impacting performance for me is better use a alias. But is this assertion correct? or is not is doing the job only once? or the engine is smart enough to recognize it and just replace the previous values of the count(1) in the latter calls?
What i mean this query would perform better?
SELECT t.name store,p.id, p.name,count(1)c
FROM regular_shippings sh
JOIN
products p ON matching columns.
ANOTHER JOINS
WHERE criteria
AND anotherCriteria
GROUP BY p.id
HAVING c>0
ORDER BY t.name,c desc;
Sorry if the question is plain!
group byandorder byand reading the data.