Last answered:

11 Oct 2022

Posted on:

08 Oct 2022

0

Resolved: using the exists key word

Hi, I am trying to understand why this code does not give me the right result for the first part of the question. Generaly speaking, it is a logical code isn't it?image

5 answers ( 1 marked as helpful)
Posted on:

09 Oct 2022

0

Hi kfir,

You should add a WHERE clause in the subquery. Using EXISTS that way follow the syntax:

SELECT column_names
FROM table_1 t1
WHERE EXISTS
    (SELECT column_name
        FROM table_2 t2
        WHERE t1.matching_column = t2.matching_column)

Hence, the CASE statement would be:

CASE
    WHEN EXISTS
        (SELECT dm.emp_no
        FROM dept_manager dm
        WHERE e.emp_no = dm.emp_no) THEN 'Manager'
    ELSE 'Employee'
END AS is_manager



I personally tried the IN for that problem and got the same result also.

SELECT 
    e.emp_no,
    e.first_name,
    e.last_name,
    CASE
        WHEN e.emp_no IN (
       	    SELECT emp_no
            FROM dept_manager) THEN 'Manager'
	ELSE 'Employee'
    END AS is_manager
FROM employees e
WHERE
	e.emp_no > 109990;



If this helps, an upvote would be appreciated.

Kind regards,
Carl

Posted on:

09 Oct 2022

0

I tried both, does not work, but thanks.

Posted on:

10 Oct 2022

0

Interesting, what error/s have you encountered?
I got the same result as the part 1 problem with both codes.

Instructor
Posted on:

10 Oct 2022

0

Hi Kfir and Carl!
Thanks for reaching out.

@: Carl
Thanks for sharing this piece of information with the Community!
@: Kfir
Could you please share with us a screenshot of the whole error message you've encountered? This can help us assist you better. Thank you.

Looking forward to your answer.
Best,
Tsvetelin

Posted on:

11 Oct 2022

0

ok, tried again and it worked..

Submit an answer