SQL Reference

Querying data

Query data in columns c1, c2 from a table

SELECT c1, c2 FROM table;

Query all rows and columns from a table

SELECT * FROM table;

Query data and filter rows with a condition

SELECT c1, c2 FROM table
WHERE condition;

Query distinct rows from a table

SELECT DISTINCT c1 FROM table
WHERE condition;

Sort the result set

SELECT c1, c2 FROM table
ORDER BY c1 ASC [DESC];

Skip offset of rows

SELECT c1, c2 FROM table
ORDER BY c1
LIMIT n OFFSET offset;

Group rows using an aggregate function

SELECT c1, aggregate(c2)
FROM table
GROUP BY c1;

Filter groups

SELECT c1, aggregate(c2)
FROM table
GROUP BY c1
HAVING condition;

Joining tables

image

Table Management

Create Table

CREATE TABLE table (
	id INT PRIMARY KEY,
	name VARCHAR NOT NULL
);

Delete Table

DROP TABLE table;

Add / Delete Column

ALTER TABLE table ADD [DROP] column;

Add / Constraint

ALTER TABLE table ADD constraint;

Misc.

Create Index

CREATE INDEX idx_name 
ON table(c1,c2);

Create View

CREATE VIEW v(c1,c2) 
AS
SELECT c1, c2
FROM table;
References