0

I have 4 tables in a SQL Server database with following schema:

  1. Attendance

    CREATE TABLE [dbo].[Attendance] (
    [AttendanceId] UNIQUEIDENTIFIER DEFAULT (newid()) NOT NULL,
    [CourseId]     UNIQUEIDENTIFIER NOT NULL,
    [StudentId]    UNIQUEIDENTIFIER NOT NULL,
    [SubjectId]    UNIQUEIDENTIFIER NOT NULL,
    [Semester]     INT              NOT NULL,
    [Month]        NVARCHAR (50)    NOT NULL,
    [Count]        INT              NOT NULL,
    CONSTRAINT [PK_Attendance] PRIMARY KEY NONCLUSTERED ([AttendanceId] ASC),
    CONSTRAINT [FK_Attendance_Student] FOREIGN KEY ([StudentId]) REFERENCES [dbo].[Student] ([StudentId]) );
    
  2. Course

    CREATE TABLE [dbo].[Course] (
    [CourseId] UNIQUEIDENTIFIER DEFAULT (newid()) NOT NULL,
    [Name]     NVARCHAR (50)    NOT NULL,
    CONSTRAINT [PK_Course] PRIMARY KEY NONCLUSTERED ([CourseId] ASC)
    );
    
  3. Student

    CREATE TABLE [dbo].[Student] (
    [StudentId] UNIQUEIDENTIFIER DEFAULT (newid()) NOT NULL,
    [CourseId]  UNIQUEIDENTIFIER NOT NULL,
    [Name]      NVARCHAR (100)   NOT NULL,
    [RollNo]    INT              NOT NULL,
    [Semester]  INT              NOT NULL,
    CONSTRAINT [PK_Student] PRIMARY KEY NONCLUSTERED ([StudentId] ASC),
    CONSTRAINT [FK_Student_Course] FOREIGN KEY ([CourseId]) REFERENCES [dbo].[Course] ([CourseId])
    );
    
  4. Subject

    CREATE TABLE [dbo].[Subject] (
    [SubjectId] UNIQUEIDENTIFIER DEFAULT (newid()) NOT NULL,
    [CourseId]  UNIQUEIDENTIFIER NOT NULL,
    [Name]      NVARCHAR (100)   NOT NULL,
    [Semester]  INT              NOT NULL,
    CONSTRAINT [PK_Subject] PRIMARY KEY NONCLUSTERED ([SubjectId] ASC),
    CONSTRAINT [FK_Subject_Course] FOREIGN KEY ([CourseId]) REFERENCES [dbo].[Course] ([CourseId])
    );
    

I need to create a attendance report in the following format:

Course Name | Student Name | Subject Name | Semester | Month | Count

Please tell me what SQL Query I need to use and if there's any change in schema required then suggest the same.

I'm looking forward to have your replies.

Thanks,

3
  • I've tried the following query: SELECT Course.Name, Student.Name, Subject.Name, Attendance.Semester, Attendance.Month, Attendance.Count FROM Course, Student, Subject, Attendance The problem with this is it is retrieving all records from Student, Subject and Course tables but I want records only for subjects for which the attendance is stored. Commented Mar 24, 2014 at 11:02
  • Google about SQL Joins. Commented Mar 24, 2014 at 11:04
  • Thanks buddy, I've tried INNER JOIN and it led me to achieving the exact query I needed. Thanks for pointing out this aspect! Commented Mar 24, 2014 at 11:32

1 Answer 1

1

You need to use a JOIN in your query so that it only returns rows which match a [StudentId] from the Attendance table.

e.g.

SELECT c.CourseName, s.StudentName, u.SubjectName, u.Semester, a.Month, a.Count
FROM Student s 
    JOIN Attendance a ON s.StudentId = a.StudentId
    JOIN Course c ON a.CourseId = c.CourseId
    JOIN Subject u ON c.CourseId = u.CourseId

Something along these lines will only return rows which specifically match

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

4 Comments

Oh, I'm sorry but I didn't Vote Down buddy and thanks for your reply, it worked with a little adjustment. I'm marking it as Answer. Thanks again!
Can you also tell me how do I create a View from this Query?
read: w3schools.com/sql/sql_view.asp - should be the same as creating view with any query
To create a view, use CREATE VIEW view_name AS SELECT xxx where xxx is your working SELECT statement

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.