Sql Server Interview Questions and Answers (1758) - Page 61

Which Sql statement would you use to select all employees whose employee Id ranging 100 to 200;

NOTE: This is objective type question, Please click question title for correct answer.
Which Alternate Sql statement would you use to select all employees whose employee Id ranging 100 to 200 instead of Between operator?

We will use Comparison Operator like Less than(<) and Greater than (>) operator instead of Between Logical operator as

select emp_id,employee_name 

from employee_master
where emp_id >= 100 and emp_id <= 200;

Above query will give us the same result as Between gives.
Which Sql statement would you use to select all employees who are Software Engg,B.A. Or Programmer?

NOTE: This is objective type question, Please click question title for correct answer.
What is an alternate way of IN operator to query for selecting all employees who are Software Engg,B.A. Or Programmer?

OR operator is used to achieve the same thing as IN operator does.
Select emp_id,employee_name 

from employee_master
where designation = 'Software Engg'
OR designation = 'B.A.'
OR designation = 'Programmer';

What will happen if we run below queries? Select * From sgt_person where person_id in(1,2); Select * From sgt_person where person_id in('1','2'); Declare @emp_ids varchar(100) = '1,2'; Select * From sgt_person where person_id in(@emp_ids);

It will give records for 1st and 2nd queries but for 3rd query,it will generate error saying that

Msg 245, Level 16, State 1, Line 5
Conversion failed when converting the varchar value '1,2' to data type int.


1,2 will be treated as single string value and when passed in IN clause will give error as above.
What will happen if we execute below Sql Statement? Declare @value int = '10,20'; Select @value as output

If we execute above statement,then we will get following error:-

Msg 245, Level 16, State 1, Line 1

Conversion failed when converting the varchar value '10,20' to data type int.


Above statement states that we can not store any datatype value or character other than numeric value in Int Datatype.
Or can not convert varchar value which contains comma or special characters into Integer.
How to check whether Table is present or not in the Database?

We will write below query to check Table presence:-
If Object_Id('Table_Name','U') Is Not Null

Print 'Table Exists';
Else
Print 'Table does not Exists';

Here,U is User Table.
How to check running Sql queries in Sql Server?

Write below query to check which Sql Statement is running
Select [Text] as Sql_Statement,Status as [Running_Info]

From sys.dm_exec_requests
Cross Apply sys.dm_exec_sql_text(sql_handle)

How to get only Website Name through query?

Write below query to get only website name
declare @Url as nvarchar(1000) = 'http://www.dotnetfunda.com/interviews/postexamquestion.aspx?';

select substring(replace(@Url,'http://www.',''),0,charindex('/',replace(@Url,'http://www.',''))) as [Website Only]

Output:-
dotnetfunda.com

Which statement is used for defining a Cursor?

NOTE: This is objective type question, Please click question title for correct answer.
Can a Clustered Index ALSO be a Unique Index?

Yes ,If no other column except primary key column is defined as non-clustered index.
What is the FEATURES or PURPOSE of Raise Error?

NOTE: This is objective type question, Please click question title for correct answer.
Which of the following CANNOT be passed back as an output parameter from a stored procedure?

NOTE: This is objective type question, Please click question title for correct answer.
Found this useful, bookmark this page to the blog or social networking websites. Page copy protected against web site content infringement by Copyscape

 Interview Questions and Answers Categories