Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6093437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:33:35+00:00 2026-05-23T12:33:35+00:00

I have an application where users can take tests (which are composed of questions

  • 0

I have an application where users can take tests (which are composed of questions and answers).

I’m trying to construct a query that returns a count of answers grouped by question, for a specific teacher and test.
The problem is I would like the query to return 0 count for questions with no answers.

answers

  • id
  • question_id
  • test_id
  • student_id

questions

  • id

teacher_students

  • student_id
  • teacher_id

tests

  • id

Query

   SELECT COUNT(answers.id) AS rcount,
          questions.id 
     FROM "questions"
LEFT JOIN answers ON answers.question_id = questions.id
LEFT JOIN teacher_students ON teacher_students.student_id = answers.student_id
    WHERE (questions.test_id = 1)  
      AND (teacher_students.teacher_id = 1)
 GROUP BY questions.id 
 ORDER BY questions.id

Output

 rcount | question_id 
--------+----
      4 | 1
      2 | 3

Desired Output

 rcount | question_id 
--------+----
      4 | 1
      0 | 2
      2 | 3
      0 | 4

If I remove teacher_students.teacher_id = 1, the number of questions returned is correct, but the count is not.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:33:36+00:00Added an answer on May 23, 2026 at 12:33 pm

    Move the teacher_students.teacher_id = 1 check from the WHERE clause to the joining ON clause.

    When a condition that refers to a table in the right side of a LEFT JOIN is put in the WHERE clause, the LEFT JOIN is cancelled and it acts as an INNER JOIN.

    SELECT count(teacher_students.student_id) AS rcount      <--- changed
         , questions.id 
    FROM "questions"
      LEFT JOIN answers 
        ON answers.question_id = questions.id
      LEFT JOIN teacher_students 
        ON teacher_students.student_id = answers.student_id
        AND teacher_students.teacher_id = 1
    WHERE questions.test_id = 1
    
    GROUP BY questions.id 
    ORDER BY questions.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application in which users can send me feedback. When they do
We have a feature in our application where users can take multiple photos from
I have an application where users can store items. Now each type of account
I have an application where multiple users can login(not simultaneously though!only a single user
I want to build a web application where users can register and have a
I have a survey application, where users can create surveys and give choices for
I have an Android application, backed by an API. To this API users can
I have an interactive web application powered by jQuery where users can manipulate visual
Im creating forum like application and I have textbox where users can enter tags
I have a application whereby a user can destroy other users attached to the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.