Use EXISTS to check if a row is present
Need to find if records from table A can also be found in table B? EXISTS is here to help.
π Here's the syntax.
ππ΄π»π΄π²π * π΅ππΎπΌ πππππ_π
ππ·π΄ππ΄ π΄ππΈπππ (
ππ΄π»π΄π²π * π΅ππΎπΌ πππππ_π
ππ·π΄ππ΄ πππππ_π.πππ’ = πππππ_π.πππ’
) πππππ_πππππ’
Let's walk through this.
1οΈβ£ the inner query retrieves all the records from table_b that has a common key with table_a.
2οΈβ£ If the inner query returns any records, the output is simply the the records found in both tables A and B. Otherwise, the query returns no records.
EXISTS is useful when one needs to implement UPDATE/DELETE of rows in one table.
π There are multiple ways of implementing the same results. Give it a try! Write the equivalent query using "INNER JOIN" or the "IN" operator in the comment section below.
EXISTS can be used in most RDBMS, including PostgreSQL, MySQL, Oracle, Redshift and Google BigQuery.
π Full example below.