-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathportfolio_select_examples.sql
More file actions
100 lines (93 loc) · 2.47 KB
/
portfolio_select_examples.sql
File metadata and controls
100 lines (93 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
-- SQL Query Examples
-- Basic queries for analyzing employee and project data
-- Query 1: Find all Sales employees making over $50,000
SELECT
employee_id,
first_name,
last_name,
email,
salary
FROM employees
WHERE department = 'Sales'
AND salary > 50000
ORDER BY salary DESC;
-- Query 2: List employees with their department names
SELECT
e.employee_id,
e.first_name,
e.last_name,
d.department_name,
e.hire_date
FROM employees e
INNER JOIN departments d ON e.department_id = d.department_id
WHERE e.hire_date >= '2023-01-01';
-- Query 3: Count employees and average salary by department
SELECT
department_id,
COUNT(*) AS employee_count,
AVG(salary) AS avg_salary,
MAX(salary) AS max_salary,
MIN(salary) AS min_salary
FROM employees
GROUP BY department_id
HAVING COUNT(*) > 5
ORDER BY avg_salary DESC;
-- Query 4: Find employees earning above company average
SELECT
employee_id,
first_name,
last_name,
salary
FROM employees
WHERE salary > (
SELECT AVG(salary)
FROM employees
)
ORDER BY salary DESC;
-- Query 5: Show employees assigned to active projects
SELECT
e.first_name,
e.last_name,
d.department_name,
p.project_name,
DATEDIFF(p.end_date, p.start_date) AS project_duration_days
FROM employees e
INNER JOIN departments d ON e.department_id = d.department_id
LEFT JOIN employee_projects ep ON e.employee_id = ep.employee_id
LEFT JOIN projects p ON ep.project_id = p.project_id
WHERE p.status = 'Active';
-- Query 6: Categorize employees by salary level
SELECT
employee_id,
first_name,
last_name,
salary,
CASE
WHEN salary < 40000 THEN 'Entry Level'
WHEN salary BETWEEN 40000 AND 70000 THEN 'Mid Level'
WHEN salary BETWEEN 70001 AND 100000 THEN 'Senior Level'
ELSE 'Executive Level'
END AS salary_grade
FROM employees
ORDER BY salary;
-- Query 7: Compare employee salaries to their department average
WITH DepartmentStats AS (
SELECT
department_id,
COUNT(*) AS emp_count,
AVG(salary) AS avg_dept_salary
FROM employees
GROUP BY department_id
)
SELECT
e.first_name,
e.last_name,
e.salary,
d.department_name,
ds.avg_dept_salary,
ROUND((e.salary / ds.avg_dept_salary) * 100, 2) AS pct_of_avg
FROM employees e
INNER JOIN departments d ON e.department_id = d.department_id
INNER JOIN DepartmentStats ds ON e.department_id = ds.department_id
WHERE e.salary > ds.avg_dept_salary
ORDER BY pct_of_avg DESC;