Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

SQL (PostgreSQL, MySQL, SQLite) Module 1 – Start here: databases and your first query

Run SQL in your browser and on your computer

Run this track’s queries in the browser, set up sqlite3, psql and mysql on your own computer, and read which engine and version printed each output.

  • Beginner
  • 15 minutes
  • Examples run with sql.js 1.14.2
  • By MySmartCoPilot

What you will learn

  • Run and edit an example in the browser, and explain why every run starts from an empty database
  • Install and open the sqlite3, psql and mysql programs on your own computer
  • Read an example's output panel to tell which engine and version printed it

Before you start

On this page

You can run SQL in two places: in your browser, with nothing to install, and on your own computer, in the programs that come with SQLite, PostgreSQL and MySQL. The examples on these pages are SQLite scripts, recorded with the SQLite that runs in your browser; much of their SQL also works in PostgreSQL and MySQL, and the lessons point out where those engines behave differently. This lesson sets up both places, and shows how to tell which engine printed an output.

In your browser

Examples with a Run button run in your browser, in sql.js: SQLite compiled to WebAssembly so that it can run inside a web page. This track uses sql.js 1.14.2, which contains SQLite 3.49.1. Ask it yourself:

Which SQLite is this? SQL · sqlite_version.sql
SELECT sqlite_version() AS sqlite_version;

Output

┌────────────────┐
│ sqlite_version │
├────────────────┤
│ 3.49.1         │
└────────────────┘

Recorded with sql.js 1.14.2 (SQLite 3.49.1) on macOS 26 arm64. To run it yourself: sqlite3 -box :memory: < sqlite_version.sql

sqlite_version() is a function built into SQLite that returns the version of the SQLite library running the query. The output under the code is a recorded run; press Run and your browser prints its own answer underneath.

The buttons of an example:

  • Run runs the example on your device, and your code stays on it. Before the first run, the page says how much it downloads (SQLite in WebAssembly, under a megabyte); later runs use the copy your browser keeps.
  • Edit turns the code into an editor, and Run then runs your version. Undo my changes puts the original code back. Your edits are not saved anywhere.
  • Stop ends a query that takes too long.

Every run starts from an empty database

The browser’s database lives in memory and is thrown away after each run, so no run sees what an earlier one did. That is why every example begins by creating and filling the tables it needs:

A table each run builds again SQL · fresh_database.sql
-- Each run starts from an empty database, so the script builds its table first.
CREATE TABLE notes (note_id INTEGER PRIMARY KEY, body TEXT NOT NULL);
INSERT INTO notes (body) VALUES ('written by this run');

SELECT * FROM notes;

Output

┌─────────┬─────────────────────┐
│ note_id │        body         │
├─────────┼─────────────────────┤
│ 1       │ written by this run │
└─────────┴─────────────────────┘

Recorded with sql.js 1.14.2 (SQLite 3.49.1) on macOS 26 arm64. To run it yourself: sqlite3 -box :memory: < fresh_database.sql

Press Run as often as you like: each run creates the table again, so there is always exactly one note, and never a “table already exists” error. Exercises work the same way: each test builds its own tables before it runs your query. To keep a database open across many queries, use the SQLite Online tool instead.

SQLite Online (SQL Playground) Keep one SQLite database open while you try queries, import CSV or JSON files, and download the database when you are done.

On your computer: SQLite

The SQLite project’s download page has ready-made bundles called sqlite-tools for Windows, macOS and Linux. Each holds the command-line program sqlite3 and three helper programs. They are built from the newest release, which may be newer than the SQLite 3.49.1 inside sql.js. Your computer may already have a sqlite3; type sqlite3 --version in a terminal to find out. On macOS, the download page warns that its programs are not signed: after you copy them to a folder on your PATH, remove the quarantine flag from each with xattr -d com.apple.quarantine sqlite3.

Using the program:

  • sqlite3 practice.db opens the database file practice.db, and creates it if it does not exist yet. sqlite3 on its own opens a temporary database in memory, which is deleted when you quit.
  • Type SQL statements and end each with a semicolon. Lines that start with a dot are commands of the program, not SQL: .mode box draws results as boxes, as on this site; .tables lists the tables; .schema products shows the statement that created a table; .read script.sql runs a file; .quit, or Ctrl+D, leaves. Dot-commands work only in the sqlite3 program, not in the browser.
  • To run an example file yourself, use the command printed under its output, such as sqlite3 -box :memory: < products.sql. :memory: asks for a new, empty database, and < feeds the file to the program. PowerShell on Windows does not accept < for this and stops with an error; there, and in any other terminal, sqlite3 -box :memory: ".read products.sql" does the same job.
  • Versions since SQLite 3.53.0 lay results out in a new way, with numbers lined up on the right and rounded corners on the boxes, so their grids look a little different from the ones recorded here. They also run a file named on the command line whose name ends in .sql, as in sqlite3 products.sql; older versions open such a file as a database, and the first query then fails with “file is not a database”.

