SELECT s.*, q.questoes FROM simulado s LEFT JOIN ( SELECT COUNT(DISTINCT q.id) as questoes, pq.simulado_id FROM questao q JOIN assunto a on a.id = q.assunto_id JOIN disciplina d on d.id = a.disciplina_id JOIN simulado_questao pq on pq.questao_id = q.id WHERE q.ativo = 1 and d.ativo = 1 and pq.ativo = 1 and pq.simulado_id = GROUP BY pq.simulado_id ) q on q.simulado_id = s.id WHERE s.id = Array ( [0] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 156 [code] => 156 [2] => [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near the keyword 'GROUP'. [message] => [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near the keyword 'GROUP'. ) [1] => Array ( [0] => 01000 [SQLSTATE] => 01000 [1] => 16954 [code] => 16954 [2] => [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Executing SQL directly; no cursor. [message] => [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Executing SQL directly; no cursor. ) )