WHERE IN
Our blogging website, Tedium, is having problems with bad actors!
Users are reporting posts spreading misinformation.
We've gotten multiple reports about the users with email Ben.Cummerata94@hotmail.com
and Audrey50@yahoo.com
.
We better look at all of their posts to investigate. We already know 2 ways to do this.
The first is to get the blogger IDs from bloggers
, then use those to get the blog_posts
in a separate query.
SELECT id
FROM bloggers
WHERE email IN ('Ben.Cummerata94@hotmail.com', 'Audrey50@yahoo.com');
id INTEGER |
---|
1 |
10 |
We can copy those IDs and match against them with the IN
operator.
SELECT *
FROM blog_posts
WHERE blogger_id IN (1, 10);
id INTEGER | title TEXT | body TEXT | blogger_id INTEGER | created_at TIMESTAMPTZ |
---|---|---|---|---|
14 | Indexing the Firewall | We need to input the wireless RAM matrix! We need to hack the online HDD driver! The SAS pixel is down, reboot the haptic monitor so we can bypass the AI capacitor! | 10 | 2021-11-23 14:22:29.048+00 |
15 | Copying the Microchip | I'll transmit the cross-platform SDD bus, that should pixel the XML array! Use the mobile USB transmitter, then you can generate the auxiliary sensor! You can't connect the bandwidth without indexing the primary SDD array! I'll synthesize the redundant PCI bandwidth, that should bandwidth the CSS interface! | 10 | 2021-10-24 18:53:13.158+00 |
16 | Indexing the Alarm | Use the bluetooth RSS port, then you can calculate the redundant system! Use the 1080p HTTP bus, then you can connect the cross-platform application! You can't navigate the microchip without generating the multi-byte GB bandwidth! | 10 | 2021-11-14 13:06:36.368+00 |
34 | Hacking the Array | calculating the system won't do anything, we need to parse the haptic ADP hard drive! You can't navigate the transmitter without bypassing the solid state HTTP alarm! Use the solid state ADP matrix, then you can calculate the redundant pixel! You can't input the bandwidth without quantifying the digital ADP card! | 1 | 2021-12-27 16:26:35.365+00 |
35 | Calculating the Driver | copying the transmitter won't do anything, we need to reboot the auxiliary SQL array! The AGP bus is down, program the mobile protocol so we can input the XSS hard drive! Use the multi-byte SDD matrix, then you can override the neural driver! programming the alarm won't do anything, we need to synthesize the solid state XML program! | 1 | 2021-12-01 04:38:21.905+00 |
36 | Compressing the Array | You can't override the driver without quantifying the optical IB monitor! I'll copy the neural AGP firewall, that should microchip the XML array! Try to synthesize the ADP bandwidth, maybe it will copy the 1080p port! | 1 | 2021-12-30 02:30:06.324+00 |
When we learned JOIN
, we found out how to do this in a single query.
SELECT p.*
FROM bloggers b
JOIN blog_posts p
ON p.blogger_id = b.id
WHERE b.email IN ('Ben.Cummerata94@hotmail.com', 'Audrey50@yahoo.com');
This works just fine, but queries with JOIN
s can get a little complicated and difficult to understand.
Many people find the first way more straightforward, even if it is 2 separate queries.
Is there a way to do that in 1 single query that resembles the first approach?
Imagine something like…
SELECT *
FROM blog_posts
WHERE blogger_id IN (bad_blogger_ids);
…where we can substitute (bad_blogger_ids)
with a subquery for those IDs.
Subquery to Expression List
Here's the solution using a subquery.
SELECT *
FROM blog_posts
WHERE blogger_id IN (
SELECT id
FROM bloggers
WHERE email IN ('Ben.Cummerata94@hotmail.com', 'Audrey50@yahoo.com')
);
id INTEGER | title TEXT | body TEXT | blogger_id INTEGER | created_at TIMESTAMPTZ |
---|---|---|---|---|
14 | Indexing the Firewall | We need to input the wireless RAM matrix! We need to hack the online HDD driver! The SAS pixel is down, reboot the haptic monitor so we can bypass the AI capacitor! | 10 | 2021-11-23 14:22:29.048+00 |
15 | Copying the Microchip | I'll transmit the cross-platform SDD bus, that should pixel the XML array! Use the mobile USB transmitter, then you can generate the auxiliary sensor! You can't connect the bandwidth without indexing the primary SDD array! I'll synthesize the redundant PCI bandwidth, that should bandwidth the CSS interface! | 10 | 2021-10-24 18:53:13.158+00 |
16 | Indexing the Alarm | Use the bluetooth RSS port, then you can calculate the redundant system! Use the 1080p HTTP bus, then you can connect the cross-platform application! You can't navigate the microchip without generating the multi-byte GB bandwidth! | 10 | 2021-11-14 13:06:36.368+00 |
34 | Hacking the Array | calculating the system won't do anything, we need to parse the haptic ADP hard drive! You can't navigate the transmitter without bypassing the solid state HTTP alarm! Use the solid state ADP matrix, then you can calculate the redundant pixel! You can't input the bandwidth without quantifying the digital ADP card! | 1 | 2021-12-27 16:26:35.365+00 |
35 | Calculating the Driver | copying the transmitter won't do anything, we need to reboot the auxiliary SQL array! The AGP bus is down, program the mobile protocol so we can input the XSS hard drive! Use the multi-byte SDD matrix, then you can override the neural driver! programming the alarm won't do anything, we need to synthesize the solid state XML program! | 1 | 2021-12-01 04:38:21.905+00 |
36 | Compressing the Array | You can't override the driver without quantifying the optical IB monitor! I'll copy the neural AGP firewall, that should microchip the XML array! Try to synthesize the ADP bandwidth, maybe it will copy the 1080p port! | 1 | 2021-12-30 02:30:06.324+00 |
We previously learned that the WHERE
clause expects a value expression,
and subqueries can only be in value expressions if they return 1 row and 1 column.
However, in this example, the subquery returns 2 rows.
Why is this allowed?
The difference here is that IN
takes an expression list and returns either TRUE
or FALSE
.
Subqueries can be converted to an expression list if they return 1 column, and any number of rows.