Books MVC ASP.NET C# SQL Server WCF Written Test HR Round Subscribe SQL Tutorial C# Programs Buy DVD

SQL Server interview questions on string manipulation functions

The following 2 SQL Server Interview questions were asked when I attended an interview for SQL Server Developer role.

Can you list a few useful string manipulation functions in SQL Server?

Then he asked me, Can you give me one example of where you have used these functions in your experience?
The following is one simple real time example, where we can use LEN(), CHARINDEX() and SUBSTRING() functions. Let us assume we have table as shown below.

I want you to write a query to find out total number of emails, by domain. The result of the query should be as shown below.

We can use LEN(), CHARINDEX() and SUBSTRING() functions to produce the desired results. Please refer to the query below.

Select SUBSTRING(Email,CHARINDEX('@',Email)+1,(LEN(Email) - CHARINDEX('@',Email))) as EmailDomain, Count(*) as Total
From TableName
Group By SUBSTRING(Email,CHARINDEX('@',Email)+1,(LEN(Email) - CHARINDEX('@',Email)))
Order by Count(*) Desc

There could be even better ways of producing the same result. If you feel you have a better way of producing the same output, please share using the form below.


  1. Hi venkat your blog helped me a ton, i have a query that there is an employee table of empid, empname ,supervisor id

    how can i get empid and supervisor name

    1. This can be acheived by using self join, that is joining the table with same table. Consider we have tblEmployee as show below

      EmpID EmpName SupervisorID
      101 Navin 104
      102 Mahesh 101
      103 Bala 101
      104 Billy NULL

      select t1.EmpID, t2.EmpName SupervisorName from tblEmployee t1 JOIN tblEmployee t2 on t1.EmpID = t2.SupervisorID

    2. Above Query is wrong . Correct answer is
      Select t1.EmpID, t2.EmpName SupervisorName from tblEmployee t1 join tblEmployee t2
      On t1.SupervisorID=t2.EmpID

  2. select RIGHT(email,LEN(email)-CHARINDEX('@',email)) as EmailDomain,count(*) total
    from tablename
    group by RIGHT(email,LEN(email)-CHARINDEX('@',email))
    order by count(*) Desc

  3. select substring(email,charindex('@',email)+1len(email))
    ,count (*)
    from table_name
    group by substring(email,charindex('@',email)+1len(email))


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.

Disclaimer - Terms of use - Contact Us