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'
30). Get how many employee exist in "EmployeeDetail" table.
Ans: SELECT COUNT(*) FROM [EmployeeDetail]
Set-4: Sql Server Salary Interview Query (Salary
Related Queries)
Related Tables:-
31. Select only one/top 1 record from "EmployeeDetail" table.
Ans: SELECT TOP 1 * FROM [EmployeeDetail]
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
41. Select second highest salary from "EmployeeDetail" table.
Ans: SELECT TOP 1 Salary FROM
(SELECT TOP 2 Salary FROM [EmployeeDetail] ORDER BY Salary DESC) T ORDER BY Salar
yASC
Set-5: Sql Server Group By Interview Query
(Group By Related Queries)
Related Table:
QUESTIONS ANSWERS
42. Write the query to get the department and department wise total(sum) salary
from "EmployeeDetail" table.
Ans: SELECT Department, SUM(Salary) AS [Total Salary] FROM [EmployeeDetail]
GROUP BY Department
43. Write the query to get the department and department wise total(sum) salary,
display it in ascending order according to salary.
Ans: SELECT Department, SUM(Salary) AS [Total Salary] FROM [EmployeeDetail]
GROUP BY Department ORDER BY SUM(Salary) ASC
44. Write the query to get the department and department wise total(sum) salary,
display it in descending order according to salary.
Ans: SELECT Department, SUM(Salary) AS [Total Salary] FROM [EmployeeDetail]
GROUP BY Department ORDER BY SUM(Salary) DESC
45. Write the query to get the department, total no. of departments, total(sum) salary
with respect to department from "EmployeeDetail" table.
Ans: SELECT Department, COUNT(*) AS [Dept Counts], SUM(Salary) AS [Total
Salary] FROM[EmployeeDetail]
GROUP BY Department
46. Get department wise average salary from "EmployeeDetail" table order by salary
ascending
Ans: SELECT Department, AVG(Salary) AS [Average Salary] FROM [EmployeeDetail]
GROUP BY Department ORDER BY AVG(Salary) ASC
47
. Get department wise maximum salary from "EmployeeDetail" table order by salary
ascending
Ans: SELECT Department, MAX(Salary) AS [Average Salary] FROM [EmployeeDetail]
GROUP BY Department ORDER BY MAX(Salary) ASC
48. Get department wise minimum salary from "EmployeeDetail" table order by
salary ascending
Ans: SELECT Department, MIN(Salary) AS [Average Salary] FROM [EmployeeDetail]
GROUP BY Department ORDER BY MIN(Salary) ASC
--
USE OF HAVING
49. Write down the query to fetch Project name assign to more than one Employee
Ans: Select ProjectName,Count(*) [NoofEmp] from [ProjectDetail] GROUP BY ProjectNa
meHAVING COUNT(*)>1
Set-6: Sql Server Join Interview Query
(Join Related Queries)
Related Tables:
SQL JOINS RELATED INTERVIEW QUERIES
51. Get employee name, project name order by firstname from "EmployeeDetail" and
"ProjectDetail" for those employee which have assigned project already.
Ans: SELECT FirstName,ProjectName FROM [EmployeeDetail] A INNER JOIN [ProjectDeta
il]B ON [Link] = [Link] ORDER BY FirstName
52. Get employee name, project name order by firstname from "EmployeeDetail" and
"ProjectDetail" for all employee even they have not assigned project.
Ans: SELECT FirstName,ProjectName FROM [EmployeeDetail] A LEFT OUTER JOIN[Projec
tDetail] B ON [Link] = [Link] ORDER BY FirstName
53(35.1) Get employee name, project name order by firstname from
"EmployeeDetail" and "ProjectDetail" for all employee if project is not assigned then
display "-No Project Assigned".
Ans: SELECT FirstName, ISNULL(ProjectName,'-No Project
Assigned') FROM[EmployeeDetail] A LEFT OUTER JOIN [ProjectDetail] B
ON [Link] = [Link] ORDER BY FirstName
54. Get all project name even they have not matching any employeeid, in left table,
order by firstname from "EmployeeDetail" and "ProjectDetail".
Ans: SELECT FirstName,ProjectName FROM [EmployeeDetail] A RIGHT OUTER JOIN[Proj
ectDetail] B ON [Link] = [Link] ORDER BY FirstName
55. Get complete record (employeename, project name) from both tables
([EmployeeDetail],[ProjectDetail]), if no match found in any table then show NULL.
Ans: SELECT FirstName,ProjectName FROM [EmployeeDetail] A FULL OUTER JOIN[Projec
tDetail] B ON [Link] = [Link] ORDER BY FirstName
56. Write a query to find out the employeename who has not assigned any project,
and display "-No Project Assigned"( tables :- [EmployeeDetail],[ProjectDetail]).
Ans: SELECT FirstName, ISNULL(ProjectName,'-No Project
Assigned') AS [ProjectName]FROM [EmployeeDetail] A LEFT OUTER JOIN [ProjectDetail]
B ON [Link] =[Link]
WHERE ProjectName IS NULL
57. Write a query to find out the project name which is not assigned to any employee(
tables :- [EmployeeDetail],[ProjectDetail]).
Ans: SELECT ProjectName FROM [EmployeeDetail] A RIGHT OUTER JOIN [ProjectDetail]
B [Link] = [Link]