Structured Query Language (SQL) - pronounced “es cue el”, not “sequel” - is a programming language used to manage data in a database.
SQL is not a programme itself but it is used in many programmes, one of the most accessible of which is SQLite1. This is a database engine - software that allows users to change the data in a database - that uses the SQL language2. It stores the database in a single file on your local machine and can be run offline from your terminal, which makes it very simple for small projects and for beginners.
Install SQLite from the terminal with:
$ sudo apt install sqlite3
You can then open it with the following (but beware that you will be working in ‘memory’ as opposed to having what you do be saved to a file):
$ sqlite3
Usually you will want to have your work be saved to a file. To launch SQLite and create a new database file (or open an existing database file if you specify the name of one that already exists) the command is as follows:
$ sqlite3 database_name.db
Here is a cheat sheet that covers most of the common functionality of SQL:
CREATE DATABASE database_name;
SHOW DATABASES;
DROP DATABASE database_name;
USE database_name;
CREATE TABLE table_name(column_1 data_type, column_2 data_type, column_3 data_type);
INTEGER
: a whole numberREAL
: a decimal numberTEXT
: a string of text characters. Quotation marks are needed.DATE
: a date (in YYYY-MM-DD format). Quotation marks are needed.CREATE TABLE table_name(column_1 data_type PRIMARY KEY);
CREATE TABLE table_name(column_1 data_type NOT NULL);
CREATE TABLE table_name(column_1 data_type UNIQUE);
CREATE TABLE table_name(column_1 data_type DEFAULT 'default_value');
DROP TABLE table_name;
INSERT INTO table_name(column_name) VALUES(value);
INSERT INTO table_name(column_1, column_2, column_3) VALUES(value_1, value_2, value_3);
UPDATE table_name SET column_1 = new_value WHERE column_2 = value;
DELETE FROM table_name WHERE column_name = value;
ALTER TABLE table_name ADD COLUMN new_column data_type;
ALTER TABLE column_1 RENAME TO column_2;
SELECT column_name FROM table_name;
SELECT column_1, column_2, column_3 FROM table_name;
SELECT * FROM table_name;
SELECT * FROM table_name LIMIT 10;
SELECT column_name AS 'alias' FROM table_name;
SELECT DISTINCT column_name FROM table_name;
SELECT * FROM table_name WHERE column_name IS NOT NULL;
SELECT * FROM table_name WHERE column_name > value;
=
: equal to!=
: not equal to>
: greater than<
: less than>=
: greater than or equal to<=
: less than or equal toSELECT * FROM table_name WHERE column_name BETWEEN value_1 AND value_2;
SELECT * FROM table_name WHERE column_1 > value AND column_2 < value;
SELECT * FROM table_name WHERE column_1 BETWEEN value_1 AND value_2 AND column_2 > value_3;
SELECT * FROM table_name WHERE column_1 BETWEEN value_1 AND value_2 OR column_2 > value_3;
AND
OR
SELECT * FROM table_name WHERE column_name LIKE 'te_t';
SELECT * FROM table_name WHERE column_name LIKE '%substring%';
SELECT * FROM table_name ORDER BY column_name;
SELECT * FROM table_name ORDER BY column_name DESC;
SELECT * FROM table_name WHERE column_1 > value ORDER BY column_2 DESC;
SELECT column_1, ROUND(column_2, num_decimal_places) FROM table_name;
SELECT COUNT(*) FROM table_name;
SELECT COUNT(*) FROM table_name WHERE column_name = value;
SELECT SUM(column_name) FROM table_name;
SELECT SUM(column_1) FROM table_name WHERE column_2 = value;
SELECT MAX(column_name) FROM table_name;
SELECT MAX(column_1) FROM table_name WHERE column_2 = value;
SELECT MIN(column_name) FROM table_name;
SELECT MIN(column_1) FROM table_name WHERE column_2 = value;
SELECT AVG(column_name) FROM table_name;
SELECT AVG(column_1) FROM table_name WHERE column_2 = value;
SELECT * FROM table_name GROUP BY column_name;
SELECT column_1, AVG(column_2) FROM table_name GROUP BY column_1 ORDER BY column_1;
SELECT column_1, AVG(column_2) FROM table_name GROUP BY column_1 ORDER BY column_1;
1
is the first column selected, 2
is the second, etc):SELECT column_1, AVG(column_2) FROM table_name GROUP BY 1 ORDER BY 1;
SELECT column_1, AVG(column_2) FROM table_name WHERE column_3 > value GROUP BY column_1 ORDER BY column_1;
SELECT column_1, COUNT(column_2) FROM table_name GROUP BY column_1 HAVING COUNT(column_2) > value;
SELECT * FROM table_1 JOIN table_2 ON table_1.column_1 = table_2.column_1;
SELECT table_1.column_2, table_2.column_3 FROM table_1 JOIN table_2 ON table_1.column_1 = table_2.column_1;
SELECT * FROM table_1 JOIN table_2 ON table_1.column_1 = table_2.column_1 WHERE column_2 = value;
SELECT * FROM table_1 LEFT JOIN table_2 ON table_1.column_1 = table_2.column_1;
ON
statement is required):
SELECT * FROM table_1 CROSS JOIN table_2;
SELECT table_1.column_1, table_2.column_2 FROM table_1 CROSS JOIN table_2;
SELECT * FROM table_1 UNION SELECT * FROM table_2;
SELECT column_1
CASE
WHEN column_2 = 5 THEN 'Great'
WHEN column_2 >= 4 THEN 'Good'
WHEN column_2 >= 3 THEN 'Average'
WHEN column_2 >= 2 THEN 'Ok'
ELSE 'Poor'
END AS 'new_column_name'
FROM table_name;
WITH previous_results AS (
SELECT column_1 AS 'alias' FROM table_1 UNION SELECT * FROM table_2
)
SELECT * FROM previous_results JOIN table_3 ON table_3.column_2 = previous_query.alias;
.open dabase.db
.tables
.schema
.schema table_name
.show
.mode mode_type
.mode column
.mode line
.mode html
.mode insert
.mode tabs
.mode csv
.mode tcl
.nullvalue 'NULL'
.headers on
.width 15 20
.dump
.exit
.output path.sql
.dump --or `select * from table_name;`
.output stdout
.mode csv
.output path.csv
select * from table_name;
.output stdout
.mode html
.output path.html
select * from table_name;
.output stdout
.read path.sql
.mode csv
.import path.csv
Technically, SQLite is not a programme but a library (ie it is embedded in fully-fledged programmes). ↩︎
Also, technically, SQLite uses ‘PostgreSQL’ which is an extension of SQL. The distinction between PostgreSQL and SQL (as well as the distinction between a library and a programme) is irrelevant for this page. ↩︎