I have two Tables:
Table 1: Questions:
QuestionId NUMERIC
Title TEXT
Test Data in Question Table:
QuestionId Title
1 What is your name?
2 What is your age?
Table 2: Answers:
AnswerId NUMERIC
PersonId NUMERIC
QuestionId NUMERIC
Answer TEXT
If there is NO DATA in Answers Table then below query returns correct results (2 rows):
SELECT q.QuestionId, q.Title, a.Answer
FROM Questions q
LEFT OUTER JOIN Answers a ON q.QuestionId = a.QuestionId
WHERE a.PersonId = 2 OR a.PersonId IS null
But if i have for example 1 record in Answer Table like:
AnswerId 1
PersonId 1
QuestionId 1
Answer 'My Name is Yaqub'
Below query works fine:
SELECT q.QuestionId, q.Title, a.Answer
FROM Questions q
LEFT OUTER JOIN Answers a ON q.QuestionId = a.QuestionId
WHERE a.PersonId = 1 OR a.PersonId IS null
But this query returns just one row:
SELECT q.QuestionId, q.Title, a.Answer
FROM Questions q
LEFT OUTER JOIN Answers a ON q.QuestionId = a.QuestionId
WHERE a.PersonId = 2 OR a.PersonId IS null
The above query returns just one row which is wrong, becuase i have two questions in Question Table & the above query should return both of them because of the condition 'OR a.PersonId IS null'.
Why its not returning TWO Rows?
Desired Results:
I want to get all the questions(2 rows) & those answers where PersonId has some value,for example TWO rows for Person 2 because i have no data for him in the Answer Table.
Introduction to SQLite LEFT JOIN clause Similar to the INNER JOIN clause, the LEFT JOIN clause is an optional clause of the SELECT statement. You use the LEFT JOIN clause to query data from multiple related tables.
The plus sign is Oracle syntax for an outer join. There isn't a minus operator for joins. An outer join means return all rows from one table. Also return the rows from the outer joined where there's a match on the join key.
Unfortunately, SQLite does not support the RIGHT JOIN clause and also the FULL OUTER JOIN clause. However, you can easily emulate the FULL OUTER JOIN by using the LEFT JOIN clause.
I FOUND the SOLUTION:
SELECT q.QuestionId, q.Title, a.Answer
FROM Questions q
LEFT OUTER JOIN Answers a ON q.QuestionId = a.QuestionId AND
(a.PersonId = 2 OR a.PersonId IS null)
This query will list all the questions & the WHERE clause will filter out those answers where PersonID has a specific value or is null. So whenever there's an answer to a question, i'll get no NULL values for PersonID.
You can do it just like this:
SELECT q.QuestionId, q.Title, a.Answer
FROM Questions q
LEFT OUTER JOIN Answers a ON q.QuestionId = a.QuestionId AND
a.PersonId = 2
The query still returns records with PersonId = null because of outer join.
This is what you have when you do the join, before the where
is applied:
QuestionID AnswerID PersonID
1 1 1
2 null null
There actually is only one record in the questions table that matches (PersonID = 2 or PersonID is null)
, so you should expect only one row.
Did you mean to put
a.PersonId = 1 OR a.PersonId IS null
instead?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With