0% found this document useful (0 votes)
100 views

Select Modifying Data: SQL Cheat Sheet - Postgresql

This document provides a summary of SQL commands and functions for PostgreSQL. It covers topics such as data selection, modification, aggregation, joins, subqueries, data definition and manipulation, and more. The summary is organized into sections on SELECT statements, modifying data, table creation and alteration, indexes, CASE statements, analytic and aggregate functions, common functions, and more. It aims to serve as a quick reference cheat sheet for common PostgreSQL SQL.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views

Select Modifying Data: SQL Cheat Sheet - Postgresql

This document provides a summary of SQL commands and functions for PostgreSQL. It covers topics such as data selection, modification, aggregation, joins, subqueries, data definition and manipulation, and more. The summary is organized into sections on SELECT statements, modifying data, table creation and alteration, indexes, CASE statements, analytic and aggregate functions, common functions, and more. It aims to serve as a quick reference cheat sheet for common PostgreSQL SQL.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

SQL Cheat Sheet - PostgreSQL - ​www.databasestar.

com

SELECT Modifying Data MERGE:


INSERT INTO table_name (col1, col2...)
SELECT col1, col2
INSERT: VALUES (val1, val2)...
FROM table
INSERT INTO tablename (col1, col2...) ON CONFLICT (identifier_column) (DO
WHERE condition
VALUES (val1, val2); UPDATE
GROUP BY cols
SET col2 = EXCLUDED.val2|DO NOTHING)
HAVING condition INSERT From Table:
ORDER BY col; INSERT INTO tablename (col1, col2…) Joins
SELECT col1, col2…
Order of Processing SELECT t1.*, t2.*
UPDATE: FROM t1
1. FROM
UPDATE tablename SET col1 = val1 join_type t2 ON t1.col = t2.col;
2. JOIN
3. WHERE WHERE condition;
INNER JOIN: show all matching records in both
4. GROUP BY tables.
DELETE:
5. HAVING
DELETE FROM tablename WHERE condition;
6. SELECT LEFT JOIN: show all records from left table, and any
7. DISTINCT TRUNCATE: matching records from right table.
8. ORDER BY TRUNCATE TABLE tablename;
9. LIMIT RIGHT JOIN: show all records from right table, and
UPDATE with Join: any matching records from left table.
SELECT Keywords UPDATE t
FULL JOIN: show all records from both tables,
SET col1 = val1
DISTINCT: Removes duplicate results whether there is a match or not.
FROM tablename t
INNER JOIN table x ON t.id = x.tid CROSS JOIN: show all combinations of records from
BETWEEN: Matches a value between two other
WHERE condition; both tables.
values (inclusive)
INSERT Multiple Rows: SELF JOIN: join a table to itself. Used for hierarchical
IN: Matches a value to one of many values
INSERT INTO tablename (col1, col2…) data.
LIKE: Performs partial/wildcard matches VALUES (valA1, valB1), (valA2, valB2), SELECT p.*, c.*
(valA3, valB3); FROM yourtable p
INNER JOIN yourtable c ON p.id =
c.parent_id;
SQL Cheat Sheet - PostgreSQL - ​www.databasestar.com

Create Table Modify Column Set Operators


ALTER TABLE tablename ALTER COLUMN
Create Table:
columnname TYPE newdatatype; UNION: Shows unique rows from two result sets.
CREATE TABLE tablename (
column_name data_type Rename Column UNION ALL: Shows all rows from two result sets.
); ALTER TABLE tablename RENAME COLUMN
currentname TO newname; INTERSECT: Shows rows that exist in both result
Create Table WIth Constraints: sets.
CREATE TABLE tablename ( Add Constraint
column_name data_type NOT NULL, ALTER TABLE tablename ADD CONSTRAINT EXCEPT: Shows rows that exist in the first result set
CONSTRAINT pkname PRIMARY KEY (col), constraintname constrainttype (columns); but not the second.
CONSTRAINT fkname FOREIGN KEY (col)
REFERENCES Drop Constraint Analytic Functions
other_table(col_in_other_table), ALTER TABLE tablename DROP function_name ( arguments ) OVER (
CONSTRAINT ucname UNIQUE (col), constraint_type constraintname; [query_partition_clause]
CONSTRAINT ckname CHECK (conditions) [ORDER BY order_by_clause
); Rename Table
[windowing_clause] ] )
ALTER TABLE tablename RENAME TO
Drop Table: newtablename; Example using RANK, showing the student details
DROP TABLE tablename; and their rank according to the fees_paid, grouped by
Indexes gender:
Create Temporary Table:
Create Index: SELECT
CREATE TEMP TABLE tablename (colname
CREATE INDEX indexname ON tablename student_id, first_name, last_name,
datatype);
(cols); gender, fees_paid,
RANK() OVER (PARTITION BY gender ORDER
Drop Index: BY fees_paid) AS rank_val
Alter Table
DROP INDEX indexname; FROM student;
Add Column
ALTER TABLE tablename ADD COLUMN CASE Statement
columnname datatype;
Simple Case:
Drop Column CASE name
ALTER TABLE tablename DROP COLUMN WHEN 'John' THEN 'Name John'
columnname; WHEN 'Steve' THEN 'Name Steve'
ELSE 'Unknown'
END
SQL Cheat Sheet - PostgreSQL - ​www.databasestar.com

Searched Case:
CASE Aggregate Functions REPLACE(whole_string, string_to_replace,
WHEN name='John' THEN 'Name John' replacement_string): Replaces one string inside the
WHEN name='Steve' THEN 'Name Steve' SUM: Finds a total of the numbers provided whole string with another string.
ELSE 'Unknown'
COUNT: Finds the number of records SUBSTRING(string, [start_pos], [length]): Returns
END
part of a value, based on a position and length.
AVG: Finds the average of the numbers provided
With Clause/Common Table Expression
WITH queryname AS ( MIN: Finds the lowest of the numbers provided
SELECT col1, col2
MAX: Finds the highest of the numbers provided
FROM firsttable)
SELECT col1, col2..
Common Functions
FROM queryname…;
LENGTH(string): Returns the length of the provided
Subqueries string
Single Row:
SELECT id, last_name, salary POSITION(string IN substring): Returns the position
FROM employee of the substring within the specified string.
WHERE salary = (
CAST(expression AS datatype): Converts an
SELECT MAX(salary)
expression into the specified data type.
FROM employee
); NOW: Returns the current date, including time.
Multi Row CEIL(input_val): Returns the smallest integer greater
SELECT id, last_name, salary than the provided number.
FROM employee
WHERE salary IN ( FLOOR(input_val): Returns the largest integer less
SELECT salary than the provided number.
FROM employee
WHERE last_name LIKE 'C%' ROUND(input_val, [round_to]): Rounds a number to a
); specified number of decimal places.

TRUNC(input_value, num_decimals): Truncates a


number to a number of decimals.

You might also like