Here is my tables
Student (sname, sid, gpa, level, deptno)
Course (cno, cname, deptno, units)
Dept (dname, deptno)
Takes (sid, cno)
Write a SQL query that returns the names (i.e., snames) of students who have taken
more courses outside their department than inside their department.you can
assume that all students in the database have taken at least one course inside their department.
I am not looking for any solutions for this question, but still welcome for any answer.
But I more hoping people can tell me how to generate the steps to write a complicated query like this..
My answer is
Select S.sname
From Student S, Course C, Dept D, Takes T
Where T.cno=C.cno and D.deptno=C.deptno and S.sid = T.sid
Having COUNT(S.deptno=C.deptno) > COUNT( S.deptno != C.deptno)
I am not sure I can use the count after HAVING in this way or not .
Thanks
Your first attempt, corrected:
and converted to SQL-92 syntax: