Last answered:

15 Aug 2023

Posted on:

10 Aug 2023

0

Resolved: EXISTS mechanism

" 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. "


How it will display just the specified rechords if it returns just true or false and the "line of code which will ensure we can get only these records." is inside exists ?

2 answers ( 1 marked as helpful)
Instructor
Posted on:

14 Aug 2023

2

Hi Marwa!
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.



Hope this helps.
Best,
Tsvetelin

Posted on:

15 Aug 2023

0

Thanks for your time and effort :)

Submit an answer