How to find instructors teaching the same courses as Professor John Cullen?

I’m working on a database query problem and could use some help. I need to find out which teachers are teaching the same courses as John Cullen. I have two tables: Faculty and CourseSection.

Here’s what I’ve tried so far:

SELECT f.firstname, f.lastname, cs.facid, cs.courseID
FROM CourseSection cs
JOIN Faculty f ON cs.facid = f.facid
WHERE cs.courseID IN (
    SELECT courseID
    FROM CourseSection
    WHERE facid = (SELECT facid FROM Faculty WHERE firstname = 'John' AND lastname = 'Cullen')
)
AND (f.firstname != 'John' OR f.lastname != 'Cullen')

This query doesn’t seem to work correctly. Can anyone spot the issue or suggest a better approach? I’m pretty new to SQL and would appreciate any guidance. Thanks!

Your approach is on the right track, but there are a few adjustments we can make to improve the query. Here’s a modified version that should work:

SELECT DISTINCT f.firstname, f.lastname, cs.facid, cs.courseID
FROM CourseSection cs
JOIN Faculty f ON cs.facid = f.facid
WHERE cs.courseID IN (
    SELECT courseID
    FROM CourseSection
    WHERE facid = (SELECT facid FROM Faculty WHERE firstname = 'John' AND lastname = 'Cullen')
)
AND f.facid != (SELECT facid FROM Faculty WHERE firstname = 'John' AND lastname = 'Cullen')

The key changes are:

  1. Added DISTINCT to avoid duplicate results
  2. Modified the last condition to exclude John Cullen based on facid

This query should return all faculty members teaching the same courses as John Cullen, excluding John Cullen himself. Let me know if you need any further clarification!

Hey there Iris! :wave: That’s an interesting database problem you’ve got there. I’m no SQL expert, but I’m curious about a few things:

Have you tried running the query without the last condition (AND (f.firstname != ‘John’ OR f.lastname != ‘Cullen’))? Sometimes it’s easier to debug if you start with a simpler version and build up from there.

Also, I’m wondering if using a subquery in the WHERE clause might be complicating things. What about trying a JOIN to directly compare the courses? That could simplify the logic a bit.

And just a thought—are you absolutely sure that John Cullen is listed in the Faculty table exactly as you expect? Maybe double-checking that data could help clarify things.

What kind of results are you getting with your current query? Is it returning too many rows, too few, or just not matching at all? Curious to see where you’re landing with this!

SQL can be tricky sometimes, but a bit of tweaking should get you that correct output. Let us know how it goes!

hey iris, ur query looks good but maybe try adding DISTINCT to avoid duplicates? also, for excluding john cullen, u could use NOT EXISTS instead of that last condition. something like:

NOT EXISTS (SELECT 1 FROM Faculty WHERE f.facid = Faculty.facid AND firstname = ‘John’ AND lastname = ‘Cullen’)

hope this helps! lemme kno if u need more info