mysql - Difference between these sql queries -


i can't seem understand why these 2 queries return different results following task: "find names , grades of students have friends in same grade. return result sorted grade, name within each grade."

tables here: https://lagunita.stanford.edu/c4x/db/sql/asset/socialdata.html

the first query:

select distinct h1.name, h1.grade highschooler h1, friend f, highschooler h2 h1.id = f.id1 , h2.id = f.id2 , h1.grade = h2.grade order h1.grade, h1.name 

the second query:

select name, grade highschooler id not in (     select id1 highschooler h1, friend, highschooler h2     h1.id = friend.id1 , friend.id2 = h2.id , h1.grade <> h2.grade) order grade, name; 

the second 1 returns expected result, not first one. if cares clarify, thanks.

the first query applies 3 filter in query simultaneously data in tables , returns entries matching filters. second query firstly subquery returns rows matching subquery condition , ids not there returned, includes ids h1.id = friend.id1 , friend.id2 = h2.id not hold true. can try like:

select name, grade highschooler where h1.id = friend.id1 , friend.id2 = h2.id  , id not in (     select id1 highschooler h1, friend, highschooler h2     h1.id = friend.id1 , friend.id2 = h2.id , h1.grade <> h2.grade) order grade, name; 

Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -