'SQL inner join performance data duplicate

table -- > qt

qstnId tagId
1 1
1 2
2 3
2 4
2 2

table --> question

qid
1
2

qid is primary key and qstnId is Fk

now when i'm running query -->

mysql> select tagId from qt inner join question on qt.qstnId = 1;

it's returning;

tagId
2
1
2
1

My question is why am i getting duplicate data here.



Solution 1:[1]

Filtering

qt.qstnId = 1 is a filtering clause; it belongs in a WHERE clause.

Relation

qt.qstnId = question.qid states how the tables are related via a JOIN. It belongs in an ON clause after the JOIN.

Fix your query; if you still have concerns; provide SHOW CREATE TABLE so we can see if you have the necessary indexes (for performance).

Solution 2:[2]

SELECT tagId 
FROM qt 
INNER JOIN question on qt.qstnId = question.qid
WHERE qid=1;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rick James
Solution 2 Dúver Cruz