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 EXISTS operator
Other subqueries

Instruction

Awesome! Let's learn about another new operator. Consider the following:

SELECT
  *
FROM City
WHERE EXISTS (
  SELECT
    *
  FROM Trip
  WHERE Trip.CityID = City.ID
);

EXISTS is an operator that checks if there are any rows that meet the given condition.

In our case, the whole query will show only those cities where our travel agency organized at least one trip. Cities with no trips will not be shown.

Exercise

Select all countries where there is at least one mountain.

Stuck? Here's a hint!

Type:

SELECT
  *
FROM Country
WHERE EXISTS (
  SELECT
    *
  FROM Mountain
  WHERE Mountain.CountryID = Country.ID
);