SQL CheatSheet
A quick look at commonly utilized postgreSQL statements and queries
/*TABLE OF CONTENTS*/
SELECT 1-3
DISTINCT 4-5
WHERE 6
OPERATOR (!=, <=, >=, = ect) 6
LOGICAL OPERATOR (AND, OR) 6
COUNT 7-8
LIMIT 9
ORDER BY 10
BETWEEN 11
NOT BETWEEN 12
IN 13
NOT IN 14
LIKE 15
NOT LIKE 16
ILIKE 17
AVG 18
ROUND 18
MIN 19
MAX 20
SUM 21
GROUP BY 22
HAVING 23
--------------------------------------------------------------------------------------------------
1./*SELECT ALL STATEMTNETS*/
SELECT * FROM table_1;
SELECT * FROM actor;
SELECT * FROM customer;
SELECT * FROM film;
--------------------------------------------------------------------------------------------------
2./*SELECT SINGLE COLUMN STATEMTNETS*/
SELECT column_1 FROM table_1;
SELECT first_name FROM actor;
--------------------------------------------------------------------------------------------------
3./*SELECT MULTIPLE COLUMN STATEMTNETS*/
SELECT column_1, column_2 FROM table_1;
SELECT first_name, last_name FROM actor;
SELECT first_name, last_name, email FROM customer;
--------------------------------------------------------------------------------------------------
4./*SELECT DISTINCT SINGLE COLUMN STATEMTNETS. [RETURNS UNIQUE VALUES]*/
SELECT DISTINCT column_1 FROM table_1;
SELECT DISTINCT release_year FROM film;
SELECT DISTINCT rating FROM film;
--------------------------------------------------------------------------------------------------
5./*SELECT DISTINCT MULTIPLE COLUMN STATEMTNETS*/
SELECT DISTINCT column_1, column_2 FROM table_1;
SELECT DISTINCT release_year, rental_rate FROM film;
--------------------------------------------------------------------------------------------------
6./*SELECT WHERE STATEMTNETS. [RETURNS SPECIFIED VALUES]*/
SELECT * FROM column_1 WHERE column_2;
SELECT * FROM payment WHERE amount != 4.99;
SELECT first_name, last_name, email FROM customer WHERE first_name='Nancy' AND last_name='Thomas'
SELECT customer_id, amount, payment FROM payment WHERE amount <= 1 OR amount >= 8;
--------------------------------------------------------------------------------------------------
7./*SELECT COUNT STATEMTNETS. [SHOWS NUMBER OF ROWS IN TABLE]*/
SELECT COUNT * FROM table_1;
SELECT COUNT(*) FROM payment;
--------------------------------------------------------------------------------------------------
8./*SELECT COUNT STATEMTNETS. [SHOWS NUMBER OF ROWS IN STATMENT]*/
SELECT COUNT(DISTINCT column_1) FROM table_1;
SELECT COUNT(DISTINCT amount) FROM payment;
--------------------------------------------------------------------------------------------------
9./*SELECT LIMIT STATEMTNETS. [RETURNS SPECIFIED NUMBER OF ROWS]*/
SELECT * FROM table_1 LIMIT;
SELECT * FROM actor LIMIT 5;
--------------------------------------------------------------------------------------------------
10./*SELECT ORDER BY STATEMTNETS. [RETURNS VALUES IN ASCENDING OR DESCENDING ORDER]*/
SELECT column_1, column_2 FROM table_1 ORDER BY column_1 ASC/DESC;
SELECT first_name, last_name FROM customer ORDER BY first_name ASC;
SELECT first_name, last_name FROM customer ORDER BY first_name ASC;SELECT first_name, last_name
FROM customer ORDER BY first_name ASC, last_name DESC;
SELECT customer_id, amount FROM payment ORDER BY amount DESC LIMIT 10;
--------------------------------------------------------------------------------------------------
11./*SELECT BETWEEN STATEMTNETS. [RETURNS VALUES BETWEEN A SPECIFIED RANGE]*/
SELECT column_1, column_2 FROM table_1 WHERE column_1 BETWEEN;
SELECT customer_id, amount FROM payment WHERE amount BETWEEN 8 AND 10;
SELECT amount, payment_date FROM payment WHERE payment_date BETWEEN '2007-02-07' AND '2007-02-15';
--------------------------------------------------------------------------------------------------
12./*SELECT NOT BETWEEN STATEMTNETS. [RETURNS VALUES NOT BETWEEN A SPECIFIED RANGE]*/
SELECT column_1, column_2 FROM table_1 WHERE column_2 NOT BETWEEN;
SELECT customer_id, amount FROM payment WHERE amount NOT BETWEEN 8 AND 9;
SELECT amount, payment_date FROM payment WHERE payment_date NOT BETWEEN '2007-02-07' AND '2007-02-15';
--------------------------------------------------------------------------------------------------
13./*SELECT IN STATEMTNETS. [RETURNS VALUES IF THEY MATCH THE SPECIFIED LIST]*/
SELECT column_1, column_2, column_3 FROM table_1 WHERE column_1 IN;
SELECT customer_id, rental_id, return_date FROM rental WHERE customer_id IN (7,13,10);
--------------------------------------------------------------------------------------------------
14./*SELECT NOT IN STATEMTNETS. [RETURNS VALUES IF THEY DO NOT MATCH THE SPECIFIED LIST]*/
SELECT column_1, column_2, column_3 FROM table_1 WHERE column_1 NOT IN;
SELECT customer_id, rental_id, return_date FROM rental WHERE customer_id NOT IN (1,15,6);
---------------------------------------------------------------------------------------------------
15./*SELECT LIKE((%) everything following or leading, (_) single character)STATEMTNETS.
[RETURNS VALUES THAT ARE LIKE THE SPECIFIED VALUE]*/
SELECT column_1, column_2 FROM table_1 WHERE column_1 LIKE;
SELECT first_name, last_name FROM customer WHERE first_name LIKE 'Jen%';
SELECT first_name, last_name FROM customer WHERE first_name LIKE '%ath%';
SELECT first_name, last_name FROM customer WHERE first_name LIKE '_heryl';
SELECT first_name, last_name FROM customer WHERE first_name LIKE '_her%';
---------------------------------------------------------------------------------------------------
16./*SELECT NOT LIKE((%) everything following or leading, (_) single character)STATEMTNETS.
[RETURNS VALUES THAT ARE NOT LIKE THE SPECIFIED VALUE]*/
SELECT column_1, column_2 FROM table_1 WHERE column_1 NOT LIKE;
SELECT first_name, last_name FROM customer WHERE first_name NOT LIKE 'Jen%';
---------------------------------------------------------------------------------------------------
17./*SELECT ILIKE((%) everything following or leading, (_) single character)STATEMTNETS.
[RETURNS VALUES THAT ARE LIKE THE SPECIFIED VALUE NOT CASE SENSETIVE]*/
SELECT column_1, column_2 FROM table_1 WHERE column_1 ILIKE;
SELECT first_name, last_name FROM customer WHERE first_name ILIKE 'BAR%';
SELECT first_name, last_name FROM customer WHERE first_name ILIKE 'BaR%';
SELECT first_name, last_name FROM customer WHERE first_name ILIKE 'baR%';
----------------------------------------------------------------------------------------------------
18./*SELECT AVG & ROUND STATEMTNETS. [RETURNS AVG OF SPECIFIED STATMENT AND ROUNDS TO GIVEN PLACE]*/
SELECT AVG(column_1)FROM table_1;
SELECT AVG(amount)FROM payment;
SELECT ROUND( AVG(amount), 2) FROM payment;
----------------------------------------------------------------------------------------------------
19./*SELECT MAX STATEMTNETS. [RETURNS MAX OF SPECIFIED STATMENT]*/
SELECT MAX(column_1)FROM table_1;
SELECT MAX(amount)FROM payment;
----------------------------------------------------------------------------------------------------
20./*SELECT MIN STATEMTNETS. [RETURNS MIN OF SPECIFIED STATMENT]*/
SELECT MIN(column_1)FROM table_1;
SELECT MIN(amount)FROM payment;
----------------------------------------------------------------------------------------------------
21./*SELECT SUM STATEMTNETS. [RETURNS SUM OF SPECIFIED STATMENT]*/
SELECT SUM(column_1)FROM table_1;
SELECT SUM(amount)FROM payment;
----------------------------------------------------------------------------------------------------
22./*SELECT GROUP BY STATEMTNETS. [GROUPS VALUES OF SPECIFIED STATMENT]*/
SELECT column_1 FROM table_1 GROUP BY column_1;
SELECT column_1, AGGREGATE(column_2) FROM table_1 GROUP BY column_1;
SELECT customer_id FROM payment GROUP BY customer_id;
SELECT customer_id, SUM (amount) FROM payment GROUP BY customer_id;
SELECT customer_id, SUM (amount) FROM payment GROUP BY customer_id ORDER BY SUM(amount) DESC;
----------------------------------------------------------------------------------------------------
22./*GROUP BY HAVING STATEMTNETS. [SETS A CONDITION FOR A GROUP BY STATMENT]*/
SELECT column_1 FROM table_1 GROUP BY column_1 HAVING column_1 (conditional statment);
SELECT customer_id, SUM(amount) FROM payment GROUP BY customer_id HAVING SUM(amount) > 200;