SQL | Subqueries exercise | List events not in the last 30 countries or the last 15 categories

This exercise is provided to allow potential course delegates to choose the correct Wise Owl Microsoft training course, and may not be reproduced in whole or in part in any format without the prior written consent of Wise Owl.

You can learn how to do this exercise if you attend one of more of the courses listed below!

Software ==> SQL  (198 exercises)
Version ==> Any version of SQL Server
Topic ==> Subqueries  (5 exercises)
Level ==> Harder than average
Classroom ==> Introduction to SQL  /  Fast-track SQL
Online ==> Introduction to SQL  /  Fast-track SQL
Before you can do this exercise, you'll need to download and unzip this file (if you have any problems doing this, click here for help). Once you've done this:
  1. Go into SQL Server Management Studio;
  2. Open the SQL file you've just unzipped (you can press CTRL + O to do this); then
  3. Execute this script.

This will generate the database that you'll need to use in order to do this exercise (note that the database and script are only to be used for exercises published on this website, and may not be reused or distributed in any form without the prior written permission of Wise Owl).

You need a minimum screen resolution of about 700 pixels width to see our exercises. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

Create a subquery to list out all of those events whose:

  • Country id is not in the list of the last 30 country ids in alphabetical order; and
  • Category id is not in the list of the last 15 category ids in alphabetical order.

You'll need to use the words NOT IN, as well as TOP N ... DESC.

Your query should list these 8 events (shown here in chronological order):

8 events returned

These events occurred in countries and categories coming early in the alphabet.

Save this query as Early events, then close it down.

You can unzip this file to see the answers to this exercise, although please remember this is for your personal use only.
This page has 2 threads Add post
21 Mar 21 at 10:51

MYSQL solution

For mysql-typical error 1235 see this https://stackoverflow.com/questions/17892762/mysql-this-version-of-mysql-doesnt-yet-support-limit-in-all-any-some-subqu



SELECT EventName , EventDetails
    FROM tblEvent e1
    WHERE e1.CountryID NOT IN
    (  SELECT sub1.CountryID FROM -- wrapping due due Error code: 1235
       (SELECT c1.CountryID FROM tblCountry c1 
      ORDER BY c1.CountryName DESC
       LIMIT 30) sub1
    ) 
 AND e1.CategoryID NOT IN
    ( 
        SELECT sub2.CategoryID FROM   -- wrapping due due Error code: 1235
        (SELECT cat1.CategoryID FROM tblCategory cat1
         ORDER BY cat1.CategoryName DESC
        LIMIT 15) sub2
    )  
    ORDER BY e1.EventDate

26 Oct 17 at 10:23

Select EventName,EventDetails,CountryID
From EventInfo
Where CountryId NOT IN
(
Select Top 30 CountryID
From Country
Order By CountryName Desc
)
And CategoryID NOT IN
(
Select Top 15 CategoryID
From Category
Order By CategoryName Desc
)

23 May 18 at 04:34

SELECT EventName,EventDetails
FROM tblCountry
INNER JOIN tblEvent
ON tblCountry.CountryID=tblEvent.CountryID
INNER JOIN tblCategory
ON tblCategory.CategoryID=tblEvent.CategoryID
WHERE tblCountry.CountryID  NOT IN       
(SELECT TOP 30 tblCountry.CountryID
FROM tblCountry
ORDER BY CountryName DESC)
AND
tblCategory.CategoryID NOT IN
(SELECT TOP 15 tblCategory.CategoryID
FROM tblCategory
ORDER BY tblCategory.CategoryName DESC)
ORDER BY EventName DESC