Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite LEFT JOIN & WHERE Clause confusion

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.

like image 307
Yaqub Ahmad Avatar asked Jun 27 '12 04:06

Yaqub Ahmad


People also ask

Does SQLite have left join?

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.

What does (+) mean in JOINs in SQL?

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.

Is SQLite right join?

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.


3 Answers

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.

like image 112
Yaqub Ahmad Avatar answered Oct 21 '22 17:10

Yaqub Ahmad


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.

like image 29
Pedro Velha Avatar answered Oct 21 '22 15:10

Pedro Velha


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?

like image 1
Blorgbeard Avatar answered Oct 21 '22 16:10

Blorgbeard