GROUP BY : SQL INTERVIEW QUERIES QUESTIONS AND ANSWERS WITH EXAMPLE FOR EXPERIENCED SET-5

SQL GROUP BY & HAVING INTERVIEW QUERIES This is 5th post related to SQL interview queries with examples. In this post we will discu...

SQL GROUP BY & HAVING INTERVIEW QUERIES
This is 5th post related to SQL interview queries with examples. In this post we will discuss most important SQL server queries, which is related to "Group by" keyword. Group by related queries is most frequently asked in all interview. This post contains questions for both fresher and experienced developers.
About GROUP BY:-
The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns.

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 ProjectName HAVING COUNT(*)>1


Click here for next set(MORE THAN 100 QUERIES)

Related

SQL SERVER 4505267375025242603

Post a comment

  1. Thanks a lot however for me it's very useful

    ReplyDelete
  2. Thanks a lot however for me it's very useful

    ReplyDelete
  3. very helpful

    ReplyDelete
  4. Seems 47 and 48 needs a small correction - Shouldn't the column names be Maximum Salary, Minimum Salary instead of Average Salary..??

    ReplyDelete
  5. hi friends i have interview at 7th on Group By, Joins , Stored Procedures, Triggers, Views these topics so plz could you share the interview questions. i already search some site but i want more because i need this job...

    ReplyDelete
  6. It was very Helpful! thanks!

    ReplyDelete
  7. Very helpful..Thanks alot ..:-)

    ReplyDelete
  8. there is a slary column in salary table and location column is in bank table and empid column in emp table then how to write query in sql resulting the data of total salary of an employee based on branch wise

    ReplyDelete
  9. thank you
    its very usefull to me

    ReplyDelete

emo-but-icon

Follow Us

Recent

Comments

Donate

Side Ads

Text Widget

Connect Us

Sql Server 2017 Interview Questions
Sql Server Basic Interview Query Set-1
Sql Server Basic Interview Query Set-2
Sql Server Date-Time Interview Query SET-3
Sql Server Salary Interview Query SET-4
Sql Server Group By Interview Query SET-5
Sql Server Join Interview Query SET-6
Sql Server Tricky Join Interview Query SET-7
Sql Server DDL Interview Query SET-8
Small but very Tricky Sql Server Query SET-9
Very Much Tricky Query (not 4 fresher)SET-10
Sql Server Complex Interview Query SET-11
Sql Server Datatype Interview Questions
Sql Server View Interview Questions
Sql Server Index Interview Questions
TCS Tricky Sql Interview Queries
HCL Sql Interview Queries
Sql Server SP Interview Questions
Sql Server Trigger Interview Questions
Sql Server Temp Table Interview Questions
Sql Server 2016 Interview Questions
Sql Server Performance Tuning Interview Q.
Sql Server Constraints Interview Questions
Sql Server Storage/Size Interview Questions
Sql Server Very Basic Interview Questions
Sql Server Quiz for Fresher
Sql Server Icon Quiz for All
Sql Server 300+ Theoretical Interview Questions
Sql Server Complete Set

ASP.NET AND JQUERY INTERVIEW QUESTION
ASP.NET: Web Config Interview Question
ASP.NET: View State Interview Question
ASP.NET: Session Interview Question
ASP.NET: Session Interview Question 
ASP.NET: Security Interview Question
ASP.NET: Catching Interview Question
C# OOPS Interview Question
MORE Interview Question
JQUERY: Interview Question Set-1
JQUERY: Interview Question Set-2
JAVA-SCRIPT: Interview Question Set-1
JAVA-SCRIPT: Interview Question Set-2
ASP.NET MVC: Interview Question Set-1

POPULAR POSTS
10 MOST DIFFICULT INTERVIEW QUESTIONS
5 BEST INTERVIEW TIPS
YOUR 10 MISTAKE DURING INTERVIEW
2016 TOP 20 GROUP DISCUSSION TOPICS
HOW SQL PERFORMANCE AFFECTED BY DATATYPE
item