dsquaredtech
dsquaredtech

Reputation: 89

SQL Query using INNER JOIN

Write a query that displays the student firstname, lastname, course number, and course name for all students taking classes – use an INNER Join. Label the output columns Student First, Student Last, Course Number, and Course Name. You should have 7 rows.

is the question in my lab.

there are three tables students,courses,registration

I can get the names of the students that are registered in a course with

select firstname,lastname from students
inner join on registration students.studentid=registration.studentid

but when i try to get the other data the teacher wants returned from the courses table it doesnt work I tried a million things but what makes sense to me is

select firstname,lastname,coursenumber,coursename from students,courses
inner join registration on students.studentid=registration.studentid

but it gives me an error unknown column students.studentid in on clause.

Upvotes: 2

Views: 2735

Answers (2)

Michael Berkowski
Michael Berkowski

Reputation: 270767

You were very close, missing the joining condition between registration and courses. You have an odd mix of implicit and explicit INNER JOINs. Your join into courses should be another INNER JOIN which is joined through registration to students.

SELECT
  firstname, /* <-- don't forget to label your columns as required */
  lastname,
  coursenumber,
  coursename 
FROM
  students
  /* `students` map into courses via many-to-many relation in `registration` */
  INNER JOIN registration on students.studentid = registration.studentid
  /* Inner join through `registration` into `courses` */
  INNER JOIN courses ON registration.courseid = courses.courseid

And don't forget your column aliases to satisfy the column output naming requirements. Use the AS keyword in your SELECT list. I'll leave that part of the assignment for you to solve.

Label the output columns Student First, Student Last, Course Number, and Course Name

Upvotes: 2

StackOverflowed
StackOverflowed

Reputation: 5975

select firstname,lastname,coursenumber,coursename from students
inner join registration on students.studentid=registration.studentid
JOIN courses  ON courses.courseid = registration.courseid

You mentioned courses in your query but you didn't join it to anything.

Upvotes: 0

Related Questions