-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweek_9.sql
More file actions
99 lines (63 loc) · 1.86 KB
/
Copy pathweek_9.sql
File metadata and controls
99 lines (63 loc) · 1.86 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
--Display the distinct emp ids and first names of employees who have joined their job in the month of June using a subquery
SELECT DISTINCT emp_id, first_name
FROM employees
WHERE emp_id IN (
SELECT emp_id
FROM employees
WHERE MONTH(join_date) = 6
);
--Design a subquery to select the first name of emp with the maximum value of emp id
SELECT first_name
FROM employees
WHERE emp_id = (
SELECT MAX(emp_id)
FROM employees
);
--Display all emp details who work for atleast one dept
SELECT *
FROM emp e
WHERE EXISTS (
SELECT 1
FROM dept d
WHERE d.emp_id = e.emp_id
);
--Display all emp names taking highest salary using subquery.
SELECT emp_name
FROM emp
WHERE salary = (
SELECT MAX(salary)
FROM emp
);
--Display all emp names and ids whose job name is same as Smith’s job title
SELECT emp_id, emp_name
FROM emp
WHERE job = (
SELECT job
FROM emp
WHERE ename = 'Smith'
);
--Retrieve ename, sal, deptno of all emp’s who are taking minimum salary in each dept, display your result according to deptnum.
WITH RankedEmp AS (
SELECT ename, sal, deptno, RANK() OVER (PARTITION BY deptno ORDER BY sal) AS rnk
FROM emp
)
SELECT ename, sal, deptno
FROM RankedEmp
WHERE rnk = 1
ORDER BY deptno;
--Retrieve all emp numbers, salary and names who are taking salary greater than the employees working as ‘CLERK’ but not working as ‘CLERK’
SELECT empno, sal, ename
FROM emp
WHERE sal > (
SELECT MAX(sal)
FROM emp
WHERE job = 'CLERK'
) AND job <> 'CLERK';
--Retrieve all emp numbers, salary and names whose salary is greater than the average salary paid by each dept. (You may use groub by)
SELECT empno, sal, ename
FROM emp
WHERE sal > (
SELECT AVG(sal)
FROM emp e2
WHERE e2.deptno = emp.deptno
)