Tony
Tony

Reputation: 61

SQL Many to Many - Very simple

I have three tables

student: id int (pk), name varchar(255)

discipline: id int (pk), name varchar(255)

student_discipline: student_id int, discipline_id int

I know that this has something to do with many-to-many relationship, and more than one joins are necessary, but i'm very new to sql, so my question is:

How can i make a query that returns only students with 'John' as starting name that have 'Math' as discipline, but only the student id and the student name, for example ?

Any help is appretiated !

Upvotes: 1

Views: 82

Answers (1)

Paweł Gonera
Paweł Gonera

Reputation: 46

SELECT * FROM Student tp
JOIN Student_Discipline tpt on tpt.sID = tp.sID
JOIN Discipline t on tpt.dID = t.dID
WHERE tp.Name like 'John%' and t.Name = 'Math'

Upvotes: 2

Related Questions