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.
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:
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
Runs on this device, in your browser. The first run downloads SQLite (about 0.7 MB), which is kept for the next runs.
Your run, in this browser
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:
-- 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
Runs on this device, in your browser. The first run downloads SQLite (about 0.7 MB), which is kept for the next runs.
Your run, in this browser
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.dbopens the database filepractice.db, and creates it if it does not exist yet.sqlite3on 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 boxdraws results as boxes, as on this site;.tableslists the tables;.schema productsshows the statement that created a table;.read script.sqlruns a file;.quit, or Ctrl+D, leaves. Dot-commands work only in thesqlite3program, 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 insqlite3 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 aspostgres, add it:psql -U postgres -d postgres. - Make a database to practise in with
CREATE DATABASE practice;, then connect to it withpsql -d practice. - Commands that start with a backslash belong to psql:
\dtlists the tables,\d productsdescribes one,\?lists every backslash command and\qquits. psql -d practice -f products.sqlruns a file, andpsql -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 --passwordasks for the password and then shows themysql>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 withUSE practice;. quit,exitor\qleaves.mysql --user=root --password --table practice < products.sqlruns 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:
-- 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,psqlandmysqlare the terminal programs of the three engines; dot-commands, backslash commands andquitbelong 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 textSQLite;version: the version of the SQLite library that runs the query, assqlite_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;
Results of the sample tests
| Test | Result | Details |
|---|
What your code printed
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.
References
- sql.js (README) (sql.js)
- SQLite Download Page (SQLite)
- Command Line Shell For SQLite (SQLite)
- SQLite: Release History (SQLite)
- Query Result Formatting In The CLI (SQLite)
- about_Redirection (PowerShell documentation) (Microsoft)
- SQLite: Built-In Scalar SQL Functions (SQLite)
- PostgreSQL Downloads (The PostgreSQL Global Development Group)
- PostgreSQL Downloads: Windows installers (The PostgreSQL Global Development Group)
- PostgreSQL Downloads: macOS packages (The PostgreSQL Global Development Group)
- PostgreSQL 18 Documentation: psql (The PostgreSQL Global Development Group)
- PostgreSQL 18 Documentation: System Information Functions and Operators (The PostgreSQL Global Development Group)
- MySQL 8.4 Reference Manual: MySQL Releases, Innovation and LTS (Oracle Corporation)
- MySQL 8.4 Reference Manual: Installing MySQL (Oracle Corporation)
- MySQL 8.4 Reference Manual: Installing MySQL on Microsoft Windows (Oracle Corporation)
- MySQL 8.4 Reference Manual: Deploying MySQL on Windows and Other Non-Linux Platforms with Docker (Oracle Corporation)
- MySQL 8.4 Reference Manual: mysql, the MySQL Command-Line Client (Oracle Corporation)
- MySQL 8.4 Reference Manual: mysql Client Commands (Oracle Corporation)
- MySQL 8.4 Reference Manual: mysql Client Options (Oracle Corporation)
- MySQL 8.4 Reference Manual: Information Functions (Oracle Corporation)
Related tools
Report a problem with this lesson
Kept only in this browser. Your Learn progress