SQL 1690091465
SQL 1690091465
JOIN typically combines rows with equal values for the specified columns. Usually, one table contains a
primary key, which is a column or columns that uniquely identify rows in the table (the cat_id column in the
cat table).
The other table has a column or columns that refer to the primary key columns in the first table (the cat_id
RIGHT JOIN
column in the toy table). Such columns are foreign keys. The JOIN condition is the equality between the RIGHT JOIN returns all rows from the right table with matching rows from the left table. Rows without a match
primary key columns in one table and columns referring to them in the other table. are filled with NULLs. RIGHT JOIN is also called RIGHT OUTER JOIN.
SELECT * toy_id toy_name cat_id cat_id cat_name
FROM toy 5 ball 1 1 Kitty
JOIN RIGHT JOIN cat 3 mouse 1 1 Kitty
ON toy.cat_id = cat.cat_id; NULL NULL NULL 2 Hugo
JOIN returns all rows that match the ON condition. JOIN is also called INNER JOIN.
1 ball 3 3 Sam
SELECT * toy_id toy_name cat_id cat_id cat_name 4 mouse 4 4 Misty
FROM toy 5 ball 1 1 Kitty whole right table
JOIN cat 3 mouse 1 1 Kitty
ON toy.cat_id = cat.cat_id; 1 ball 3 3 Sam
4 mouse 4 4 Misty
There is also another, older syntax, but it isn't recommended. FULL JOIN
List joined tables in the FROM clause, and place the conditions in the WHERE clause. FULL JOIN returns all rows from the left table and all rows from the right table. It fills the non-matching rows
SELECT * with NULLs. FULL JOIN is also called FULL OUTER JOIN.
FROM toy, cat SELECT * toy_id toy_name cat_id cat_id cat_name
WHERE toy.cat_id = cat.cat_id; FROM toy 5 ball 1 1 Kitty
FULL JOIN cat 3 mouse 1 1 Kitty
ON toy.cat_id = cat.cat_id; NULL NULL NULL 2 Hugo
JOIN CONDITIONS 1 ball 3 3 Sam
4 mouse 4 4 Misty
The JOIN condition doesn't have to be an equality – it can be any condition you want. JOIN doesn't interpret 2 spring NULL NULL NULL
the JOIN condition, it only checks if the rows satisfy the given condition. whole left table whole right table
To refer to a column in the JOIN query, you have to use the full column name: first the table name, then a dot (.)
and the column name:
ON cat.cat_id = toy.cat_id CROSS JOIN
You can omit the table name and use just the column name if the name of the column is unique within all CROSS JOIN returns all possible combinations of rows from the left and right tables.
columns in the joined tables.
SELECT * toy_id toy_name cat_id cat_id cat_name
FROM toy 1 ball 3 1 Kitty
NATURAL JOIN CROSS JOIN cat; 2
3
spring
mouse
NULL
1
1
1
Kitty
Kitty
If the tables have columns with the same name, you can use Other syntax: 4 mouse 4 1 Kitty
NATURAL JOIN instead of JOIN. cat_id toy_id toy_name cat_name SELECT * 5 ball 1 1 Kitty
1 5 ball Kitty FROM toy, cat; 1 ball 3 2 Hugo
SELECT * 1 3 mouse Kitty 2 spring NULL 2 Hugo
FROM toy 3 1 ball Sam 3 mouse 1 2 Hugo
NATURAL JOIN cat; 4 4 mouse Misty 4 mouse 4 2 Hugo
5 ball 1 2 Hugo
The common column appears only once in the result table. 1 ball 3 3 Sam
Note: NATURAL JOIN is rarely used in real life. ··· ··· ··· ··· ···
SELECT
child.cat_name AS child_name, child_name mom_name
mom.cat_name AS mom_name Hugo Kitty
FROM cat AS child Sam Hugo JOIN WITH MULTIPLE CONDITIONS
JOIN cat AS mom Misty Kitty
You can use multiple JOIN conditions using the ON keyword once and the AND keywords as many times as you
ON child.mom_id = mom.cat_id; need.
Default Partition: with no PARTITION BY clause, the Default ORDER BY: with no ORDER BY clause, the order of
SYNTAX entire result set is the partition. rows within each partition is arbitrary.
1. FROM, JOIN 7. SELECT As of 2020, GROUPS is only supported in PostgreSQL 11 and up.
2. WHERE 8. DISTINCT
3. GROUP BY 9. UNION/INTERSECT/EXCEPT
4. aggregate functions 10. ORDER BY ABBREVIATIONS DEFAULT WINDOW FRAME
5. HAVING 11. OFFSET
6. window functions 12. LIMIT/FETCH/TOP Abbreviation Meaning If ORDER BY is specified, then the frame is
UNBOUNDED PRECEDING BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW RANGE BETWEEN UNBOUNDED PRECEDING
n PRECEDING BETWEEN n PRECEDING AND CURRENT ROW AND CURRENT ROW.
You can use window functions in SELECT and ORDER BY. However, you can’t put window functions CURRENT ROW BETWEEN CURRENT ROW AND CURRENT ROW Without ORDER BY, the frame specification
anywhere in the FROM, WHERE, GROUP BY, or HAVING clauses. n FOLLOWING BETWEEN AND CURRENT ROW AND n FOLLOWING
is ROWS BETWEEN UNBOUNDED PRECEDING
UNBOUNDED FOLLOWING BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING AND UNBOUNDED FOLLOWING.
order by month
order by month
∙
count(expr) − count of values UNBOUNDED PRECEDING AND UNBOUNDED
offset=2
1 500 400 1 500 0
for rows within the window
2 300 100 2 300 0
FOLLOWING with last_value(). With the default
frame 3 400 500 3 400 500 window frame for ORDER BY, RANGE UNBOUNDED
offset=2