Resolved: related columns
I did not understand a simple point, is it indispensable to write the related columns such as " where e.emp_no= t.emp_no" if "exists" only tries to check the existence of certain elements? I know that without it, the query does not work correctly. However, I do not understand the necessity of typing it as we have selected * in a few lines before.
Hi Arsalan!
Thanks for reaching out!
If you run this query:
SELECT
*
FROM
employees e
WHERE
EXISTS( SELECT
*
FROM
titles
WHERE
title = 'Assistant Engineer');
the result will be all rows. Why? Because the condition in the subquery is
always true. But we want to limit only these results which have title="Assistant Engineer". So what we need? Some line of code which will ensure we can get only these records. So, please use:
SELECT
*
FROM
employees e
WHERE
EXISTS( SELECT
*
FROM
titles t
WHERE
t.emp_no = e.emp_no
AND title = 'Assistant Engineer');
This query will give a result set with 15 128 rows. Also the following query will give also 15 128 rows:
SELECT
*
FROM
titles
WHERE
title = 'Assistant Engineer';
But because we use in our query EXISTS
we need to limit the result set with this additional line of code. The point is to exercise EXISTS
. Please, watch the lecture one more time. Thank you!
Hope this helps.
Best,
Tsvetelin