Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Very simple subqueries
Subqueries with multiple results
Correlated subqueries
16. The operator EXISTS with NOT
Other subqueries

Instruction

Good! Remember the NOT operator? We can use it together with EXISTS. Take a look:

SELECT *
FROM city
WHERE NOT EXISTS (
  SELECT
    *
    FROM trip
    WHERE city_id = city.id
);

As you probably expect, this query will find all cities that don't have a trip organized to them.

Exercise

Select all mountains with no hiking trips to them.

Stuck? Here's a hint!

Type:

SELECT *
FROM mountain
WHERE NOT EXISTS (
  SELECT *
  FROM hiking_trip
  WHERE mountain_id = mountain.id
);