title: Retrieve data column wise in SQL Server
body: <p>I have a problem where I need to display subject names (ex- english, science ....)  of student as columns and under each subject their respective marks. I query a database and get these values as rows in dataset not as columns. How should I query the database?</p>
  
  <p>My query result</p>
  
  <pre><code>student name | marks  | Subectname
  ------------------------------------
  raghuram        78         english
  raghuram        34         physics
  raghuram        76         chemistry
  
   ................              ...         ..........
  </code></pre>
  
  <p>Required result in report</p>
  
  <pre><code>studentname | english | Maths | physics | ......
  ----------------------------------------------------
  raghuram        78       66        32
  </code></pre>
  
  <p>Here's my query</p>
  
  <pre><code>SELECT
      st.FirstName + ' ' + st.LastName as StudentName,
      s.Subject_Name, se.Mark 
  FROM
      dbo.FEE_STUDENT_EXAM se
  INNER JOIN 
      dbo.FEE_STUDENT_GRADE sg ON sg.Student_ID=se.Student_ID
  INNER JOIN 
      FEE_STUDENT st ON st.Student_ID=sg.Student_ID
  INNER JOIN 
      dbo.EMP_SUBJECT s ON s.SubjectID=se.Subject_ID
  WHERE
      sg.Grade_ID = @GradeId 
      AND sg.Division_ID = @DivId 
      AND se.Subject_ID IN (SELECT d.SubjectId 
                            FROM dbo.FEE_STUDENT_EXAM_SCHEDULED d 
                            INNER JOIN dbo.FEE_STUDENT_EXAM_SCHEDULEH h ON h.ExamScheduleID = d.ExamScheduleID 
                            WHERE h.Grade_Id = @GradeId AND h.Division_ID = @DivId 
                              AND YEAR(CAST(d.TestDate AS DATETIME)) = @Year
                              AND MONTH(CAST(d.TestDate AS DATETIME)) = @Month)
  </code></pre>