Vous êtes sur la page 1sur 5

12/24/2016 SQL Server Interview Questions: What is the use of COALESCE in SQL Server

0   More    Next Blog» Create Blog   Sign In

SQL Server Interview Questions
C#
Boo MV ASP. C SQL W Written HR Subsc SQL Buy
Program 0
ks C NET # Server CF Test Round ribe Tutorial DVD
s

If you like this website, please share


this site using g+1 button What is the use of COALESCE in SQL Server
Recommend this on Google
{
Take: [“the”],
Android:
“course”
}
Best software training institute in bangalore for SAP ABAP,
SAP BI, .Net, Informatica, Software Testing, Siebel CRM
I am Venkat and this is my website. I did training in Pragim
Technologies and got job in Dell in less than a week. PRAGIM is the
best S/W training institute in Bangalore.
Click here for Hyderabad Center Website
For further details please call 09900113931.

Let us understand the use of COALESCE with the help of an example.
 

In this example, the Candidate table is shown to include three columns with
information about a Candidate: 
1. Candidate_id
2. PrimaryEmail
3. SecondaryEmail
 

COALESCE in the SELECT statement below, selects the PrimaryEmail if it is
not null. If the PrimaryEmail is null then SecondaryEmail will be selected. If both
PrimaryEmail and SecondaryEmail is present then only PrimaryEmail is
selected. So, COALESCE returns the first nonnull column among the list of
columns passed. If both PrimaryEmail and SecondaryEmail is NULL,
COALESCE returns NULL.

SAP BI Training in Bangalore
Best SAP BI Training institute in Bangalore,
Marathahalli. Real time project based training
provided by working software professionals
having more than 10 years of experience.
Informatica Training in Bangalore
Informatica training in bangalore delivered by
a real time software expert having 10 years
of experience. 
Siebel CRM Training in Bangalore
Best software training institute for Siebel CRM
training in Marathahalli, Bangalore. 
Software testing training institute in
bangalore

http://venkatsqlinterview.blogspot.in/2011/05/what­is­use­of­coalesce­in­sql­server.html 1/5
12/24/2016 SQL Server Interview Questions: What is the use of COALESCE in SQL Server

Software testing training on real time projects
and placements. 
MSBI Training Institute in Bangalore
Best MSBI Training in Bangalore by an
expert. MSBI training on real time projects
and placements. 
Basic SQL Server Interview Questions

SQL Server Interview Questions on


Temporary Tables

SQL Server Interview Questions on Airtel Postpaid Plans


Indexes ‐ Part 1
Get MyPlan with Great Bene韛�ts and
SQL Server Interview Questions on Packs. Browse for Details.
Indexes ‐ Part 2 www.airtel.in/myplan

What is the difference between


Having and Where clause

What is the difference between a


Temporary Table and a Table Variable

What is the use of COALESCE in SQL


Server

SQL Server Interview Questions on


triggers

Difference between User Defined


Function and Stored Procedure

SQL Server Interview Questions on


Views ‐ Part 1

SQL Server Interview Questions on


Views ‐ Part 2

Basic SQL Server Interview Questions


on Joins
COALESCE can also be used in joins as shown in the example below. If the
Explain Inner Join with an example Candidate table has a non null value in the Email column, then the value is
selected. If the Email column is null in the Candidate Table then, CompanyEmail
Explain Left Outer Join with an from CandidateCompany Table is selected.
example

Explain Right Outer Join with an


example

Explain Full Outer Join with an


example

Explain Self Join with an example

What is the difference between Index


Scan and Index Seek

Write a SQL Query to delete from a


table that is involved in a SQL join

What are the advantages of using


stored procedures

What are the different ways to


replace NULL values in SQL Server
http://venkatsqlinterview.blogspot.in/2011/05/what­is­use­of­coalesce­in­sql­server.html 2/5
12/24/2016 SQL Server Interview Questions: What is the use of COALESCE in SQL Server

SQL Server interview questions on


string manipulation functions

Write a Stored Procedure that takes


column name as a parameter and
returns the result sorted by the
column that is passed

What is deferred name resolution in


SQL Server?

Recommend this on Google

9 comments:

Anonymous August 22, 2011 at 10:45 PM


Thanks venkat! this is the thing which i came across for the first
time now i will start make use of it in real time scenarios
Reply

Anonymous August 31, 2011 at 6:44 PM


yup realy great thanks from my side vijay raj gupta
Reply

kaku September 26, 2011 at 3:42 AM


but the problem is that it also displays null columns in that
table if some of the columns in having both primary and sec is
null.....
Reply

Harinath naidu March 31, 2013 at 8:51 PM


The best website ever for the concepts to understand in a solid
way.... Thank you Venkat.
Reply
http://venkatsqlinterview.blogspot.in/2011/05/what­is­use­of­coalesce­in­sql­server.html 3/5
12/24/2016 SQL Server Interview Questions: What is the use of COALESCE in SQL Server

Reply

gjk October 20, 2013 at 9:59 AM


COALESCE can also be used to concatenate multiple rows as a
single comma separated row.

declare @sample varchar(50)


select @sample = @sample + COALESCE(@sample, ',') + colname
from tablename
Reply

umairumair razzaq January 6, 2014 at 6:27 AM


Thanks
This way make easy in practice
Reply

Anonymous April 27, 2014 at 8:43 PM


COALESCE can also be used to concatenate multiple rows as a
single comma separated row use bellow query(SLIGHT MODIFIED
ABOVE QUERY)

DECLARE @Names VARCHAR(MAX)


SELECT @Names = COALESCE(@Names + ', ','') + NAME FROM
EMPLOYEE
SELECT @Names
Reply

Anonymous December 3, 2014 at 4:47 AM


Thanks Venkat..I have never use COALESCE as I was not aware
of it. but now I came across it through you article.
Reply

Dipali February 19, 2016 at 10:06 AM


Awesome explanation...toooooo gooooood..
Reply

http://venkatsqlinterview.blogspot.in/2011/05/what­is­use­of­coalesce­in­sql­server.html 4/5
12/24/2016 SQL Server Interview Questions: What is the use of COALESCE in SQL Server

Enter your comment...

Comment as:  Select profile...

Publish
  Preview

If you are aware of any other sql server questions asked in an


interview, please post them below. If you find anything missing or
wrong, please feel free to correct by submitting the form below.

Newer Post Home Older Post

Subscribe to: Post Comments (Atom)

 
Disclaimer ­ Terms of use ­ Contact Us

http://venkatsqlinterview.blogspot.in/2011/05/what­is­use­of­coalesce­in­sql­server.html 5/5

Vous aimerez peut-être aussi