Tuesday, May 26, 2026

Top SQL Queries for Practice (With Short Answers) - Aggregate Queries (16–30)

 

Aggregate Queries (16–30)

  1. Count total employees
SELECT COUNT(*) FROM Employee;
  1. Count employees in HR
SELECT COUNT(*) FROM Employee WHERE department = 'HR';
  1. Find total salary payout
SELECT SUM(salary) FROM Employee;
  1. Find average salary
SELECT AVG(salary) FROM Employee;
  1. Find highest salary
SELECT MAX(salary) FROM Employee;
  1. Find lowest salary
SELECT MIN(salary) FROM Employee;
  1. Count employees by department
SELECT department, COUNT(*) FROM Employee GROUP BY department;
  1. Find avg salary by department
SELECT department, AVG(salary) FROM Employee GROUP BY department;
  1. Find highest salary by department
SELECT department, MAX(salary) FROM Employee GROUP BY department;
  1. Find departments with more than 5 employees
SELECT department, COUNT(*) 
FROM Employee
GROUP BY department
HAVING COUNT(*) > 5;
  1. Find departments with avg salary > 60000
SELECT department, AVG(salary)
FROM Employee
GROUP BY department
HAVING AVG(salary) > 60000;
  1. Count distinct departments
SELECT COUNT(DISTINCT department) FROM Employee;
  1. Find total salary by department
SELECT department, SUM(salary) FROM Employee GROUP BY department;
  1. Find total employees in each location
SELECT location, COUNT(*) FROM Employee GROUP BY location;
  1. Find max salary where department = IT
SELECT MAX(salary) FROM Employee WHERE department = 'IT';