This page allows you to practice SQL online, and provides more than 100 different SQL exercises and solutions. This platform is also accompanied by a self-study guide, which offers comprehensive coverage of the SQL language, in a detailed, logical and well organized manner.

To accomplish these SQL exercises, you can use two environments – SQL Fiddle and Stack Overflow Data Explorer, each provides different dataset and exercises.

 

We hope you’ll find this page useful and effective.

Ram Kedem and Elad Peleg.

 

 

 

Using the links provided below, you may perform the SQL tutorial exercises online. This online environment will allow you to perform ACDB database (SQL Server) and HR schema (Oracle) related exercises. After choosing which database you want to use, the following window will be opened:

Practice SQL Online

At the right side of the window write your SQL query and execute it by pressing the Run SQL button:

Practice SQL Online

And that’s it, you’re all set !

Please note – Exercises performed on the online environment, might return less results than those performed on your local environment (by downloading ACDB or HR to your local machine). The platform I used to create this online SQL practice has a limit for the maximum number of rows in each database, therefore in each table you’ll find only sample rows of data.

 

 

The following questions allow you to practice SQL online using the Stack Overflow Data Explorer.

The Stack Overflow Data Explorer is a free utility allowing you to learn SQL interactively by querying the data of Stack Overflow Q&A for programmers.

 

sql practice online

 

Very important
Most of the tables of Stack Overflow Data Explorer contain great amount of rows. Querying the whole table will slow you down significantly, and most importantly – will overload the database server.
Please, use the TOP command in every SQL statement you issue, and retrieve back only partial results.
You will be reminded to so that in every exercise 🙂

 

Chapter 1 - Basic SQL Select Statments

Practice SQL Online – Basic SQL Select Statments

 

  1. Create a query to display all data from the Posts table. Remember to use TOP 20.
  2. Create a query to display all the data from the Users table. Remember to use TOP 20.
  3. The following SELECT statement executes successfully (True / False)
    FROM Posts
    SELECT PostTypeId, AnswerCount
    
  4. Create a query to display the AnswerCount, ViewCount, and Title. Name the last column (Title) heading as “Post Title” (Posts table). Remember to use TOP 20.
  5. The following SELECT statement executes successfully (True / False)
    SELECT TOP 20 Id, Views, Views
    FROM Users
    
  6. The following SELECT statement executes successfully (True / False)
       
    SeleCT TOP 20 PostTypeId, AnswerCount, 
    FROM Posts
    
  7. Create a query to display the post id, ViewCount, Title, AnswerCount and CreationDate (Posts table). Remember to use TOP 20.
  8. Create a query to display the Title, Body, CreationDate, AnswerCount, and FavoriteCount after an addition of 20%. Name the last column (FavoriteCount after the addition) heading as “New Count” (Posts table). Remember to use TOP 20.
  9. Create a query to display the Body concatenated with the Title, separated by space, and the literal ‘Tag List : ‘ concatenated with the values of Tags column. Name the column headings “Post Details” and “Tags” respectively (Posts table). Remember to use T0P 20.
  10. Create a query to display the unique PostTypeId from Posts table.
  11. Create a query to display the Body concatenated with Tags column, separated by space. Name this column heading as “Body and Tags” (Posts table). Remember to use TOP 20.
  12. Create a query to display the Title, Body, and AnswerCount, also display the Score column concatenated with the literal string “SCn”, separated by space. Name the column headings “TL”, “BD”, “AC”, and “SC” respectively (Posts table). Note that the Score column datatype is numeric, and in order to concatenate it with a string you need to use the CAST function. Remember to use TOP 20.
  13. Create a query to display the unique ReviewTaskTypeId in ReviewTasks table. TOP statement cannot be used in this exercise.

Chapter 2 - Filtering and Sorting the Results

