The following code fetches data if the SubjectB column from Table 2 matches the Subject A column from Table 1.
SELECT
UID
FROM
Table1
WHERE EXISTS (
SELECT 1
FROM Table2
WHERE SubjectB = SubjectA
)
I want to add an additional condition after the closed parentheses:
AND another_column_from_table1 > 10
But the sytax does not seem valid. What is the correct way to have a query with WHERE EXISTS … AND conditions?
Given your initial query, you should be able to write:
There shouldn’t be a syntax error from simply adding the condition to the end of the query. From the comments, it seems that this is syntactically accepted, but generates no output. The obvious technique to debug this is:
This will show you the values in the column; presumably, since the result set is empty when you add the extra filter condition, the values in the second column of this select will all be less than 10 (or null).
If there’s a one-to-one relationship between entries in Table1 and Table2, then you can use a JOIN instead:
If there could be several rows in Table2 for each row in Table1, then you would need to add DISTINCT: