Set 4 & 5 Practice Sheet
Set 4 & 5 Practice Sheet
25). Get the first name, current date, joiningdate and diff between current date and
joining date in months.
Ans: SELECT FirstName, GETDATE() [Current Date], JoiningDate,
DATEDIFF(MM,JoiningDate,GETDATE()) AS [Total Months] FROM [EmployeeDetail]
26). Get the first name, current date, joiningdate and diff between current date and
joining date in days.
Ans: SELECT FirstName, GETDATE() [Current Date], JoiningDate,
DATEDIFF(DD,JoiningDate,GETDATE()) AS [Total Months] FROM [EmployeeDetail]
27). Get all employee details from EmployeeDetail table whose joining year is 2013.
Ans: SELECT * FROM [EmployeeDetail] WHERE DATEPART(YYYY,JoiningDate) = '2013'
28). Get all employee details from EmployeeDetail table whose joining month is
Jan(1).
Ans: SELECT * FROM [EmployeeDetail] WHERE DATEPART(MM,JoiningDate) = '1'
29). Get all employee details from EmployeeDetail table whose joining date between
"2013-01-01" and "2013-12-01".
Ans: SELECT * FROM [EmployeeDetail] WHERE JoiningDate BETWEEN '2013-01-
01' AND '2013-12-01'
32. Select all employee detail with First name "Vikas","Ashish", and "Nikhil".
Ans: SELECT * FROM [EmployeeDetail] WHERE FirstName IN('Vikas','Ashish','Nikhil')
33. Select all employee detail with First name not in "Vikas","Ashish", and "Nikhil".
Ans: SELECT * FROM [EmployeeDetail] WHERE FirstName NOT IN('Vikas','Ashish','Nikhil')
34. Select first name from "EmployeeDetail" table after removing white spaces from
right side
Ans: SELECT RTRIM(FirstName) AS [FirstName] FROM [EmployeeDetail]
35. Select first name from "EmployeeDetail" table after removing white spaces from
left side
Ans: SELECT LTRIM(FirstName) AS [FirstName] FROM [EmployeeDetail]
36. Display first name and Gender as M/F.(if male then M, if Female then F)
Ans: SELECT FirstName, CASE WHEN Gender = 'Male' THEN 'M'
WHEN Gender = 'Female' THEN 'F' END AS [Gender]
FROM [EmployeeDetail]
37. Select first name from "EmployeeDetail" table prifixed with "Hello "
Ans: SELECT 'Hello ' + FirstName FROM [EmployeeDetail]
38. Get employee details from "EmployeeDetail" table whose Salary greater than
600000
Ans: SELECT * FROM [EmployeeDetail] WHERE Salary > 600000
39. Get employee details from "EmployeeDetail" table whose Salary less than 700000
Ans: SELECT * FROM [EmployeeDetail] WHERE Salary < 700000
40. Get employee details from "EmployeeDetail" table whose Salary between 500000
than 600000
Ans: SELECT * FROM [EmployeeDetail] WHERE Salary BETWEEN 500000 AND 600000