2

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.

8
  • 2
    what are the columns in students? Commented Nov 16, 2012 at 2:57
  • Also how are you joining the table courses? Commented Nov 16, 2012 at 2:59
  • is this right: ` join on registration students.studentid=registration.student.id` I think you have typos, can you correct these? Commented Nov 16, 2012 at 2:59
  • FYI, You're not using a join to connect the courses table Commented Nov 16, 2012 at 2:59
  • sorry forgot to add that columns in students are studentid,firstname,lastname,major,admitdate,graddate,gender,dob Commented Nov 16, 2012 at 3:00

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

5 Comments

this gives me an error unknown column 'courses.courseid' in 'on clause'
@dsquaredtech Not possible unless you misspelled it or you used a table alias like INNER JOIN courses AS c ON...
In your comment above, you said the cols in courses were colums in courses courseid,coursename You did not mention a coursenumber column as in your SELECT list though..
aha i'm an idiot that's probably it it's not courseid it's coursenumber
@dsquaredtech as both registration.coursenumber and courses.coursenumber? Substitute the correct column names in the join's ON clause and you should be in business.
0
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.

2 Comments

I keep trying these queries and get an error similiar to what I was getting with select firstname,lastname,coursenumber,coursename from students,courses inner join registration on students.studentid=registration.studentid except now the unknown column is courses.courseid in the on clause
Glad your question got answered and congrats on joining S/O! In the future, if you have any more MySQL related questions, what would help would be to run SHOW CREATE TABLE _tablename_ for each table involved in your queries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.