Note : All sql related question related to SQL Server.
(1) How to get current Age based on BirthDate ?
SELECT top 5 BirthDate, DATEDIFF ( YEAR ,BirthDate , getdate() ) as age,LoginID
FROM [HumanResources].[Employee]
(2) What is the output when you concate two null value using plus '+' operator ? SELECT NULL + NULL;
Ans : It will resturn single null value.
(3) What is the output when you concate two null value using CONCATE function ?
SELECT CONCAT (NULL, NULL)
Ans : It will resturn empty string/ blank value.
(4) What is the output of the following code ?
DECLARE @TempVariable AS NVARCHAR(50)
SET @TempVariable = 'Hello World'
PRINT @TempVariable
Ans : "Hello World"
(5) What is the output of the following code ?
DECLARE @TempVariable AS NVARCHAR(50)
SET @TempVariable = 'Hello World'
GO
PRINT @TempVariable
Ans : It will gices error. - Must declare the scalar variable "@PurchaseName".
Reason is local variable scope expires at the end of the batch once "Go" statement came.
(6) Teacher table have 6 records, contain LastName valiues as shown in bellow.
What is the output of the following code ?
declare @tempLastName nvarchar(50);
select @tempLastName = LastName from [EF_Demo_DB].[dbo].Teacher;
select @tempLastName;
Ans : Jena
Explanation : Value of LastName from last record will be set in local variable @tempLastName.
(7) What is output of following code snipet.
print null;
Ans : Blank
Explanation : We cant print null value in sql server.
(8) What is output of following code snipet.
Declare @localvariable nvarChar(50) = NULL;
PRINT @localvariable + 'Tech for Boost';
Ans : Blank
Explanation : concate with null value return blank value.
(9) What is output of following code snipet.
declare @localvariable nvarchar(50) = NULL;
PRINT 'Tech for Boost' + @localvariable;
Ans : Blank
Explanation : concate with null value return blank value.
Null value containted variable in concation using '+' operator is first or second pposition does not matter.
but concation of null value using CONCAT function gives different result. Let's see with example.
(10) What is output of following code snipet.
declare @localvariable nvarchar(50) = NULL;
print CONCAT('Tech for Boost', @localvariable);
-- or
print CONCAT(@localvariable, 'Tech for Boost');
Ans : Tech for Boost
(11) Can we use GROUP BY clause without aggregate function ?
Ans : Yes, let's see with example. We have table EmployeeSalary