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.

More from Travis Tang
Use ARRAY_AGG to flatten columns to lists.

Use ARRAY_AGG to flatten columns to lists.

You need to convert a long table of values into a list? This is the most convenient function. πŸ“Œ Syntax: πš‚π™΄π™»π™΄π™²πšƒ π™°πšπšπ™°πšˆ_𝙰𝙢𝙢(πšŒπš˜πš•πšžπš–πš—) π™΅πšπ™Ύπ™Ό πšπšŠπš‹πš•πšŽ There
Pivot Table (From Long to Wide)

Pivot Table (From Long to Wide)

Every data scientist, scientist, and engineer should know how to create a pivot table. π™²π™°πš‚π™΄ πš†π™·π™΄π™½ is the best way to do
Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Travis Tang.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.