Create a table with exactly one row. Then you can use the left join to achieve the desired NULL result.
CREATE TABLE dummy (d TINYINT NOT NULL); INSERT INTO dummy SET d = 1; SELECT q25, ( ( AVG( q1 ) + AVG( q2 ) + AVG( q3 ) ) /3 ) AS Overall FROM dummy LEFT JOIN t_results ON brand = 'XYZ' AND DATE = 'MAY2012' GROUP BY q25 ORDER BY Overall DESC LIMIT 1
You can also replace the dummy table with a subquery:
SELECT q25, ( ( AVG( q1 ) + AVG( q2 ) + AVG( q3 ) ) /3 ) AS Overall FROM (SELECT 1) AS dummy LEFT JOIN t_results ON brand = 'XYZ' AND DATE = 'MAY2012' GROUP BY q25 ORDER BY Overall DESC LIMIT 1
Tested with sqlfiddle , where you can also experiment with alternatives.
Result selection conditions that were previously in the WHERE should now be included in the ON clause. Otherwise, the left join will create NULL strings that will be deleted using WHERE instead of generating a single NULL string if no matching string is found. If there were no WHERE clauses in the original query, ON 1 can be used to express any row matches .
source share