CH5 SQL Commands Aggregate Functions and Joins
CH5 SQL Commands Aggregate Functions and Joins
AGGREGATE FUNCTIONS
& JOINS
MUHAIMIN P. MACAPUNDAG
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
SQL Syntax
The syntax of SQL is governed by the American National
Standards Institute (ANSI) and the International Organization for
Standardization (ISO).
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
Statements
SQL statements are very simple and straightforward like plain English
but with specific syntax.
An SQL statement is composed of a sequence of keywords,
identifiers, etc. terminated by a semicolon (;). Here is an example of a
valid SQL statement.
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
For better readability you can also write the same statement, as follow:
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
SQL Comments
A comment is simply a text that is ignored by the database engine.
Comments can be used to provide a quick hint about the SQL
statement.
SQL support single-line as well as multi-line comments. To write a
single-line comment start the line with two consecutive hyphens (--).
For example:
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
MEDIUMINT(size) A medium integer. Signed range is from -8388608 to 8388607. Unsigned range is from
0 to 16777215. The size parameter specifies the maximum display width (which is 255)
INT(size) A medium integer. Signed range is from -2147483648 to 2147483647. Unsigned range
is from 0 to 4294967295. The size parameter specifies the maximum display width
(which is 255)
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
MEDIUMINT(size) A medium integer. Signed range is from -8388608 to 8388607. Unsigned range is from
0 to 16777215. The size parameter specifies the maximum display width (which is 255)
INT(size) A medium integer. Signed range is from -2147483648 to 2147483647. Unsigned range
is from 0 to 4294967295. The size parameter specifies the maximum display width
(which is 255)
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
DATETIME(fsp) A date and time combination. Format: YYYY-MM-DD hh:mm:ss. The supported range is from
'1000-01-01 00:00:00' to '9999-12-31 23:59:59'. Adding DEFAULT and ON UPDATE in the column
definition to get automatic initialization and updating to the current date and time
TIMESTAMP(fsp) A timestamp. TIMESTAMP values are stored as the number of seconds since the Unix epoch
('1970-01-01 00:00:00' UTC). Format: YYYY-MM-DD hh:mm:ss. The supported range is from
'1970-01-01 00:00:01' UTC to '2038-01-09 03:14:07' UTC. Automatic initialization and updating to
the current date and time can be specified using DEFAULT CURRENT_TIMESTAMP and ON
UPDATE CURRENT_TIMESTAMP in the column definition
TIME(fsp) A time. Format: hh:mm:ss. The supported range is from '-838:59:59' to '838:59:59'
YEAR A year in four-digit format. Values allowed in four-digit format: 1901 to 2155, and 0000.
MySQL 8.0 does not support year in two-digit format.
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
Operators
An operator is a symbol specifying an action that is performed on one
or more expressions.
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
Expression
An expression is a combination of one or more values, operators and
SQL functions that evaluate to a value. It is used to query the
database for a specific set of data.
Syntax:
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
Boolean Expression
SQL Boolean Expressions fetch the data based on matching a single
value.
Syntax:
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
Numeric Expression
This is used to perform any mathematical operation in any query.
Syntax
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
There are several built-in functions like avg(), sum(), count(), etc., to
perform what is known as the aggregate data calculations against a table
or a column.
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
Date Expressions
Date Expressions return current system date and time values.
Example:
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
SQL Aliasing
Alias is used to rename columns, tables, subqueries, anything.
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
SQL Aliasing
Alias is used to rename columns, tables, subqueries, anything.
This query counts the number of values in column2 - for each group of
unique column1 values. Then it renames the COUNT(column2) column
to number_of_values.
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
FUNCTIONS
SQL has many built-in functions for performing processing on string,
numeric, and date data.
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
Aggregate Functions
Group By Clause – It is used with SQL aggregate functions like SUM to
provide means of grouping the result dataset by certain database table
column(s).
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
MySQL IN Clause - This is a clause, which can be used along with any
MySQL query to specify a condition.
You can use IN clause to replace many OR conditions. To understand
IN clause, consider an employee_tbl table, which is having the
following records:
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
Now, suppose based on the example table you want to fetch records
with conditions daily_typing_pages more than 170 and equal and less
than 300 and equal. This can be done using >= and <= conditions as
follows:
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
It does not matter if all the three tables have different column names.
The following query illustrates how to select names and addresses from
the three tables all at once:
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
If you want to select all records, including duplicates, follow the first
UNION keyword with ALL:
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
Syntax
SELECT COUNT (Column_Name) FROM table_name
WHERE Condition;
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
COUNT function is the simplest function and very useful in counting the
number of records, which are expected to be returned by a SELECT
statement. To understand COUNT function, consider an employee_tbl
table, which is having the following records:
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
To count the total number of rows in this table, then you can do it as
follows:
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
String Functions
String functions are used to manipulate strings in MySQL.
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
Date Functions
These functions are used to retrieve and return date values based
from specified expressions.
DATE(expr)
Extracts the date part of the date or datetime expression expression.
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
DATE_FORMAT(date,format)
Formats the date value according to the format string. The following
specifiers may be used in the format string. The .%. character is
required before format specifier characters.
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
DATEDIFF(expr1,expr2)
DATEDIFF() returns expr1 . expr2 expressed as a value in days from
one date to the other. expr1 and expr2 are date or date-and-time
expressions. Only the date parts of the values are used in the
calculation.
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
CURDATE()
Returns the current date as a value in 'YYYY-MM-DD' or
YYYYMMDD format
TIME()
Returns the time from the given date.
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
USING JOINS
The most frequently used relational operation, which brings together
data from two or more related tables into one resultant table.
INNER JOIN
The INNER JOIN selects all rows from both participating tables as
long as there is a match between the columns.
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
Syntax
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
Output
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
Syntax
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
Output
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
RIGHT JOIN
The RIGHT JOIN keyword returns all records from the right table
(table2), and the matched records from the left table (table1). The
result is NULL from the left side, when there is no match
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
Syntax
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
Output
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
FULL JOIN
The FULL OUTER JOIN keyword returns all records when there is
a match in left (table1) or right (table2) table records, and may
return very large results. However, MySQL does not support FULL
JOIN.
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
Syntax
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
(optional condition)
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
NATURAL JOIN
In MySQL, the NATURAL JOIN is such a join that performs the
same task as an INNER or LEFT JOIN, in which the ON or USING
clause refers to all columns that the tables to be joined have in
common.
The MySQL NATURAL JOIN is structured in such a way that,
columns with the same name of associate tables will appear once
only.
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
Syntax
SELECT column_name(s)
FROM table1
NATURAL JOIN
table2
NATURAL JOIN
table3...;
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
Example:
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
CROSS JOIN
A cross join (also called a Cartesian join) is a join of tables
without specifying the join condition. In this scenario, the query
would return all possible combination of the tables.
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
Note: If you don't specify a join condition when joining two tables,
database system combines each row from the first table with each
row from the second table. This type of join is called a cross join or a
Cartesian product. So, if one table has 5 rows and another has 10
rows, a cross-join query produces 50 rows.
Syntax
SELECT column_name(s) FROM table1 CROSS JOIN
table2;
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
Example
Store_Information Geography
SQL COMMANDS, AGGREGATE FUNCTIONS & JOINS
Output