My SQL Part2
My SQL Part2
My SQL Part2
SQL Join
are no matches in the left table.
FULL JOIN: returns rows when there is a match in one of the
Types
tables.
SELF JOIN: is used to join a table to itself as if the table were two
tables, temporarily renaming at least one table in the SQL
statement.
CARTESIAN JOIN: returns the Cartesian product of the sets of
records from the two or more joined tables.
Conceptual Evaluation
From Tables: Cross
product and join 1
operations
Restriction on
where conditions 2
Compute
Sort on aggregates Restriction
Group
Group BY and reduce on HAVING
By? Yes columns each group conditions
to 1 row
No 3 5
4
Order By?
Sort
No Yes columns in 6
ORDER BY
Project columns 7
in SELECT
finish
The INNER JOIN selects all rows Example: INNER JOIN
from both participating tables as SELECT * FROM table_A
long as there is a match between the INNER JOIN table_B
columns.
ON table_A.A=table_B.A;
An SQL INNER JOIN is same as
JOIN clause, combining rows from
two or more tables.
INNER JOIN
The SQL LEFT JOIN, joins two tables and Example: LEFT JOIN or LEFT OUTER JOIN
fetches rows based on a condition, which are SELECT * FROM table_A
matching in both the tables.
LEFT JOIN table_B
The unmatched rows will also be available ON table_A.A=table_B.A;
from the table before the JOIN clause.
SELF JOIN
The SQL CROSS JOIN produces a result set
which is the number of rows in the first table Example : CROSS JOIN
multiplied by the number of rows in the
second table, if no WHERE clause is used SELECT * FROM table_A CROSS JOIN table_B;
along with CROSS JOIN.
This kind of result is called as Cartesian
Product.
If, WHERE clause is used with CROSS JOIN,
it functions like an INNER JOIN.
UNION Operator
SQL Aggregate Functions
An aggregate function performs a List of Aggregate Functions
calculation on a set of values, and Name
AVG()
Description
Return the average value of the argument
are deterministic.
STDDEV_POP() Return the population standard deviation
STDDEV_SAMP() Return the sample standard deviation
SUM() Return the sum
VAR_POP() Return the population standard variance
VAR_SAMP() Return the sample variance
VARIANCE() Return the population standard variance
How to Use Aggregate Functions
Name
Name Syntax
Syntax
Use aggregate functions as AVG()
AVG()
BIT_AND()
SELECT
SELECT AVG(column_name)
AVG(column_name) FROM
BIT_AND(expr);
FROM table_name
table_name WHERE
WHERE condition;
condition;
BIT_AND() SELECT Column1, BIT_AND(expr) FROM table_name ;
situations: COUNT()
COUNT()
COUNT(DISTINCT)
SELECT
SELECT COUNT(column_name)
COUNT(column_name) FROM
COUNT(DISTINCT
FROM table_name
expr,[expr...])
table_name WHERE
WHERE condition;
condition;
COUNT(DISTINCT) COUNT(DISTINCT expr,[expr...])
GROUP_CONCAT() GROUP_CONCAT(expr);
The select list of a SELECT GROUP_CONCAT()
JSON_ARRAYAGG()
JSON_ARRAYAGG()
GROUP_CONCAT(expr);
JSON_ARRAYAGG(expr);
JSON_ARRAYAGG(expr);