 <p>use group by for aggregate function (and for this you don0t need distinct )</p> <pre><code>"SELECT Test_ID AS Tcode, COUNT(*) AS Qcount, User_ID FROM user_answers WHERE User_ID = 1 group by Tcode, User_ID ") </code></pre>