It sounds like you need to "pivot"
Using the PIVOT sql command you can rotate and restructure the data although I'm not sure you can get it working for n columns as a static query.
You need an extra id in the marks table so each mark against each id has own sequence too. So the primary key would be id, markid.
Id MarkId Mark
Row1 1 1 15
Row2 1 2 16
Row3 1 3 17
Row4 2 1 25
Row5 2 2 26
Row6 2 3 27
You can then use PIVOT to rotate the selection.
SELECT Id, [1], [2], [3]
FROM
(SELECT Id, MarkId, Mark FROM MarksTable) AS SourceTable
PIVOT
(Min(Mark) FOR MarkId IN ([1], [2], [3])) AS PivotTable
This will output like this:
Id "1" "2" "3"
1 15 16 17
2 25 26 27
The following is a full copy and paste script which follows your example and works:
SELECT Id = 1, MarkId = 1, Mark = 15 INTO #Temp UNION
SELECT Id = 1, MarkId = 2, Mark = 16 UNION
SELECT Id = 1, MarkId = 3, Mark = 17 UNION
SELECT Id = 2, MarkId = 1, Mark = 25 UNION
SELECT Id = 2, MarkId = 2, Mark = 26 UNION
SELECT Id = 2, MarkId = 3, Mark = 27
SELECT * FROM #Temp
SELECT Id, [1], [2], [3]
FROM
(SELECT Id, MarkId, Mark FROM #Temp) AS SourceTable
PIVOT
(Min(Mark) FOR MarkId IN ([1], [2], [3])) AS PivotTable
DROP TABLE #Temp
Using a dynamic query you can build up a select for n number of marks by generating the SQL on the fly and running it using SP_ExecuteSQL.
The following article shows how to build a PIVOT query using dynamic SQL
http://www.simple-talk.com/community/blogs/andras/archive/2007/09/14/37265.aspx[
^]