sqlsqlitesubqueryrelational-databasesql-except

I'm trying to learn sqlite queries but keep getting this error


I'm trying to write an SQLite Query using sqliteonline.com that can:

Find the names of faculty members who teach in every room in which some class is taught.

Here are the relations: enter image description here

This is what I have:

SELECT DISTINCT F.fname
FROM Faculty F
WHERE NOT EXISTS (( SELECT *
                    FROM Class C )
                    EXCEPT
                   (SELECT C1.room
                    FROM Class C1
                    WHERE C1.fid = F.fid ))    

AND This is the error I get:

enter image description here

I've double checked and the parenthesis are balanced. So I'm kind of confused on where to go from here.



Solution

  • You must remove the parentheses that enclose each of the subqueries and select the same columns in each of them (I guess only room is needed):

    SELECT F.fname
    FROM Faculty F
    WHERE NOT EXISTS ( SELECT room FROM Class C 
                       EXCEPT
                       SELECT C1.room
                       FROM Class C1
                       WHERE C1.fid = F.fid 
                     )
    

    Also DISTINCT is not really needed, because you are selecting unique (I believe) fnames from Faculty.