Showing posts with label SQL. Show all posts
Showing posts with label SQL. Show all posts

Friday, 1 February 2013

How to get all the Emails as Comma separatesd string in Sql Server.

SQL Server provides a function named Coalesce to do the task to join all the column value by comma separated string.

The Query is below:
Go
Declare @strEmail varchar(max)

SELECT @strEmail=coalesce(@strEmail+',','')+[Email]
       FROM Users      
print @strEmail  

Monday, 12 March 2012

FIND Nth Maximum value and Nth minimum Value from a colum in a table in MS-Sql Server.

 FIND Nth Maximum value 
SELECT id, amount
FROM(SELECT id, amount, Row_Number() OVER(ORDER BY amount DESC) AS highest
FROM @tmp
) as x
WHERE highest = 3

-- FIND Nth Minimum value 
SELECT id, amount
FROM(SELECT id, amount, Row_Number() OVER(ORDER BY amount ASC) AS lowest
FROM @tmp


) as x
WHERE lowest = 3

Second Highest Salary From a table( such as Employee Table)

Sometimes it requires to find the second most from a column(say second highest salary from employee table) in a table, to do it write the Query below:

select max(salary) from employee where salary not in (select max(salary) from employee );