Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge data from multiple tables in SQL


Guess I am in a complex situation. Heres the scene.
I have 3 tables in SQL Server.
Enrollment, Student, Course. (FYI, there are no foreign keys)
The table columns are
Student - StudentId, StudentName
Course - CourseId, CourseName
Enrollment - EnrollmentId, StudentId, CourseId, CourseResult

Sample Data
Student - s1, Sid
Course - c1, Science
Enrollment - 1, s1, c1, 80

I would want a SQL query that selects data like below

1, s1, Sid, c1, Science, 80

I did it hard way in the DAL layer with multiple calls to database. But would like to do it in one call, and in DB.

Any one for the rescue!

like image 847
Null Head Avatar asked Nov 27 '22 15:11

Null Head


1 Answers

Use a join.

select enrollment.*, student.*, course.* from enrollment
   inner join Student on enrollment.studentId = student.studentId
   inner join Course on enrollment.courseId = course.courseId
like image 136
Richard Schneider Avatar answered Dec 04 '22 08:12

Richard Schneider