Practice SQL Online – Filtering and Sorting the Results

 

  1. Display the Reputation, Location, DisplayName, and Views for all users who live in Madagascar (Users table). Remember to use TOP 20.
  2. Display Text, UserDisplayName and Score from Comments table, for all Comments with score of 100. Remember to use TOP 20.
  3. Display the Title, Body, ViewCount and AnswerCount for all Posts with AnswerCount greater than 62 (Posts table). Remember to use TOP 20.
  4. Display all data from Posts table for all Posts which were created after January 1st, 2010. Remember to use TOP 20.
  5. Display the Id, Title, Tags and ViewCount for all Posts whose ViewCount equals 100, 200 or 300 (Posts table). Remember to use TOP 20.
  6. Display the Id, Title, Tags and ViewCount for all Posts whose ViewCount is not equal to 200, 600 or 800 (Posts table). Remember to use TOP 20.
  7. Display the PostId, Score, and Text, for all comments whose Score equals 10, 20 or 30 (Comments table). Remember to use TOP 20.
  8. Display the Title and AnswerCount for all Posts whose Title ends with an e (Posts table). Remember to use TOP 20.
  9. Display the Body and ViewCount for all Posts where the second letter in their Body is i (Posts table). Remember to use TOP 20.
  10. Display all data from Posts table for all Posts that have the letters : L, J, or H in their Body. Remember to use TOP 20.
  11. Display the Title, CreationDate, AnswerCount and ViewCount for all Posts whose Title doesn’t have the letter A. Remember to use TOP 20.
  12. Display all data from Posts table for all Posts without any ViewCount. Remember to use TOP 20.
  13. Display the Title concatenated with the Body, separated by comma, and AnswerCount, for all Posts whose AnswerCount is not in the range of 7000 and 15000. Remember to use TOP 20.
  14. Display :
      • The Title concatenated with the Body, separated by comma
      • The AnswerCount concatenated with the FavoriteCount, separated by hyphen
      • The ViewCount

    for all Posts whose AnswerCount is in the range of 0 and 2000. Name the column headings: TB, AL and VC respectively (Posts table). Remember to use TOP 20.

  15. Display all data from Posts table for all Posts whose:
      • AnswerCount is in the range of 600 AND 800 and their LastEditDate is not null

    OR

    • ViewCount is not equal to 80, 90 AND 100 and their CreationDate is before January 1st, 2015.

    Remember to use TOP 20.

  16. Display Body, Tags and CreationDate for all Posts which were created during December 12th, 2015 and April 17th, 2015. Remember to use TOP 20.
  17. Display the Title concatenated with Body, CreationDate, LastEditDate, FavoriteCount, and AnswerCount for all Posts whose AnswerCount is greater than 100 or the third digit in their AnswerCount equals 5. Remember to use TOP 20.
  18. Display the Body and AnswerCount for all Posts with more than 1200 answers (Posts table). Remember to use TOP 20.
  19. Display the Body and ViewCount for all Posts whose ViewCount is equal to 50 or 80. Perform this exercise once by using the IN operator, once by using the OR operator. Remember to use TOP 20.
  20. Display the Title and AnswerCount for all Posts without any LastEditDate. Remember to use TOP 20.
  21. Display the Title, AnswerCount, and FavoriteCount for all Posts whose FavoriteCount is not null. Remember to use TOP 20.

Chapter 3 - Scalar Functions

Practice SQL Online – Scalar Functions

 

String Functions

  1. Display the PostTypeId, Title in lowercase and Body in uppercase for all posts whose PostTypeId equals 1. Remember to use TOP 20.
  2. Generating Unique Post ID –
    • For all posts – display the Body, Title and a new column named UniquePostID.
    • The UniquePostID will be composed from the first letter of Title concatenated with three first letters of Body concatenated with the string “#POSTID”.
    • Remember to use TOP 20.
  3. For all posts – display the Body, Title and new column named UniquePostID.
      • The UniquePostID will be composed from the first letter of Title concatenated with three last letters of Body concatenated with the string “#POSTID”.
      • Remember to use TOP 20.
  4. Display the Body and the length of the Body for all posts whose Body’s length is greater than 500 characters. Remember to use TOP 20.
  5. Display the Title, Body, and a new Body using the REPLACE function. In the new Body replace all occurrences of “I’m” with “I am”. Remember to use TOP 20.

Numeric Functions

  1. From Posts table, for all posts, display :
      • Title
      • ViewCount
      • ViewCount after an addition of 19.7%.
      • ViewCount after an addition of 19.7%, expressed as a whole number (ROUND).
      • ViewCount after an addition of 19.7%, round down to the nearest whole number (FLOOR).
      • ViewCount after an addition of 19.7%, round up to the nearest whole number (CEILING).
      • Remember to use TOP 20.

Date Functions

  1. From Posts table, for all posts, display the Title, CreationDate, CreationDate minus 10 days, CreationDate plus one month and the difference in days between CreationDate and current date. Remember to use TOP 20.
  2. Display the Title, CreationDate and age for all posts created at least 3 years ago. Remember to use TOP 20.
  3. Display the Title, CreationDate, and the difference in years between CreationDate and current date, for all posts which were created exactly 3 years ago. Remember to use TOP 20.

Conversion functions

  1. Display the Title concatenated with the CreationDate, and Body concatenated with the ViewCount, for all posts. Complete this exercise using CAST. Remember to use TOP 20.
  2. From Posts table, for all posts whose Body starts with “a”, “d” or “k”, display:
    • Body
    • Tags in uppercase concatenated with PostTypeId
    • CommentCount concatenated with CreationDate
    • Complete this exercise using CONVERT, and in the WHERE clause instead of using LIKE, try to define the filtering condition using SUBSTRING.
    • Remember to use TOP 20.

