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

What will be the output of below query? Select Stuff('Vishal',2, 3,'Neeraj');

NOTE: This is objective type question, Please click question title for correct answer.
What is the difference between Now() and Current_Timestamp() function in MySql?

There is NO difference between Now() and Current_Timestamp() function in MySql.
Both produces the same output as Current Date and Time as the system date as a string in the form of "YYYY-MM-DD HH:MM:DD".

For ex:-

Select Now() as 'Current Time';
Select Current_Timestamp() as 'Current Time';
What will be the output of below query? Select 1;

NOTE: This is objective type question, Please click question title for correct answer.
What will be the output of below query? Select null from employee_master; If employee_master have 25 rows.

NOTE: This is objective type question, Please click question title for correct answer.
Will below query return records? Select * from employee_master where 1 = 1; If employee_master have only 25 rows.

Yes,It will definitely return all records as 25 records are there in employee_master.Because select query filters rows based on where clause.If condition is true otherwise will not return any record if condition is false.

Here 1=1 means condition is true.That's why it will return all records.
What will be the output of below query? Select * from employee_master where 1 <> 1; If employee_master have only 25 rows.

It will Not return any record as condition is false.Because select query filters rows based on where clause we pass in select query.If condition is true it returns rows otherwise does not return any record.

Here 1<>1 means condition is false.That's why it will not return any row.
What will be the output of below query? Select * from employee_master where employee_code = null;

It will NOT return any records.Because Equal(=) sign is a comparison operator used with string/integer/double/decimal or any other data-types BUT not with NULL values.

Because Null means no data or absence of data.
If we want to fetch rows,then we have to modify query as

Select * from employee_master where employee_code Is Null;

Then it will fetch record which have NULL values.
What is the difference between JOIN and UNION?

SQL JOIN allows us to fetch records on other table based on the given conditions between two tables.

For example,if we have the Employee ID of each employee, then we can use this Employee ID of the employee table to join with the Employee ID of Project master table to fetch employe details.

UNION operation allows us to add 2 similar data sets to create resulting data set that contains all the data from the source data sets.Union does not require any condition for joining.

For example, if you have 2 employee tables with same structure, you can UNION them to create one result set that will contain all the records from both of the tables.

Join example:-

Suppose we have 2 tables i.e. Employee_Master and Project_Master.

Join Example:-

Select emp.employee_name,emp.address,emp.ph_no from employee_master emp

Join
project_master prj
on emp.employee_id = prj.emp_id and prj.status = 'AA'
where emp.status = 'AA';


Union Example:-

Select * from table_name1

Union
Select * from table_name2

How to fetch records from employee_master where dept_no = 20 or 50 and employees are active?

select * from employee_master 

where (dept_no = 20 or dept_no = 50)
and status = 'ÁA';


Suppose,employee_master contains following column and rows:-

Id      Name    Address   Dept_No    Status


1 Vishal Pune 10 AA

2 Neeraj NULL 20 DD

3 Nitin Nagpur 30 AA

4 Rajesh Jodhpur 20 AA

5 Pooja Nagpur 50 AA

6 Praveen Bhilai 20 AA

7 Vinod NULL 50 DD


Here,Status ='AA' means active employee and 'DD' means De-active employees.

And if i fire above query,then i will get below output as:-

Id      Name    Address   Dept_No    Status


4 Rajesh Jodhpur 20 AA

5 Pooja Nagpur 50 AA

6 Praveen Bhilai 20 AA

How to check which Database is a current database?

With the help of DB_NAME() in-built Sql-Server function,we can check which DB is currently selected.

Below is a query to check DB:-
SELECT DB_NAME() AS [Current Database];

What will be the Output of below query? Select iif(2>=2,null,'2>2') as 'output';

NOTE: This is objective type question, Please click question title for correct answer.
What will be the output of below query? Select Difference('Green','Greene') as 'Number';

NOTE: This is objective type question, Please click question title for correct answer.
How to get value as capital letter in Sql-Server.

NOTE: This is objective type question, Please click question title for correct answer.
Differences between ISNULL and NULLIF ?

This is one of the Sql server interview question which I found interesting. So,
I am sharing that here.

ISNULL and NULLIF accepts 2 parameters:

ISNULL: Returns parameter2 if Paremeter1 IS NULL. Else, it retunrs Parameter1



NULLIF: Returns parameter2 if Paremeter1 =parameter2 . Else, it retunrs Parameter1



Here is the complete video which shows differences between isnull and nullif


http://www.dotnetfunda.com/videos/show/264/differences-between-isnull-and-nullif-in-sql-server
How to Search particular Text in Sql-Server.

With the help of CharIndex() sql-serverfunction,we can search any Text inside any String.

For Example:-

CHARINDEX('Vishal','Vishal Kumar');


Here,1st argument is our Search criteria and 2nd argument is our String to which charindex will find text.
Should Output parameter be returned from Stored Procedure?

No,It's not required.
As stored procedure may or may not return any value.
and output parameters work like reference variable as Out and Ref in Dot Net.It stores the value and is similar like return type in functions/methods.

Output parameters are defined as:-
Create Procedure sp_get_value(@result varchar(50) Output)
As
Go
How many ways we can pass parameters to Stored Procedures?

In SQL Server,there are two ways,we can pass parameters to Stored Procedures.

1).InPut parameter -> These types of parameters are used to send values to stored procedures.

2).OutPut parameter -> These types of parameters are used to get values from stored procedures.
What will be the output of below query? Select Choose(1,'Vishal','Kumer','Neeraj','Rajesh')

NOTE: This is objective type question, Please click question title for correct answer.
How to retrieve all the column names from table?

It's very simple and easy way to get all the column names the Table has.

Write below query:-

Select Top 0 * From Employee_Master;


Top 0 * will no fetch any rows only columns will display.
How to Convert Integer into Varchar in Sql-Server Database?

With the help of Cast method or function,we can convert Integer into Varchar.

For Example:-

Declare @Id int = 1;
Select Cast(@Id as Varchar) as Id;
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