-
Notifications
You must be signed in to change notification settings - Fork 0
/
TP4.sql
95 lines (83 loc) · 2.43 KB
/
TP4.sql
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
#===============
# TP 4
#-------
USE hr;
# (1)
SELECT e.last_name, department_id, d.department_name
FROM employees AS e
NATURAL JOIN departments AS d;
# (2)
SELECT DISTINCT j.job_title
FROM employees AS e
JOIN jobs AS j USING (job_id)
WHERE e.department_id=30;
# (3)
SELECT e.last_name, d.department_name, l.state_province
FROM employees AS e
JOIN departments AS d ON e.department_id=d.department_id
JOIN locations AS l ON d.location_id=l.location_id
WHERE e.commission_pct IS NOT NULL;
# OR
SELECT e.last_name, d.department_name, r.region_name
FROM employees AS e
JOIN departments AS d ON e.department_id=d.department_id
JOIN locations AS l ON d.location_id=l.location_id
JOIN countries AS c ON c.country_id=l.country_id
JOIN regions AS r ON c.region_id=r.region_id
WHERE e.commission_pct IS NOT NULL;
# (4)
SELECT e.last_name, d.department_name
FROM employees AS e
JOIN departments AS d USING (department_id)
WHERE LOWER(e.last_name) LIKE "%a%";
# (5)
SELECT e.last_name, j.job_title, e.department_id, d.department_name
FROM employees AS e
JOIN jobs AS j ON e.job_id=j.job_id
JOIN departments AS d ON e.department_id=d.department_id
JOIN locations AS l ON d.location_id=l.location_id
WHERE l.city="Seattle";
# (6)
SELECT e.last_name AS "Employee",
e.employee_id AS "Emp#",
ee.last_name AS "Manager",
e.manager_id AS "Mgr#"
FROM employees AS e
JOIN employees AS ee ON e.manager_id=ee.employee_id;
# (7)
SELECT e.last_name AS "Employee",
e.employee_id AS "Emp#",
ee.last_name AS "Manager",
e.manager_id AS "Mgr#"
FROM employees AS e
LEFT OUTER JOIN employees AS ee ON e.manager_id=ee.employee_id;
# (8)
SELECT e.department_id AS "Dep#",
e.last_name AS "Employee",
ee.last_name AS "Collegue"
FROM employees AS e
RIGHT OUTER JOIN employees AS ee ON e.department_id=ee.department_id
WHERE e.last_name<>ee.last_name;
# (9)
DESC jobs;
SELECT e.last_name, j.job_title, d.department_name, e.salary,
CONCAT(j.min_salary, " -- ", j.max_salary) AS "Interval"
FROM employees AS e
JOIN jobs AS j ON e.job_id=j.job_id
JOIN departments AS d ON e.department_id=d.department_id;
# (10)
SELECT e.last_name, e.hire_date
FROM employees AS e
WHERE e.hire_date > (
SELECT e.hire_date
FROM employees AS e
WHERE e.email = "JKING"
);
# (11)
SELECT e.last_name AS "Employee",
e.hire_date AS "Emp Hiredate",
ee.last_name AS "Manager",
ee.hire_date AS "Mgr Hiredate"
FROM employees AS e
JOIN employees AS ee ON e.manager_id=ee.employee_id
WHERE e.hire_date < ee.hire_date;