diff --git a/docs/4-language-usage/3-dml-and-sql/1-general/g-3120.md b/docs/4-language-usage/3-dml-and-sql/1-general/g-3120.md index c96e6bda..b506287e 100644 --- a/docs/4-language-usage/3-dml-and-sql/1-general/g-3120.md +++ b/docs/4-language-usage/3-dml-and-sql/1-general/g-3120.md @@ -21,6 +21,19 @@ select last_name where extract(month from hire_date) = extract(month from sysdate); ``` +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. + +``` sql +select last_name + ,first_name + from employees + where employee_id in ( + select employee_id + from jobs + where job_title like '%Manager%' + ); +``` + ## Example (better) ``` sql @@ -47,23 +60,6 @@ select emp.last_name where extract(month from emp.hire_date) = extract(month from sysdate); ``` -## Example Subquery (bad) - -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. - -``` sql -select last_name - ,first_name - from employees - where employee_id in ( - select employee_id - from jobs - where job_title like '%Manager%' - ); -``` - -## Example Subquery (good) - 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. ``` sql @@ -75,4 +71,4 @@ select emp.last_name from jobs j where j.job_title like '%Manager%' ); -``` \ No newline at end of file +``` diff --git a/docs/4-language-usage/3-dml-and-sql/1-general/g-3195.md b/docs/4-language-usage/3-dml-and-sql/1-general/g-3195.md index 174391f4..1b72e454 100644 --- a/docs/4-language-usage/3-dml-and-sql/1-general/g-3195.md +++ b/docs/4-language-usage/3-dml-and-sql/1-general/g-3195.md @@ -18,6 +18,8 @@ select e.employee_id ## Example (good) +Using a wildcard: + ``` sql select e.employee_id ,e.last_name @@ -25,7 +27,7 @@ select e.employee_id where e.last_name like 'Smith%'; ``` -## Example (good) +Change to equality operator instead: ``` sql select e.employee_id diff --git a/docs/4-language-usage/3-dml-and-sql/3-transaction-control/g-3310.md b/docs/4-language-usage/3-dml-and-sql/3-transaction-control/g-3310.md index 85d1d85a..26e495db 100644 --- a/docs/4-language-usage/3-dml-and-sql/3-transaction-control/g-3310.md +++ b/docs/4-language-usage/3-dml-and-sql/3-transaction-control/g-3310.md @@ -67,7 +67,7 @@ end; / ``` -## Example (better) +## Example (best) (Assuming suitable foreign key relationship exists to allow updating a join.) diff --git a/docs/4-language-usage/5-exception-handling/g-5040.md b/docs/4-language-usage/5-exception-handling/g-5040.md index a421d0de..147e3a2e 100644 --- a/docs/4-language-usage/5-exception-handling/g-5040.md +++ b/docs/4-language-usage/5-exception-handling/g-5040.md @@ -33,7 +33,7 @@ end; / ``` -## Example (exception to the rule) +An exception to the rule where `when others` can be good to log the error and then re-raise it: ``` sql begin diff --git a/docs/4-language-usage/8-patterns/3-validating-input-parameter-size/g-8310.md b/docs/4-language-usage/8-patterns/3-validating-input-parameter-size/g-8310.md index de9931ca..4c3e597b 100644 --- a/docs/4-language-usage/8-patterns/3-validating-input-parameter-size/g-8310.md +++ b/docs/4-language-usage/8-patterns/3-validating-input-parameter-size/g-8310.md @@ -49,7 +49,7 @@ end department_api; / ``` -## Function call +The exception should be handled where the function is called, like this: ``` sql ...