Evaluate this SQL statement:
SELECT e.emp_name, d.dept_name
FROM employees e
JOIN departments d
USING (department_id)
WHERE d.department_id NOT IN (10, 40)
ORDER BY dept_name;
The statement fails when executed. Which change fixes the error?
A.
remove the ORDER BY clause
B.
remove the table alias prefix from the WHERE clause
C.
remove the table alias from the SELECT clause
D.
prefix the column in the USING clause with the table alias
E.
prefix the column in the ORDER BY clause with the table alias
F.
replace the condition
”d.department_id NOT IN (10, 40)”
in the WHERE clause with
”d.department_id <> 10 AND d.department_id <> 40”
Explanation:
Prefix the column in the ORDER BY Clause would cause the statement to succeed, assuming that the
statement failed because the dept_name existed in employee & department tables.
Not C: Removing the alias from the columns in the SELECT clause would cause the Statement to fail if the
columns existing in both tables.
C and E is wrong. B is the correct answer.
SQL> select e.first_name,d.department_name
2 from employees e join departments d
3 using (department_id)
4 where department_id NOT IN(10,40)
5 order by department_name
6 ;
FIRST_NAME DEPARTMENT_NAME
——————– ——————————
William Accounting
Shelley Accounting
Lex Executive
Steven Executive
Neena Executive
Nancy Finance
John Finance
Ismael Finance
Daniel Finance
Jose Manuel Finance
Luis Finance
SELECT e.FIRST_NAME, d.DEPARTMENT_NAME
FROM employees e
JOIN departments d
USING (department_id)
WHERE d.department_id NOT IN (10,40)
ORDER BY DEPARTMENT_NAME;
—
ORA-25154: column part of USING clause cannot have qualifier
////////////////////////////////////////////////////////////////////
——————/————/———————————–
SELECT e.FIRST_NAME, d.DEPARTMENT_NAME
FROM employees e
JOIN departments d
USING (department_id)
WHERE department_id NOT IN (10,40)
ORDER BY DEPARTMENT_NAME;
the correct answer is B
column_alias can be used in an ORDER BY clause, but it cannot be used in a WHERE, GROUP BY, or HAVING clause.
The answer is B