Skip to main content
Code formatting
Source Link
Akina
  • 20.3k
  • 2
  • 19
  • 22

The easy answer would be to combine both queries, directly, using subqueries (IN). So, assuming the column on shipments is also called id (substitute it with the real name, if different):

SELECT *  
FROM shipments  
WHERE id IN ( SELECT DISTINCT id 
              FROM customers 
              WHERE country = 'Neverland')

It may be nice to learn a bit about JOINs, while those may seem more complicated at first, they may have in some cases a better query plan or logically less complex once you learn about those:

SELECT *
FROM shipments
JOIN customers
ON shipments.id = customers.id
WHERE shipments.country = 'Neverland'

(again, you may need to adapt it to the particular column names, and cardinality relation- show no duplicate shipments, etc.).

The easy answer would be to combine both queries, directly, using subqueries (IN). So, assuming the column on shipments is also called id (substitute it with the real name, if different):

SELECT * FROM shipments WHERE id IN (SELECT DISTINCT id FROM customers WHERE country = 'Neverland')

It may be nice to learn a bit about JOINs, while those may seem more complicated at first, they may have in some cases a better query plan or logically less complex once you learn about those:

SELECT *
FROM shipments
JOIN customers
ON shipments.id = customers.id
WHERE shipments.country = 'Neverland'

(again, you may need to adapt it to the particular column names, and cardinality relation- show no duplicate shipments, etc.).

The easy answer would be to combine both queries, directly, using subqueries (IN). So, assuming the column on shipments is also called id (substitute it with the real name, if different):

SELECT *  
FROM shipments  
WHERE id IN ( SELECT DISTINCT id 
              FROM customers 
              WHERE country = 'Neverland')

It may be nice to learn a bit about JOINs, while those may seem more complicated at first, they may have in some cases a better query plan or logically less complex once you learn about those:

SELECT *
FROM shipments
JOIN customers
ON shipments.id = customers.id
WHERE shipments.country = 'Neverland'

(again, you may need to adapt it to the particular column names, and cardinality relation- show no duplicate shipments, etc.).

Source Link
jynus
  • 15k
  • 1
  • 37
  • 46

The easy answer would be to combine both queries, directly, using subqueries (IN). So, assuming the column on shipments is also called id (substitute it with the real name, if different):

SELECT * FROM shipments WHERE id IN (SELECT DISTINCT id FROM customers WHERE country = 'Neverland')

It may be nice to learn a bit about JOINs, while those may seem more complicated at first, they may have in some cases a better query plan or logically less complex once you learn about those:

SELECT *
FROM shipments
JOIN customers
ON shipments.id = customers.id
WHERE shipments.country = 'Neverland'

(again, you may need to adapt it to the particular column names, and cardinality relation- show no duplicate shipments, etc.).