Null-Related Functions

  1. Display the Title, Body, CreationDate, AnswerCount and PostTypeId for all posts whose PostTypeId equals 2. Replace every null value in AnswerCount, Body or Title with ‘N/A’. Remember to use TOP 20.

Chapter 4 - Group Functions

Practice SQL Online – Group Functions

In the following exercises, at the FROM clause, instead of using the actual table name, use this statement instead – (SELECT TOP 100 * FROM table_name) TBL So for example, instead of writing:

SELECT MAX(AnswerCount) FROM posts 

Use:

SELECT MAX(AnswerCount) FROM (SELECT TOP 100 * FROM posts) TBL

This way you’ll narrow down the sets of values that need to be considered by the Group Function. In these exercises you will not be able to work on partial results using the TOP statement.

Part 1 – Basic Usage

  1. Display the lowest Title alphabetically (Posts table).
  2. Display the highest Title alphabetically (Posts table).
  3. Display the number of rows in Posts table.
  4. Display the number of values (exclude NULLs) in LastEditDate column (Posts table).
  5. Display the number of NULL values in LastEditDate column (Posts table).
  6. Display the highest, lowest, and average AnswerCount.

Part 2 – GROUP BY and HAVING clauses

  1. Average Reputation per Location
      • Display the Location and average Reputation for each Location (Users table).
    • Modify your query to display the results only for Budapest or Germany.
  2. Numer of Views per Specific Age
      • Display the number of Views for each Age (Users table).
      • Modify your query to display the results for all users except those living in the United Kingdom.
    • Modify your query again, this time display the results only for those having at least 20 views.
  3. Display the number of ViewCount, and the average AnswerCount for each year (Posts table).
  4. Display the UserDisplayName, average Score, and number of posts for each UserDisplayName (Comments table)
    • Modify your query to display the results only for users with more than 50 posts

Chapter 5 - Querying Multiple Tables

Practice SQL Online – Querying Multiple Tables

 

  1. Votes and Vote Types –
    • Write a query to display PostId, VoteTypeId, CreateationDate and Vote Type Name for all votes.
    • Remember to use TOP 20
  2. Posts and Comments –
    • Write a query to display the post id, CreationDate, Score, and comment text.
    • Remember to use TOP 20
  3. Posts, Comments and Badges –
    • Write a query to display the post id, CreationDate, Score, comment text, and badges name.
    • Remember to use TOP 20
    • Repeat last question and display the results only for badges whose name equals “Guru”. Remember to use TOP 20.
  4. PendingFlags and FlagTypes
    • Create a query to display flag type name, flag type description, post id, and DuplicateOfQuestionId. Remember to use TOP 20.
  5. PendingFlags, CloseReasonTypes, and Posts
    • Create a query to display the post title and the name of its closed reason type. Remember to use TOP 20.
  6. PostTags, Tags and Posts
    • Create a query to display the post title and its tags. Remember to use TOP 20.
    • Repeat last question and display the results only for post id 4187767. Remember to use TOP 20.

Chapter 6 - Sub Queries

Practice SQL Online – Sub Queries

  1. Display the Title, Body, CreationDate and ViewCount for all posts which have the same ViewCount as post id 2379173 (posts table). Remember to use TOP 20.
  2. Display the Reputation, DisplayName and Location for all users whose location equals to the location of user Id 3657106 (Users table). Remember to use TOP 20.
  3. Display the Title, Body and CreationDate for all posts which were created after Post id 2379173 (Posts table). Remember to use TOP 20.
  4. Display the Title, Body and CreationDate for all Posts which were created on the same month and on the same year as post id 2379173 (Posts table). Remember to use TOP 20.
  5. Display the PostId, Score, Text, and UserId for all comments written by a user with a DisplayName of “jeff_new” (Comments & Users tables). Remember to use TOP 20.
  6. Display the PostId, Score and Text for all comments which have the same score as comment id 39859914. Remember to use TOP 20.
  7. Display the Title, ViewCount, AnswerCount and postTypeId for all posts with TagId equals to 3 (Posts & PostTags tables). Remember to use TOP 20.
  8. Display the Title, ViewCount and AnswerCount for all posts whose AnswerCount is greater than the average AnswerCount (Posts table). Remember to use TOP 20.
  9. Display the Title, CreationDate and ViewCount for all posts which were created at the same date as post id 2379173, and whose ViewCount is greater than the ViewCount of post id 2379183 (Posts table). Remember to use TOP 20.
  10. Display all the data from Users table for users whose Location equals to the location of user id 3657109, and whose Age is greater than the age of user id 3657102 (Users table). Remember to use TOP 20.