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

What is the use of USE keyword in Sql Server Or MySql Database?

The USE keyword is used for selecting any Database in MySQL and SQL Server.The syntax is as follows:-
USE "database_name"

Suppose,we have so many database and we want to query on specific database then we will write above syntax then we can fire any select statement.
How to Concatenate two strings in Sql Server?

We can use Concat in-built Sql Server function to append 2 string values.
For Example:-
Select Concat('Vishal', ' ','Kumar') As Name;

Output:-
Vishal Kumar

What is an alternate way of appending or concatenating strings in Sql Server.

We can use + sign to append string values in Sql Server.

For Example:-
Select ('Vishal' + ' ' + 'Kumar') As Name;

Output:-
Vishal Kumar

Which Sql statement would you use to select all employees whose first name starts with 'F'?

NOTE: This is objective type question, Please click question title for correct answer.
Which Sql statement would you use to delete the row for employee id = 10?

NOTE: This is objective type question, Please click question title for correct answer.
Which Sql statement would you use to select all employees whose last name ends with 'F'?

NOTE: This is objective type question, Please click question title for correct answer.
What will happen when we fire below query? Select ID,Name from Table_name order by 3;

It will give us error saying that:-
Msg 108, Level 16, State 1, Line 1
The ORDER BY position number 3 is out of range of the number of items in the select list.


Meaning that as we know that we can also access column by their's index position.So here Index 1 is Id column,index 2 is Name column but in order by clause we have passed 3 that is also an index position,but in select statement there are only 2 columns.That's why it will give an error.So to overcome this error,we have to pass number of column index as specified in select list.
What will happen when we fire below query? Select ID,Name from Table_name order by 2;

It will successfully give result and will set order by with Name field as we have passed 2 in order by clause and as in the select list Id has 1st index position and Name has 2nd index position.That's why query will sort based on Name column.
What will happen if we execute below query?Will it compile or will it give the output? Select created_date,* From Employee_Master e order by Emp_Id,Created_Date;

Above query will give us error as:-
Msg 209, Level 16, State 1, Line 1
Ambiguous column name 'Created_Date'.


Ambiguous column mean in above query we have specified created_date twice in select list and also provided alias name for table 'Employee_Master'.And also Table is searching for 2 Created_Date columns.That's why it's showing as Ambiguous column name.

So,to overcome this problem,we must specify alias name.column name as:-
Select e.created_date,* From Employee_Master e order by Emp_Id,e.Created_Date;

//OR
Select created_date,* From Employee_Master e order by Emp_Id,e.Created_Date;


Now,above query will work.
What will happen if we execute below queries? Select emp_id,employee_name from employee_master e inner join employee_address ea on e.emp_id = ea.emp_id; Select emp_id,emp_id,employee_name from employee_master e inner join employee_address ea on e.emp_id = ea.emp_id;

It will give us error as Ambiguous column name 'emp_id'.Because 'emp_id' is present on both tables i.e. employee_master and employee_address table that's why when we execute above queries,tables will confuse which emp_id is refering to which Tables,then it will say Ambiguous column name or duplicate column name.

To overcome this problem just provide alias name in columns as below:-
Select e.emp_id,employee_name from employee_master e

inner join employee_address ea
on e.emp_id = ea.emp_id;

Select e.emp_id,ea.emp_id,employee_name from employee_master e
inner join employee_address ea
on e.emp_id = ea.emp_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