I am trying to do the following in ruby on rails, but even if I can get an answer in mysql that would be great.
I have tables Student and Courses. Student -> Courses is one-to-many relationship.
Student Course
|--------------| |--------------------------------------|
|id| name | |id | course_name | grade | student_id |
|---------- | |--------------------------------------|
|S1| student 1 | |C1 | Course1 | A | S1 |
|S2| student 2 | |C2 | Course2 | C | S1 |
|---------- | |C3 | Course1 | A | S2 |
|C4 | Course2 | B | S2 |
|--------------------------------------|
select * from Student
where id NOT IN (select student_id from Course where grade = 'C')
I want to achieve same result using single SQL JOIN statement or using activerecord.
Or join to your subquery
Or use exists
I’m afraid I can’t test this at the moment and I have a suspicion that these queries may not help you. I have no experience with ActiveRecord. Let me know in the comments.