Question: ```
SELECT SUM(Marks) AS tot
FROM [Tbl_Results]
WHERE [month] = 'Mar'
    AND [year] = '2016'
GROUP BY sname

```

the output of the above query i want to pass to the another query stating that max of the "Marks"

```
select MAX(?)  from [Tbl_Results]  
? how to pass an argument to get an max of marks from the table

```

Answer: 
There are a number of ways to do this.  

You can create a [cte](https://msdn.microsoft.com/en-us/library/ms175972.aspx) from your first query, like this:

```
;WITH cte AS
(
    SELECT SUM(Marks) AS tot
    FROM [Tbl_Results]
    WHERE [month] = 'Mar'
    AND [year] = '2016'
    GROUP BY sname
)

SELECT MAX(tot)  
FROM cte  

```

Or you could create a view from it:

```
CREATE VIEW vwTot 
AS

    SELECT SUM(Marks) AS tot
    FROM [Tbl_Results]
    WHERE [month] = 'Mar'
    AND [year] = '2016'
    GROUP BY sname
GO

SELECT MAX(tot)  
FROM vwTot  

```

You can also insert the results into a temporary table:

```
SELECT SUM(Marks) AS tot INTO #Temp
FROM [Tbl_Results]
WHERE [month] = 'Mar'
AND [year] = '2016'
GROUP BY sname

SELECT MAX(tot)  
FROM #Temp  

```

Or use it as a derived table:

```
SELECT MAX(tot)  
FROM (
    SELECT SUM(Marks) AS tot
    FROM [Tbl_Results]
    WHERE [month] = 'Mar'
    AND [year] = '2016'
    GROUP BY sname
) dt

```
you can use a subquery. since you're using a groupby clause you also need to add sname to your select statement.

```
select MAX(tot)  from 
(
    SELECT  sname, SUM(Marks) AS tot
    FROM [Tbl_Results]
    WHERE [month] = 'Mar'
    AND [year] = '2016'
    GROUP BY sname
) b

```