SQL: Find all rows in a table when the rows are a foreign key in another table -


the caveat here must complete following tools:

  1. the basic sql construct: select .. where... distinct ok.

  2. set operators: union, intersect, except

  3. create temporary relations: create view... ...

  4. arithmetic operators <, >, <=, == etc.

  5. subquery can used in context of not in or subtraction operation. i.e. (select ... from... not in (select...)

i can not use join, limit, max, min, count, sum, having, group by, not exists, exists, count, aggregate functions or else not listed in 1-5 above.

schema:

people (id, name, age, address)  courses (cid, name, department)  grades (pid, cid, grade) 

i satisfied query used not exists (which can't use). sql below shows people took every class in courses table:

select people.name people not exists (select courses.cid courses not exists (select grades.cid grades grades.cid = courses.cid , grades.pid = people.id)) 

is there way solve using not in or other method allowed use? i've struggled hours. if can goofy obstacle, i'll gladly upvote answer , select answer.

as nick.mcdermaid said can use except identify students missing classes , not in exclude them.

1 complete list cartesian product of people x courses. grades if every student has taken every course.

create view complete_view     select people.id pid, courses.id cid     people, courses 

2 use except identify students missing @ least 1 class

create view missing_view select distinct pid (     select pid, cid complete_view     except     select pid, cid grades ) t 

3 use not in select students aren't missing classes

select * people id not in (select pid missing_view) 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -