0

I have 2 tables in my DB, first table called "questions" and hold questions with id, second table called "answers" and hold answers for the questions (as multiple choices).

how to select questions that have less than 4 answers?

questions table:

id     question
1      what is ...?
2      how many ...?
3      Is ....?

answers table

id     question_id     answer
1          1             54
2          1             11
3          1             22
4          2            England
5          1              5
6          2            Turkey

how to select questions that have answers less than 4?

thanks,

2 Answers 2

3
select questions.id, questions.question from questions 
inner join answers on questions.id = answers.question_id
group by questions.id, questions.question having count(questions.id) <4

here you go.

Sign up to request clarification or add additional context in comments.

3 Comments

I want to return questions (text)
your join is not perfect because none of second table columns selected, select question.id, questions.question from questions means that the join is expected
@Farrokh, what's d problem for not having column selected on 2nd column? joining it just to get the count for list of answers. subquery normally will take longer time to execute when the data is huge which inner join is normally use the primary key which result faster. But anyway there was missing some s on my questions, fixed it
0

use this :

SELECT * 
FROM questions 
WHERE id in
    (
    SELECT question_id 
    from answers 
    group by question_id 
    having count(question_id) <4
    )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.