Version note

The sqlite3 program from sqlite.org and sql.js do not behave the same way everywhere. One difference shows up early: since SQLite 3.41.0, the program from sqlite.org turns off by default an old habit of SQLite’s, reading a word in double quotes as text when no column has that name; sql.js keeps the habit. The lesson on SELECT and FROM shows why single quotes are the safe choice in both.

On your computer: PostgreSQL and psql

PostgreSQL’s download page lists the ways to install it on each system. On Windows, it is the interactive installer by EDB, which installs the server and pgAdmin, a graphical tool. On macOS you can choose that installer, Postgres.app, Homebrew, MacPorts or Fink. On Linux, use the packages of your distribution: the page has instructions for Debian, Ubuntu, the Red Hat family and SUSE. This track describes PostgreSQL 18.

psql is PostgreSQL’s program for the terminal: you type queries, and it shows the results.

  • Connect with psql -d postgres. If your installation set up a user name for you, such as postgres, add it: psql -U postgres -d postgres.
  • Make a database to practise in with CREATE DATABASE practice;, then connect to it with psql -d practice.
  • Commands that start with a backslash belong to psql: \dt lists the tables, \d products describes one, \? lists every backslash command and \q quits.
  • psql -d practice -f products.sql runs a file, and psql -d practice -c 'SELECT version();' runs one statement.

SELECT version(); returns a line of text that describes the server, starting with its version, such as “PostgreSQL 18.6”.

On your computer: MySQL and mysql

MySQL has two kinds of release. LTS (long-term support) series, such as MySQL 8.4, keep the same features and get only fixes for years; Innovation releases bring new features and changes more often. This track describes MySQL 8.4. Its manual lists the ways to install it: on Windows, an MSI installer followed by the MySQL Configurator; on macOS, native packages; on Linux, MySQL’s own APT and Yum repositories, your distribution’s packages or a Docker container. Oracle builds its MySQL Docker images for Linux and does not support them on other systems.

mysql is MySQL’s program for the terminal.

  • mysql --user=root --password asks for the password and then shows the mysql> prompt. Let the program ask: the MySQL manual calls a password typed on the command line itself insecure.
  • Make a database to practise in with CREATE DATABASE practice;, and choose it with USE practice;.
  • quit, exit or \q leaves.
  • mysql --user=root --password --table practice < products.sql runs a file. Without --table, results read from a file come out as plain text separated by tabs.

SELECT VERSION(); returns the server’s version, such as 8.4.11.

Reading an output panel

Every recorded output on this site ends with a line like “Recorded with sql.js 1.14.2 (SQLite 3.49.1) on macOS 26 arm64. To run it yourself: …”. It tells you three things:

  • the engine and its version: here sql.js 1.14.2, so SQLite 3.49.1;
  • the kind of computer it ran on, which rarely matters for SQL but is recorded anyway;
  • the command that runs the same file on your computer.

Errors appear under a label of their own, “Printed as an error”, and when a statement failed, the heading of the output adds the exit status, 1. This script asks for a version in two engines’ ways:

Two ways to ask for a version SQL · version_functions.sql
-- SQLite reports its version with sqlite_version().
SELECT sqlite_version() AS sqlite_version;

-- PostgreSQL and MySQL call theirs version(); SQLite has no function of that name.
SELECT version() AS server_version;

Output (exit status 1)

┌────────────────┐
│ sqlite_version │
├────────────────┤
│ 3.49.1         │
└────────────────┘

Printed as an error (standard error)

Parse error near line 5: no such function: version

Recorded with sql.js 1.14.2 (SQLite 3.49.1) on macOS 26 arm64. To run it yourself: sqlite3 -box :memory: < version_functions.sql

The first statement prints its grid. The second fails, because SQLite has no function called version(). SQLite then carries on with the next statement, if there is one, and the run ends with exit status 1. In PostgreSQL and MySQL it is the other way round: version() works, and sqlite_version() does not exist.

Practise only on data you can lose

Later lessons change and delete rows. Statements such as DELETE and UPDATE do exactly what they say, at once, to whatever database they are given. Practise in the browser, in a new file such as practice.db, or in a database you created for practice, never in one that an application or other people depend on.

