
Learn SQL Online Free - Interactive SQL Tutorial for Beginners
This tutorial provides you with easy to understand SQL instructions and allows you to practice while you are learning, using an online SQL interpreter. By practicing your SQL commands and seeing immediate results you will learn quickly.
More than 200,000 students learnt SQL since 2017.
Use the menu and follow the tutorial to learn SQL on your own.
Interactive SQL Lessons
Click on any lesson below to start learning SQL interactively:
. SELECT specific columns
SELECT * grabs all fields (called columns) in a table. If we only wanted to see the name and num_books_read columns, we would type SELECT name, num...
. WHERE ... Equals
In order to select particular rows from this table, we use the WHERE keyword. So for example, if we wanted to grab all of the rows that correspond...
. WHERE ... Greater than
If we want to only select family members based on a numerical field, we can also use the WHERE keyword. For example, if we wanted to select family...
. WHERE ... Greater than or equal
SQL accepts various inequality symbols, including: = “equal to”> “greater than”< “less than”>= “greater than or equal to”<= “less than ...
. AND
In the WHERE part of a query, you can search for multiple attributes by using the AND keyword. For example, if you wanted to find the friends of P...
. OR
In the WHERE part of a query, you can search for rows that match any of multiple attributes by using the OR keyword. For example, if you wanted to...
. IN
Using the WHERE clause, we can find rows where a value is in a list of several possible values. SELECT * FROM friends_of_pickles WHERE species IN (...
. DISTINCT
By putting DISTINCT after SELECT, you do not return duplicates. For example, if you run SELECT DISTINCT gender, species FROM friends_of_pickles WH...
. ORDER BY
If you want to sort the rows by some kind of attribute, you can use the ORDER BY keyword. For example, if you want to sort the friends_of_pickles ...
. LIMIT # of returned rows
Often, tables contain millions of rows, and it can take a while to grab everything. If we just want to see a few examples of the data in a table, w...
. COUNT(*)
Another way to explore a table is to check the number of rows in it. For example, if we are querying a table states_of_us, we’d expect 50 rows, or ...
. COUNT(*) ... WHERE
We can combine COUNT(*) with WHERE to return the number of rows that matches the WHERE clause. For example, SELECT COUNT(*) FROM friends_of_pickles...
. SUM
We can use the SUM keyword in order to find the sum of a given value. For example, running SELECT SUM(num_legs) FROM family_members; returns the to...
. AVG
We can use the AVG keyword in order to find the average of a given value. For example, running SELECT AVG(num_legs) FROM family_members; returns th...
. MAX and MIN
We can use the MAX and MIN to find the maximum or minimum value of a table. To find the least number of legs in a family member (2), you can run SE...
. GROUP BY
You can use aggregate functions such as COUNT, SUM, AVG, MAX, and MIN with the GROUP BY clause. When you GROUP BY something, you split the table i...
. Nested queries
In SQL, you can put a SQL query inside another SQL query. For example, to find the family members with the least number of legs, you can run: SELE...
. NULL
Sometimes, in a given row, there is no value at all for a given column. For example, a dog does not have a favorite book, so in that case there is...
. Date
Sometimes, a column can contain a date value. The first 4 digits represents the year, the next 2 digits represents the month, and the next 2 digit...
. Inner joins
Different parts of information can be stored in different tables, and in order to put them together, we use INNER JOIN ... ON. Joining tables gets ...
. Multiple joins
In the previous exercise, we explained that TV show character names were not duplicated, so if the name of a character were to change, you would on...
. Joins with WHERE
You can also use joins with the WHERE clause. To get a list of characters and TV shows that are not in “Buffy the Vampire Slayer” and are not Barn...
. Left joins
In the previous exercise, we used joins to match up TV character names with their actors. When you use INNER JOIN, that is called an “inner join” ...
. Table alias
These queries are starting to get pretty long! In the previous exercise, we ran a query containing the tables character, tv_show, and character_tv_...
. Column alias
In addition to making aliases for tables, you can also make them for columns. This clears up confusion on which column is which. In the previous...
. Self joins
Sometimes, it may make sense for you to do a self join. In that case, you need to use table aliases to determine which data is from the “first”/”l...
. LIKE
In SQL, you can use the LIKE command in order to search through text-based values. With LIKE, there are two special characters: % and _. The perc...
. CASE
You can use a CASE statement to return certain values when certain scenarios are true. A CASE statement takes the following form: CASE WHEN *firs...
. SUBSTR
In SQL, you can search for the substring of a given value. Perhaps a location is stored in the format “city, state” and you just want to grab the ...
. COALESCE
COALESCE takes a list of columns, and returns the value of the first column that is not null. Suppose we wanted to find the most powerful weapon th...
1. SELECT *
In SQL, data is usually organized in various tables. For example, a sports team database might have the tables teams, players, and games. A wedding...
Quick Start
New to SQL? Start with these fundamental lessons: