You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SELECT-- column names, possibly with functions called on them
name,
length,
length/3as length_yards
FROM-- list of tables to query from, this is where they are defined
table_name,
-- can also have full queries in here
(
SELECT*FROM-- this can be repeated
(
SELECT*FROM
some_table
)
as bar
WHERE
field ='3'
)
as foo
WHERE-- queries go here
field = value
and-- where clauses can also reference queries
otherField in (select field from table where foo ='bar')
-- no need for an as there-- orderby groupby and limit go here
update
basic
UPDATE
tablename
SET
length_yards = length_feet/3,
length_furlongs = length_feet/660WHERE
length_yards is nullOR
length_furlongs is null;
join
UPDATE
tablename
SET
field ='value',
tablename.otherField=otherTable.value-- only if other table also has that fieldFROM
otherTable
-- other needed if you have a joinwheretablename.id=otherTable.id--join themandtablename.value= ?
-- if you need to specify which fields to update
Delete
DELETEFROM
tablename
WHERE
foo = ?;
-- where is optional, though remember truncate
Insert
basic
INSERT INTO
TABLE
(text, number)
VALUES
('foo', 1),
('bar', 2);
based on selection
INSERT INTO
TABLE
(text, number)
SELECT
name astext,
id asnumberFROM
otherTable
RETURNING
text;
Create table as
copy a table
CREATE TABLE
new_table
AS
TABLE
old_table;
copy table structure
CREATE TABLE
new_table
AS
TABLE
old_table
WITH NO DATA;
create a table froma query
CREATE TABLE
new_table
ASSELECT
field1 as thingy,
field2
FROM
old_table
WHERE
num BETWEEN 1AND5;