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

  • SEARCH
  • Home
  • 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 8950497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:28:03+00:00 2026-06-15T13:28:03+00:00

There are two tables: question and answer . In answer I hold user_id and

  • 0

There are two tables: question and answer. In answer I hold user_id and question_id. I want to count how many times each choice is selected.

Below is a working query, but instead of joining the same table 4 times, what is a faster way i.e. joining the answer table only once.

SELECT question.question_id,
    question.correct_choice,
    COUNT(DISTINCT a.user_id) as num_of_a,
    COUNT(DISTINCT b.user_id) as num_of_b,
    COUNT(DISTINCT c.user_id) as num_of_c,
    COUNT(DISTINCT d.user_id) as num_of_d
FROM answer a,
    answer b, 
    answer c, 
    answer d,
    question 
WHERE a.question_id = question.question_id 
    AND b.question_id = question.question_id
    AND c.question_id = question.question_id 
    AND d.question_id = question.question_id 
    AND a.choice = 'A' 
    AND b.choice = 'B'
    AND c.choice = 'C' 
    AND d.choice = 'D'  
GROUP BY question.question_id 
ORDER BY question.question_id asc;

returns

273, D, 5, 2, 8, 39
274, C, 2, 14, 50, 2
277, C, 3, 5, 41, 17
278, C, 16, 9, 34, 9
279, C, 8, 30, 24, 12
280, B, 17, 21, 20, 3
284, C, 2, 3, 19, 1
286, A, 16, 3, 2, 2
287, D, 1, 2, 1, 18
289, B, 3, 18, 2, 2
290, D, 6, 9, 8, 6
  • 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-06-15T13:28:05+00:00Added an answer on June 15, 2026 at 1:28 pm

    This solution only does a single join… additionally, I converted your implicit joins to explicit, and rounded out your GROUP BY:

    SELECT 
        q.question_id,
        q.correct_choice,
        COUNT(DISTINCT CASE WHEN a.choice = 'A' THEN a.user_id END) as num_of_a,
        COUNT(DISTINCT CASE WHEN a.choice = 'B' THEN a.user_id END) as num_of_b,
        COUNT(DISTINCT CASE WHEN a.choice = 'C' THEN a.user_id END) as num_of_c,
        COUNT(DISTINCT CASE WHEN a.choice = 'D' THEN a.user_id END) as num_of_d
    FROM 
        answer a
        JOIN question q ON a.question_id = q.question_id
    GROUP BY q.question_id, q.correct_choice
    ORDER BY q.question_id asc;
    

    This works because when the CASE statement doesn’t evaluate to true, it returns NULL, which won’t be included in the COUNT DISTINCT of user Ids.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, so say I have two tables. Question questionID QuestionDescription Answer AnswerID QuestionID AnswerDescription
There are two tables, one is an Answer table and the other is a
I'm designing an answer question site. Previously i create two tables: posts and answers.
Let's suppose I have two tables: Question (Id, Text) Answer (Value, QuestionId, Guid) Column
I have two tables: question(qid int,title varchar(100)) answer(aid int,qid int,vote int,content varchar(4096)) Qid and
There are two tables Table 1 select * from manage_tcp; +----+----------+--------+ | id |
There are two tables: CREATE TABLE STORE ( ID INTEGER NOT NULL, MEDICINE_ID INTEGER
There are two tables Table A id column_a 01 abc 01 abc 02 abc
there are two tables tbldoctor and tblschedule tbldoctor doc_id(PK) docMrId docfname 1 22 manish
There are two tables: A, with rows (row_id, column1, ...) and B, with rows

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.