Mircoproject Queries
Mircoproject Queries
Salaries Table
INSERT INTO Salaries (EmployeeID, SalaryAmount, EffectiveDate, EndDate,
Bonus, OvertimeHours, OvertimePay, Deductions, TaxWithheld,
PensionContribution, InsuranceContribution, SalaryType, Currency,
IsActive)
VALUES
(1, 75000.00, '2015-06-01', '2016-06-01', 5000.00, 10, 500.00, 2000.00,
1500.00, 3000.00, 1200.00, 'Yearly', 'USD', TRUE),
(2, 90000.00, '2016-01-10', '2017-01-10', 7000.00, 8, 400.00, 2500.00,
1700.00, 4000.00, 1500.00, 'Yearly', 'USD', TRUE),
(3, 85000.00, '2017-02-15', '2018-02-15', 6000.00, 12, 600.00, 2200.00,
1600.00, 3500.00, 1400.00, 'Yearly', 'USD', TRUE),
(4, 95000.00, '2018-05-20', '2019-05-20', 8000.00, 15, 750.00, 2800.00,
1900.00, 5000.00, 1800.00, 'Yearly', 'USD', TRUE),
(5, 80000.00, '2015-07-15', '2016-07-15', 5500.00, 5, 250.00, 2100.00,
1700.00, 3200.00, 1300.00, 'Yearly', 'USD', TRUE),
(6, 60000.00, '2016-08-10', '2017-08-10', 3000.00, 20, 1000.00, 1800.00,
1200.00, 2500.00, 1000.00, 'Yearly', 'USD', TRUE),
(7, 72000.00, '2016-09-01', '2017-09-01', 4500.00, 10, 500.00, 2100.00,
1600.00, 3000.00, 1200.00, 'Yearly', 'USD', TRUE),
(8, 68000.00, '2017-03-10', '2018-03-10', 4200.00, 15, 750.00, 2000.00,
1500.00, 2700.00, 1100.00, 'Yearly', 'USD', TRUE),
(9, 62000.00, '2018-04-15', '2019-04-15', 3800.00, 18, 900.00, 2200.00,
1300.00, 2600.00, 1000.00, 'Yearly', 'USD', TRUE),
(10, 77000.00, '2016-07-01', '2017-07-01', 5500.00, 10, 500.00, 2300.00,
1700.00, 3100.00, 1200.00, 'Yearly', 'USD', TRUE);
QUERIES:
1. Select all employees
SELECT * FROM Employees;
2. Select employees' first and last names only
SELECT FirstName, LastName FROM Employees;
3. Select all distinct positions in the company
SELECT DISTINCT Position FROM Employees;
4. Count the total number of employees.
SELECT COUNT(*) AS TotalEmployees FROM Employees;
5. Count the number of employees in each department
SELECT DepartmentID, COUNT(*) AS NumberOfEmployees FROM Employees GROUP
BY DepartmentID;
6. List employees who are managers (ManagerID is NULL)
SELECT * FROM Employees WHERE ManagerID IS NULL;
7. List all employees hired after 2016
SELECT * FROM Employees WHERE HireDate > '2016-01-01';
8. Get employees whose nationality is American
SELECT * FROM Employees WHERE Nationality = 'American';
9. Get employees with a salary greater than $70,000
SELECT e.FirstName, e.LastName, s.SalaryAmount
FROM Employees e
JOIN Salaries s ON e.EmployeeID = s.EmployeeID
WHERE s.SalaryAmount > 70000;
10. Find the average salary of all employees
SELECT AVG(SalaryAmount) AS AverageSalary FROM Salaries;
11. Get the total bonuses given to employees
SELECT SUM(Bonus) AS TotalBonuses FROM Salaries;
29. Find the number of employees in each salary bracket (e.g., $50K–$70K,
$70K–$90K)
SELECT
CASE
WHEN SalaryAmount BETWEEN 50000 AND 70000 THEN '50K-70K'
WHEN SalaryAmount BETWEEN 70001 AND 90000 THEN '70K-90K'
ELSE '90K+'
END AS SalaryRange,
COUNT(*) AS NumberOfEmployees
FROM Salaries
GROUP BY SalaryRange;
30. Find employees who joined before 2017 and earn more than $80,000
SELECT e.FirstName, e.LastName, s.SalaryAmount
FROM Employees e
JOIN Salaries s ON e.EmployeeID = s.EmployeeID
WHERE e.HireDate < '2017-01-01' AND s.SalaryAmount > 80000;
31. List employees whose salary has increased over time
SELECT e.FirstName, e.LastName, s.SalaryAmount, s.EffectiveDate
FROM Employees e
JOIN Salaries s ON e.EmployeeID = s.EmployeeID
WHERE s.EndDate IS NOT NULL
ORDER BY e.EmployeeID, s.EffectiveDate;
32. Get the total tax withheld from all employees
SELECT SUM(TaxWithheld) AS TotalTaxWithheld FROM Salaries