You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/4-language-usage/3-dml-and-sql/1-general/g-3120.md
+14-18Lines changed: 14 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,6 +21,19 @@ select last_name
21
21
where extract(month from hire_date) = extract(month fromsysdate);
22
22
```
23
23
24
+
If the `jobs` table has no `employee_id` column and `employees` has one this query will not raise an error but return all rows of the `employees` table as a subquery is allowed to access columns of all its parent tables - this construct is known as correlated subquery.
25
+
26
+
```sql
27
+
select last_name
28
+
,first_name
29
+
from employees
30
+
where employee_id in (
31
+
select employee_id
32
+
from jobs
33
+
where job_title like'%Manager%'
34
+
);
35
+
```
36
+
24
37
## Example (better)
25
38
26
39
```sql
@@ -47,23 +60,6 @@ select emp.last_name
47
60
where extract(month fromemp.hire_date) = extract(month fromsysdate);
48
61
```
49
62
50
-
## Example Subquery (bad)
51
-
52
-
If the `jobs` table has no `employee_id` column and `employees` has one this query will not raise an error but return all rows of the `employees` table as a subquery is allowed to access columns of all its parent tables - this construct is known as correlated subquery.
53
-
54
-
```sql
55
-
select last_name
56
-
,first_name
57
-
from employees
58
-
where employee_id in (
59
-
select employee_id
60
-
from jobs
61
-
where job_title like'%Manager%'
62
-
);
63
-
```
64
-
65
-
## Example Subquery (good)
66
-
67
63
If the `jobs` table has no `employee_id` column this query will return an error due to the directive (given by adding the table alias to the column) to read the `employee_id` column from the `jobs` table.
0 commit comments