Write a query to return a list of professor names and their associated courses for all courses outside of their departments. There should be no duplicate rows, but they can be in any order.

The output should contain two columns: professor.name, course.name.

 

Schema
PROFESSOR
NameTypeDescription
IDIntegerunique id, primary key
NAMEString 
DEPARTMENT_IDIntegerforeign key, department.id
SALARYInteger 
DEPARTMENT
NameTypeDescription
IDIntegerunique id, primary key
NAMEString 

 

COURSE
NameTypeDescription
IDIntegerunique id, primary key
NAMEString 
DEPARTMENT_IDIntegerforeign key, department.id
CREDITSInteger 
SCHEDULE
NameTypeDescription
PROFESSOR_IDIntegerforeign key, professor.id
COURSE_IDIntegerforeign key, course.id
SEMESTERInteger 
YEARInteger 

 

Sample Data Tables

 

PROFESSOR
IDNAMEDEPARTMENT_IDSALARY
1Alex Daniels47169
2Drew Knight19793
3Jordan Myers425194
4Tyler Rodriguez39686
5Blake Gome230860
6Spencer George510487
7Ellis Vasquez46353
8Morgan Flores125796
9Riley Gilbert535678
10Peyton Stevens226648
DEPARTMENT
IDNAME
3Biological Sciences
5Technology
6Humanities & Social Sciences
2Clinical Medicine
4Arts and Humanities
1Physical Sciences

 

COURSE
IDNAMEDEPARTMENT_IDCREDITS
9Clinical Biochemistry23
4Astronomy16
10Clinical Neuroscience25
1Pure Mathematics and Mathematical Statistics13
6Geography17
8Chemistry11
5Physics18
3Earth Science17
7Materials Science and Metallurgy15
2Applied Mathematics and Theoretical Physics15
SCHEDULE
PROFESSOR_IDCOURSE_IDSEMESTERYEAR
4432003
3312011
1752011
7712010
4612001
9312012
10242009
1132014
1232008
1752007

Sample Output

Tyler Rodriguez Astronomy
Jordan Myers Earth Sciences
Alex Daniels Materials Science and Metallurgy
Ellis Vasquez Materials Science and Metallurgy
Tyler Rodriguez Geography
Riley Gilbert Earth Sciences
Peyton Stevens Applied Mathematics and Theoretical Physics
Alex Daniels Pure Mathematics and Mathematical Statistics
Alex Daniels Applied Mathematics and Theoretical Physics
Alex Daniels Materials Science and Metallurgy 

 

Explanation

 

Example logic

  1. Professor Tyler Rodriguez’s department_id is 3, but the Astronomy course’s department_id is 1.
  2. Professor Jordan Myers‘s department_id is 4, but the Earth Sciences course’s department_id is 1

Solution:

				
					SELECT DISTINCT p.NAME AS professor_name, c.NAME AS course_name
FROM PROFESSOR p
JOIN SCHEDULE s ON p.ID = s.PROFESSOR_ID
JOIN COURSE c ON s.COURSE_ID = c.ID
WHERE c.DEPARTMENT_ID <> p.DEPARTMENT_ID;