Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to select the distinct values of three sql tables and perform inner join with python sqlite3?

I’m trying to perform an inner join of distinct values of three tables from an SQLite DB. I tried multiple times and failed. Please guide me.

Below is a pseudo-code of what I’m trying to achieve

sql = ''' 
SELECT DISTINCT lesson_id, question_id FROM lesson_practice_questions as lpq

INNER JOIN 
SELECT DISTINCT topic_id, lesson_id FROM chapter_lessons as cl
WHERE cl.topic_id==2 
ON cl.lesson_id = lpq.lesson_id 

INNER JOIN 
SELECT DISTINCT question_id, subject_id, question_type_id, knowledge_type_ids complexity_level FROM questions as q
ON q.question_id = lpq.question_id;'''

cur.execute(sql)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

here is the right sql syntax , however you need to provide sample data and desired output if this is not the right output :

SELECT
    lesson_id,
    question_id,
    topic_id,
    lesson_id,
    question_id,
    subject_id,
    question_type_id,
    knowledge_type_ids,
    complexity_level
FROM  lesson_practice_questions as lpq
INNER JOIN chapter_lessons as cl on cl.topic_id = 2 and cl.lesson_id = lpq.lesson_id
INNER JOIN questions as q ON q.question_id = lpq.question_id;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading