-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcorrelated subquery.sql
More file actions
46 lines (39 loc) · 1.4 KB
/
correlated subquery.sql
File metadata and controls
46 lines (39 loc) · 1.4 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
-- correalted subquery:
-- is a subquery that depends on outer query
-- it is executive repeatatively based on the values of outer query
-- Correlated subquery example
create database regexcorr;
use regexcorr;
CREATE TABLE employee (
eid INT PRIMARY KEY,
ename VARCHAR(50),
salary INT,
department_name VARCHAR(50)
);
INSERT INTO employee (eid, ename, salary, department_name) VALUES
(1, 'Emp1', 30000, 'HR'),
(2, 'Emp2', 32000, 'HR'),
(3, 'Emp3', 34000, 'HR'),
(4, 'Emp4', 36000, 'HR'),
(5, 'Emp5', 38000, 'HR'),
(6, 'Emp6', 50000, 'IT'),
(7, 'Emp7', 52000, 'IT'),
(8, 'Emp8', 54000, 'IT'),
(9, 'Emp9', 56000, 'IT'),
(10, 'Emp10', 58000, 'IT'),
(11, 'Emp11', 60000, 'Finance'),
(12, 'Emp12', 62000, 'Finance'),
(13, 'Emp13', 64000, 'Finance'),
(14, 'Emp14', 66000, 'Finance'),
(15, 'Emp15', 68000, 'Finance'),
(16, 'Emp16', 40000, 'Sales'),
(17, 'Emp17', 42000, 'Sales'),
(18, 'Emp18', 44000, 'Sales'),
(19, 'Emp19', 46000, 'Sales'),
(20, 'Emp20', 48000, 'Sales');
select * from employee;
select department_name, min(salary) from employee group by department_name;
select ename, salary, department_name from employee where salary > (select avg(salary) from employee as e
where e.department_name = employee.department_name);
select ename, salary, department_name from employee where salary <=all (select salary from employee as e
where e.department_name = employee.department_name);