Bird
0
0

Which of the following is the correct way to use EXISTS in a PostgreSQL query to check for related records?

easy📝 Syntax Q3 of 15
PostgreSQL - Subqueries in PostgreSQL
Which of the following is the correct way to use EXISTS in a PostgreSQL query to check for related records?
ASELECT * FROM employees WHERE EXISTS (SELECT 1 FROM tasks WHERE tasks.employee_id = employees.id);
BSELECT * FROM employees WHERE EXISTS SELECT 1 FROM tasks WHERE tasks.employee_id = employees.id;
CSELECT * FROM employees WHERE EXISTS (tasks.employee_id = employees.id);
DSELECT * FROM employees WHERE EXISTS (SELECT * FROM tasks WHERE tasks.employee_id = employees.id) = TRUE;
Step-by-Step Solution
Solution:
  1. Step 1: Understand EXISTS syntax

    EXISTS requires a subquery enclosed in parentheses that returns rows.
  2. Step 2: Analyze options

    SELECT * FROM employees WHERE EXISTS (SELECT 1 FROM tasks WHERE tasks.employee_id = employees.id); correctly uses EXISTS with a subquery in parentheses returning 1 for matching rows.
    SELECT * FROM employees WHERE EXISTS SELECT 1 FROM tasks WHERE tasks.employee_id = employees.id; misses parentheses around the subquery.
    SELECT * FROM employees WHERE EXISTS (tasks.employee_id = employees.id); uses an invalid condition inside EXISTS.
    SELECT * FROM employees WHERE EXISTS (SELECT * FROM tasks WHERE tasks.employee_id = employees.id) = TRUE; compares EXISTS to TRUE, which is unnecessary and invalid syntax.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    EXISTS needs a subquery in parentheses [OK]
Quick Trick: EXISTS requires a subquery enclosed in parentheses [OK]
Common Mistakes:
  • Omitting parentheses around the subquery
  • Using EXISTS without a subquery
  • Comparing EXISTS result to TRUE or FALSE explicitly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes