Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 13

 A Join is a Query that combines data from multiple tables

 Multiple tables are specified in the From Clause


 For two tables to be joined in a sensible manner, they need to have data in common
 The SQL Joins clause is used to combine records from two or more tables in a
database.
 A JOIN is a means for combining fields from two tables by using values common to
each.

SQL Join Example:


Schema: Student (Student_name, student_id, admission_date)
Course(Course_taken, year_admission, persion_id)

Query: Select Student_name, course_taken, year_admission


From student, course
Where student_id = person_id
 There are different types of joins available in SQL:
 INNER JOIN: returns rows when there is a match in both tables.
 LEFT JOIN: returns all rows from the left table, even if there are
no matches in the right table.
RIGHT JOIN: returns all rows from the right table, even if there

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.

LEFT JOIN or LEFT OUTER JOIN


 The SQL RIGHT JOIN, joins two tables and Example : RIGHT JOIN or RIGHT OUTER
fetches rows based on a condition, which are JOIN
matching in both the tables.
SELECT * FROM table_A
 The unmatched rows will also be available
RIGHT JOIN table_B
from the table written after the JOIN clause.
ON table_A.A=table_B.A;

RIGHT JOIN or RIGHT OUTER JOIN


 Combines the results of both left Example: FULL OUTER JOIN
and right outer joins. SELECT * FROM table_A
 Returns all matched or unmatched FULL OUTER JOIN table_B
rows.
ON table_A.A=table_B.A;
 Includes tables on both sides of the
join clause.

FULL OUTER JOIN


 A self join is a join in which a table is Example : SELF JOIN
joined with itself (Unary relationships), SELECT * FROM table_A X, table_A Y WHERE
specially when the table has a X.A=Y.A;
FOREIGN KEY which references its
own PRIMARY KEY.
 To join a table itself means that each
row of the table is combined with itself
and with every other row of the table.
 The self join can be viewed as a join of
two copies of the same table.

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.

Cross/ Cartesian Join


 The UNION operator is used to combine  UNION Syntax
the result-set of two or more SELECT
statements.  SELECT column_name(s) FROM
 Every SELECT statement within  table1
UNION must have the same number of UNION
columns SELECT column_name(s) FROM
 The columns must also have similar  table2;
data types
 The columns in every SELECT
statement must also be in the same
order

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

returns a single value. Except for BIT_AND()


BIT_OR()
Return bitwise AND
Return bitwise OR

COUNT(*), aggregate functions BIT_XOR()


COUNT()
Return bitwise XOR
Return a count of the number of rows returned

ignore null values. Aggregate COUNT(DISTINCT)


GROUP_CONCAT()
Return the count of a number of different values
Return a concatenated string

functions are often used with the JSON_ARRAYAGG()


JSON_OBJECTAGG()
Return result set as a single JSON array
Return result set as a single JSON object

GROUP BY clause of the SELECT MAX()


MIN()
Return the maximum value
Return the minimum value

statement. All aggregate functions STD()


STDDEV()
Return the population standard deviation
Return the population standard deviation

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 ;

expressions only in the following BIT_OR()


BIT_OR()
BIT_XOR()
 BIT_OR(expr);
 BIT_OR(expr);
 BIT_XOR(expr);
BIT_XOR()  BIT_XOR(expr);

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);

statement (either a subquery or an JSON_OBJECTAGG()


JSON_OBJECTAGG()
MAX()
 JSON_OBJECTAGG(key, value);
 JSON_OBJECTAGG(key,
SELECT
value);
MAX() SELECT MAX(column_name) FROM
MAX(column_name) FROM table_name
table_name WHERE
WHERE condition;
condition;
outer query). A HAVING clause. MIN()
MIN() SELECT
SELECT MIN(column_name)
MIN(column_name) FROM
FROM table_name
table_name WHERE
WHERE condition;
condition;
STD()
STD()  STD(expr);
 SELECT STD(expr) FROM table_name WHERE condition;
STDDEV()
STDDEV()  STDDEV(expr);
 STDDEV(expr);
STDDEV_POP()
STDDEV_POP()  STDDEV_POP(expr);
 STDDEV_POP(expr);
STDDEV_SAMP()
STDDEV_SAMP()  STDDEV_SAMP(expr);
 STDDEV_SAMP(expr);
SUM()
SUM() SELECT
SELECT SUM(column_name)
SUM(column_name) FROM
FROM table_name
table_name WHERE
WHERE condition;
condition;
VAR_POP()
VAR_POP()  VAR_POP(expr);
 VAR_POP(expr);
VAR_SAMP()
VAR_SAMP()  VAR_SAMP(expr);
 VAR_SAMP(expr);
VARIANCE()
VARIANCE()  VARIANCE(expr);
 VARIANCE(expr);
SQL HAVING CLAUSE
 HAVING Syntax  SQL HAVING clause is similar to
SELECT column_name(s) the WHERE clause; they are both
FROM table_name used to filter rows in a table based
WHERE condition on conditions.
GROUP BY column_name(s)  The HAVING clause was added to
HAVING condition SQL because the WHERE keyword
ORDER BY column_name(s); cannot be used with aggregate
functions.

You might also like