Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
*********************
******** Comments/Notes
********
***********************************************************************************
*********************
Commenting/adding notes that aren't part of the query
To comment to the end of the current line, use '--'
To comment between two specific points use '/* ... */'
***********************************************************************************
*********************
******** Data Types
********
***********************************************************************************
*********************
Types:
numbers:
whole: integer
decimal: float
Decimal with limited significant values: numeric
text: text
Text with maximum size set: varchar(length)
date objects:
calendar date: date
calendar date with specific time: timestamp
Calendar date with specific time and timezone: timestamp without
timezone
***********************************************************************************
*********************
******** Key Concept Review - Select Functions: Select and Aliasing
********
***********************************************************************************
*********************
Select
SELECT * FROM <table>; -- return all data (columns and rows) in
the table
Examples: SELECT * FROM users;
SELECT * FROM products;
Aliasing
Format: SELECT <column> AS col_alias FROM <table> AS table_alias;
rename your columns and tables so you don't have to type out the
full names
NOTE: columns and tables alias names must start with a letter and
cannot start with a number
Examples: SELECT o.shipping_total AS total FROM orders o;
***********************************************************************************
*********************
******** Key Concept Review - Select Functions: Unique Records & Grouping
********
***********************************************************************************
*********************
Unique records: Distinct and Group by
Format - Group by: Group by <column/value> -- will group identical
rows into a single row. Must use raw column name, not alias
Format - Distinct: Distinct <column> -- will only select unique rows of
data selected/returned, any duplicate rows are not returned.
Examples:
SELECT product_type_id FROM products
GROUP BY product_type_id
ORDER BY product_type_id;
Group By & column number reference: Refer to the columns selected by the
order they are returned instead of the column itself
Example:
SELECT product_type_id FROM products
GROUP BY 1; -- will group by product_type_id since it is the 1st
column being selecte
***********************************************************************************
*********************
******** Key Concept Review - Select Functions: Math/Aggregate functions
********
***********************************************************************************
*********************
Aggregate functions
Note: If you select any columns/values that are not aggregate functions,
these will require a 'group by' clause to group the results and be able to
perform the function results.
Minimum and Maximum: returns the minimum or maximum value of the rows for
that column/calculation
Format: MAX(<column/value>), MIN(<column/value>)
Example
Example:
SELECT SUM(shipping_total) as shipping_total,
COUNT(*) as total_orders,
AVG(order_total) as avg_order,
EXTRACT(MONTH FROM TIMESTAMP created_at) as month,
EXTRACT(YEAR FROM TIMESTAMP created_at) as year
FROM orders
ORDER BY year, month;
***********************************************************************************
*********************
******** Key Concept Review - Select Functions: Date Select Functions
********
***********************************************************************************
*********************
EXTRACT(Value From <date/timestamp>)
Examples:
Select id,
EXTRACT(DAY FROM created_at),
EXTRACT(WEEK FROM created_at),
EXTRACT(MONTH FROM created_at),
EXTRACT(YEAR FROM created_at)
FROM users;
Date Functions with aggregate (count) - How many users were created in
each week, include month and year in results.
SELECT COUNT(id),
EXTRACT(WEEK FROM created_at) as week,
EXTRACT(MONTH FROM created_at) as month,
EXTRACT(YEAR FROM created_at) as year
FROM users
GROUP BY year, month, week
ORDER BY year, month, week;
***********************************************************************************
*********************
******** Key Concept Review - Casting values
********
***********************************************************************************
*********************
Sometimes columns 'type' doesn't match what you're evaluating, for this use CAST
Format: CAST(<column/result> AS <type>)
Format: <column/result>::<type>
Note: By default cast will round for you
Examples:
SELECT CAST(order_total as int), order_total from orders;
***********************************************************************************
*****************************************************
***********************************************************************************
*****************************************************
************************ KEY CONCEPTS NOT REVIEWED IN
CLASS ************************
***********************************************************************************
*****************************************************
***********************************************************************************
*****************************************************
***********************************************************************************
*********************
******** Filtering
********
***********************************************************************************
*********************
Filter: Requires using 'WHERE'
Format: WHERE <true/false evaluation/result>
>, <, = -- use less than, greater than and equal to filter numbers
!= , <> -- use this for �not equal�
Nulls note: If a value being evaluated is null, the results will ONLY be
included when specified to include nulls
Example:
SELECT * from orders where status IS NULL
SELECT * from orders where status in ('ready to ship', 'order
confirmed') OR status IS NULL
***********************************************************************************
*********************
******** Text Filtering
********
***********************************************************************************
*********************
Exact match (include upper/lower case matched) use '='
Match with wildcards (any text for part of the string) use like, or ilike to
be case insensitive
Advanced match or match anywhere in text, use '~', '~*' to be case
insensitive
like and ilike - match a string using '%' to identify where any set of
characters/words is acceptable
like -- matches the case exactly
ilike -- will ignore case when matching
Examples: (return same results/rows)
SELECT * from product_types WHERE name like '%clothing'
SELECT * from product_types WHERE name ilike '%cLoThInG'
***********************************************************************************
*********************
******** Dealing with Nulls
********
***********************************************************************************
*********************
NOTE: SELECT and WHERE will each treat nulls independantly.
If you only address nulls in the filter, nulls will still be returned
in results.
If you only address nulls in the select, the filter won't have that
context
Filtering with Nulls - If a value being filtered is null, the results will
ONLY be included when specified to include nulls
Example:
SELECT * from orders
WHERE status IS NULL
***********************************************************************************
*********************
******** Date Filtering
********
***********************************************************************************
*********************
>, <, = -- use less than, greater than and equal to filter by date strings
Ex: What orders were created from January through the end of March of
this year?
SELECT * FROM orders
WHERE created_at >= '2018-01-01'
AND created_at < '2018-04-01'
***********************************************************************************
*********************
******** Set Operators
********
***********************************************************************************
*********************
Union - return the unique set of rows unioned together. Columns of results being
united must match
Example:
SELECT product_id from order_products
UNION
SELECT product_id from cart_products
Intersect - return the unique set of rows in the primary results AND in the
secondary results
Example: get products that are currently in a cart and have been ordered
SELECT product_id from order_products
INTERSECT
SELECT product_id from cart_products
Except - return the unique set of rows in the primary results NOT in the secondary
results
Example: products not currently in any carts
SELECT id as product_id FROM products
EXCEPT
SELECT product_id FROM cart_products
***********************************************************************************
*********************
******** Joins
********
***********************************************************************************
*********************
Joins will align the values of two tables to form a single set of results with
columns from both tables.
Using a join requires giving it a clause to use to define how it will pull the data
together
Joining - There are two ways to give join the clause: using ON, and using WHERE
Format: ON - JOIN <table> ON <Join clause>
JOIN <table2> ON <join clause2>
WHERE - JOIN <table>, <table2> ... WHERE <join Clause> AND <join
clause2>
EXAMPLES:
SELECT * FROM products p
JOIN product_types pt ON pt.id = p.product_type_id -- define that we
want results aligned on product_type for the product
Inner Join - return rows from both tables where the joined values match on
the clause given, JOIN without other indicators is by default an Inner join
Note: join will by default be an INNER join
Example:
SELECT distinct order_products.product_id
FROM order_products
JOIN cart_products ON cart_products.product_id =
order_products.product_id
--Identical to above
SELECT distinct order_products.product_id
FROM order_products
INNER JOIN cart_products on cart_products.product_id =
order_products.product_id
--additional example
SELECT * FROM products p
INNER JOIN product_types pt ON pt.id = p.product_type_id -- define
that we want results aligned on product_type for the product
Outer join - return all rows from the primary table, fill in all values for
secondary table that don�t match the primary with null values (otherwise same as
inner join)
Left outer join will select the table that is first declared as the
primary table
Right outer join will select the table being joined to the first
declared table as the primary table
Example: --this will pull all rows from products and fill in
cart_product values for those that don�t match
SELECT * FROM products p
LEFT OUTER JOIN cart_products cp on cp.product_id = p.id
Example: This will pull all rows from
full outer join - return all rows from all tables and fill in any
unmatched rows with
Example:
SELECT * FROM orders o
FULL OUTER JOIN users on o.user_id = u.id
***********************************************************************************
*********************
******** Table Aliasing
********
***********************************************************************************
*********************
SELECT distinct order_products.product_id
FROM order_products
JOIN cart_products on cart_products.product_id = order_products.product_id
***********************************************************************************
*********************
******** Table Creation
********
***********************************************************************************
*********************
Create table based on a query
Creating tables: SELECT ... INTO and CREATE TABLE AS ...
Create a table that will remain in the database based on a query
Create Table
Format: CREATE TABLE <tablename> (<col name> <col type>,
<col name 2> <col type 2>,... )
***********************************************************************************
*****************************************************
***********************************************************************************
*****************************************************
************************ ADVANCED FUNCTIONS
************************
***********************************************************************************
*****************************************************
***********************************************************************************
*****************************************************
***********************************************************************************
*********************
******** Having vs. Where
********
***********************************************************************************
*********************
This won't run because aggregate functions can't be in where clause:
Select product_id, count(*) from order_products
WHERE count(*) > 2
GROUP BY product_id
ORDER BY count desc
***********************************************************************************
*********************
******** Concatenate Text
********
***********************************************************************************
*********************
Format: Select <thing to put first> || <thing2> || ... AS some_column FROM
<table>;
Example:
SELECT 'add text: ' || name as new_name FROM products;
***********************************************************************************
*********************
******** Random and Rounding
********
***********************************************************************************
*********************
Returns a random value between 0 and 1: Select random();
Returns a random value between 0 and 10: Select random() * 10;
Example:
SELECT random() * 10,
round(random() * 10),
floor(random() * 10),
ceiling(random() * 10)
***********************************************************************************
*****************************************************
***********************************************************************************
*****************************************************
************************ SUB-SELECT
************************
***********************************************************************************
*****************************************************
***********************************************************************************
*****************************************************
Sub-Select: running a query within ( and ) and utilizing those results within
a query
Example:
SELECT random_value,
round(random_value),
floor(random_value),
ceiling(random_value)
FROM (select random() * 10 AS random_value ) random_query
ON p_types.id = p.product_type_id;
***********************************************************************************
*****************************************************
***********************************************************************************
*****************************************************
************************ WITH
************************
***********************************************************************************
*****************************************************
***********************************************************************************
*****************************************************
Format: WITH use_query_as_table as (<query from database>)
<Query utilizing use_query_as_table>;
Examples:
WITH clothing_product_types as (select id from product_types where name
ilike '%clothing')
***********************************************************************************
*****************************************************
***********************************************************************************
*****************************************************
************************ TEMPORARY TABLES
************************
***********************************************************************************
*****************************************************
***********************************************************************************
*****************************************************
***********************************************************************************
*****************************************************
***********************************************************************************
*****************************************************
************************ VIEWS
************************
***********************************************************************************
*****************************************************
***********************************************************************************
*****************************************************
Create View:
CREATE VIEW <view_name> AS <query>;
CREATE VIEW <view_name> (<col1_name>, <col2_name>,...) AS <query>;
Temporary View:
CREATE TEMPORARY VIEW <view_name> AS <query>;
CREATE TEMPORARY VIEW <view_name> (<col1_name>, <col2_name>,...) AS
<query>;
Examples:
CREATE VIEW clothing
AS SELECT id as product_type_id,
name
FROM product_types
WHERE name ilike '%clothing'
***********************************************************************************
*****************************************************
***********************************************************************************
*****************************************************
************************ VIEWS LAB
************************
***********************************************************************************
*****************************************************
***********************************************************************************
*****************************************************
--CASCADE
CREATE TABLE carts_cascade AS SELECT * FROM carts;
--TRUNCATE
TRUNCATE carts_cascade;
***********************************************************************************
*****************************************************
***********************************************************************************
*****************************************************
************************ INDEXES
************************
***********************************************************************************
*****************************************************
***********************************************************************************
*****************************************************
Starting Query:
SELECT order_id, product_id,
name as product_name,
price, grams, product_type_id,
orders.created_at AS order_date,
user_id
FROM products
JOIN order_products ON order_products.product_id = products.id
JOIN orders on orders.id = order_id;
Create Index:
Format: CREATE INDEX <index_name> ON <table> (<column>)
Example:
CREATE INDEX p_type_products_index ON products (product_type_id)
QUERY TO IMPROVE:
select * from products
Join cart_products on products.id = cart_products.product_id
Join order_products on products.id = order_products.product_id
join carts on cart_products.cart_id = carts.id
join orders on orders.id = order_products.order_id