Key takeaways

  • The Run buttons use sql.js 1.14.2, which is SQLite 3.49.1, on your device, and every run starts from an empty database, which is why examples create their own tables.
  • sqlite3, psql and mysql are the terminal programs of the three engines; dot-commands, backslash commands and quit belong to those programs, not to SQL.
  • Each engine reports its version its own way: sqlite_version() in SQLite, version() in PostgreSQL and MySQL.
  • An output panel names the engine and version that printed it, and the command to run it yourself.

Exercise

Exercise · Easy · SQL

Report which SQLite runs your query

Write a query that returns one row with two columns:

  • engine: the text SQLite;
  • version: the version of the SQLite library that runs the query, as sqlite_version() reports it.

The sample tests run in your browser, where sql.js runs SQLite 3.49.1, so they expect:

┌────────┬─────────┐
│ engine │ version │
├────────┼─────────┤
│ SQLite │ 3.49.1  │
└────────┴─────────┘

Run the same query in the sqlite3 program on your computer and compare: the version there is the one that program was built with.

Starter code · query.sql

-- Return one row with two columns: engine and version.
SELECT 'SQLite' AS engine;
The sample tests · sample_tests.yaml
# Sample tests (public): the query needs no table, so each test starts from an empty database.
tests:
  - name: names the engine and the version of the SQLite library in the browser
    setup: ''
    expected:
      columns: [engine, version]
      rows:
        - [SQLite, '3.49.1']
A hint

A query without FROM returns one row. Put the text in single quotes and call the function with empty parentheses, then name each column with AS: SELECT 'SQLite' AS engine, … AS version;

The sample tests run on this device, in your browser (sql.js): nothing is sent to mysmartcopilot.com. The first run downloads SQLite (about 0.7 MB), which is kept for the next runs. A check in your browser is feedback for you, not proof that the code is right for every input.

Check yourself

5 questions about this lesson. Every answer and why it is right is on the page, behind “Show the answer”. Your score stays in this browser.

  1. Question 1 of 5 You press Run on this example three times in a row. What does the third run print?

    What does this program print? Choose one answer.

    -- Each run starts from an empty database, so the script builds its table first.
    CREATE TABLE notes (note_id INTEGER PRIMARY KEY, body TEXT NOT NULL);
    INSERT INTO notes (body) VALUES ('written by this run');
    
    SELECT * FROM notes;
    Show the answer to question 1

    Answer: it prints

    ┌─────────┬─────────────────────┐
    │ note_id │        body         │
    ├─────────┼─────────────────────┤
    │ 1       │ written by this run │
    └─────────┴─────────────────────┘
    

    Every Run starts from an empty database in memory, so each run creates the table again, adds its one row and reads it back. Nothing from an earlier run is left over: no extra rows, and no "already exists" error.

  2. Question 2 of 5 In the command sqlite3 -box :memory: < query.sql, what does the < do?

    Choose one answer.

    Show the answer to question 2

    Answer: It feeds the statements in query.sql to the program, as if you had typed them

    < gives the program a file as its input, so sqlite3 reads your statements from query.sql. :memory: is the database, a new and empty one, and -box draws the result tables. The opposite, > query.sql, would write the output into the file and replace your script. Feeding the file in with < works in every version of the program (in PowerShell, use ".read query.sql" instead); only versions since SQLite 3.53.0 also run a .sql file that is named without it.

  3. Question 3 of 5 Which of these return the version of the engine that runs them?

    Choose every answer that is right.

    Show the answer to question 3

    Answer:

    • SELECT version(); in PostgreSQL
    • SELECT sqlite_version(); in SQLite
    • SELECT VERSION(); in MySQL

    Each engine has its own function. PostgreSQL and MySQL both have version(), and MySQL does not mind whether you write it in capitals. SQLite's is sqlite_version(); it has no version(), so that query fails with "no such function: version", as this lesson's example shows.

  4. Question 4 of 5 An example's output panel says "Recorded with sql.js 1.14.2 (SQLite 3.49.1)". Which SQLite printed that output?

    Choose one answer.

    Show the answer to question 4

    Answer: SQLite 3.49.1, the version built into sql.js 1.14.2

    sql.js is SQLite compiled to WebAssembly. 1.14.2 is the version of sql.js, and the SQLite inside it, the engine that ran the query, is 3.49.1. Your own sqlite3 program may be another version and, now and then, behave differently.

  5. Question 5 of 5 You want to try a DELETE statement you have just learned. Where should you run it?

    Choose one answer.

    Show the answer to question 5

    Answer: In a database made for practice, which you can throw away

    Practise only on data you can lose: the browser runner, a new file such as practice.db, or a database you created for practice. Statements that change or delete rows do exactly what they say, at once, on whatever database they are given.

References

Related tools

Report a problem with this lesson

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.