Exercise

SQL Exercises: A Question Generator

Practice SQL on as many questions as you like: write a query in the browser, get it checked, and learn from feedback on what is wrong.

Last updated:


Which query answers this question?

This page gives you a new SQL question each time, about the data you choose:
the bike-share database of the SQL guide, the new-immigrants database of its practice, or the S&P 500 database of the S&P 500 practice.
You write a query in the box and the page runs it, compares your result with the expected one, and explains what is wrong if they differ.
Nothing is installed, stored, or sent anywhere.

How the practice works

  1. Choose the data and a topic (or leave All topics), and read the question. The topic Mixed questions combines several topics and does not say which; the first hint does. The S&P 500 data is not hosted on this site: when you choose it, the page asks for the file s_p.sqlite that you downloaded, and reads it on your computer without uploading it. The question says which columns to show and how to sort.
  2. Write your query. Show a sample of the data displays the first rows of the tables, so you can check column names.
  3. Select Run to see your result, or Check my answer to compare it with the expected result. Both use the same table of results, and Download CSV saves any result to a file.
  4. If the answer is wrong, the page says what differs: the number of columns, the number of rows, the order of the rows or columns, or the values. When your result is the result of a common mistake, the page names that mistake and explains it.
  5. Stuck? Show a hint opens up to three hints, from a nudge to the structure of the query. Show the answer shows a possible query and its result; a question whose answer was shown is not counted as solved.
  6. Each question has a question code. Type a code in the box and select Open to get the same question, for example to work on one question with a friend or in class.

While you type, suggestions for keywords, table names, and column names appear.
Tab or Enter accepts a suggestion, Esc closes the list, and Ctrl+Space opens it (on a Mac, Ctrl+Space or Option+Esc).

For the questions about joining tables, the page creates the small table season_plan for you, as in the JOIN section of part 2 of the guide.
If a term is new to you, return to the guide or to the glossary.

Practice generator

Three questions for paper

The generator is hidden in print and without JavaScript, so here are three fixed questions with answers.
All three use the bike-share database of the guide, although the generator above can also draw questions from the other databases.
Write each query in the box under the question and run it before opening the answer.

Question 1. How many days in the season Summer had more than 6000 rentals (total_rides)?

Show a possible answer
SELECT COUNT(*)
FROM daily_ridership
WHERE season = 'Summer'
  AND total_rides > 6000;

The result is 78. Both conditions must hold, so they are joined with AND.

Question 2. For each weather description in 2012, show the description and the number of days, from the most days to the fewest.

Show a possible answer
SELECT weather, COUNT(*) AS days
FROM daily_ridership
WHERE year = 2012
GROUP BY weather
ORDER BY days DESC;

The result has three rows: Clear or partly cloudy (237 days), Mist or cloudy (123), and Light rain or snow (6).
Without GROUP BY the query would return a single row.

Question 3. The table season_plan gives each season a service plan.
For each service plan, show the plan and the average rentals per day in 2011, rounded to a whole number, from the highest average to the lowest.

Show a possible answer
SELECT season_plan.service_plan,
       ROUND(AVG(daily_ridership.total_rides), 0) AS average_rentals
FROM daily_ridership
INNER JOIN season_plan
    ON daily_ridership.season = season_plan.season
WHERE daily_ridership.year = 2011
GROUP BY season_plan.service_plan
ORDER BY average_rentals DESC;

The result is Extra staff (4464), Normal service (3721), and Reduced service (1667).

Return to the SQL guide, or continue with the S&P 500 practice.