UPDATE statement
UPDATE {table} SET {field}={value} WHERE {field}={value}
Make new table
CREATE TABLE {table} (
{col1 def},
{col2 def}
);
New table (foreign key)
CONSTRAINT {colname} FOREIGN KEY {local_fk_colname} REFERENCES {table}({column}),
New table (primary key; auto; not null) [sqlite]
{colname} INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
New table (primary key; auto; not null) [postgres]
{colname} SERIAL PRIMARY KEY NOT NULL,
New table (column definition)
{colname} {type} [NOT NULL],
remove table (test for existence first)
DROP TABLE IF EXISTS {table};
New index
CREATE INDEX {idx_name} ON {table}({column});
INSERT statement
INSERT INTO {table} ({col1}, {col2}) VALUES ({val1}, {val2});
COUNT statement
SELECT COUNT({col})
FROM {table}
WHERE {condition};
SELECT statement
SELECT {col1}, {col2}
FROM {table};
INNER JOIN
SELECT {table1}.{col1}, {table2}.{col2}
FROM {table1}
INNER JOIN {table2} ON {table1}.{col}={table2}.{col};
UNION statement
SELECT {cols} FROM {table1}
UNION
SELECT {cols} FROM {table2};
(cols must be same number and type)
DELETE statement
DELETE FROM {table} WHERE {condition};
SUM statement
SELECT SUM({col})
FROM {table}
WHERE {condition};
AVG statement
SELECT SUM({col})
FROM {table}
WHERE {condition};
sqlite list tables
.tables
sqlite show DB schema
.schema {table}
postgres choose database
bash# pgsql {db_name}
pgsql> \c {db_name}
postgres list tables
pgsql> \dt
postgres show table schema
psql> \d+ {table}
Search for string in text field
SELECT * FROM {table}
WHERE {col} LIKE ‘%word1%’
postgres list databases
pgsql> \l
subquery syntax
SELECT
first_name,
salary
FROM
employees
WHERE
salary = (
SELECT
MAX(salary)
FROM
employees
);