2. Review
• Install MySQL, Work Bench
• Load sample Data to DB
• Select
• DISTINCT,
• WHERE, Not IN, IN, AND, OR, BETWEEN, Like, Is
null, Is Not Null, Alias
• Limit, Order by
• Sub Queries
• Trim function
5. INSTR function
• Locate a substring in a string or to check if a substring
exists in a string.
• Returns the position of the first occurrence of a
substring in a string. If the substring is not found in the
str, the INSTR function returns zero (0).
• The syntax of the INSTR function:
• INSTR(str,substr);
• The INSTR function accepts two arguments:
• The str is the string that you want to search in.
• The substr is the substring that you want to search
for.
7. INSTR function examples
• SELECT
• productName
• FROM
• products
• WHERE
• INSTR(productname,'Car') > 0;
8. CONCAT
function
• Concatenate two or more strings together.
• The MySQL CONCAT function takes one or more string
arguments and concatenates them into a single string.
• The CONCAT function requires a minimum of one
parameter otherwise it raises an error.
• The syntax of the CONCAT function:
• CONCAT(string1,string2, ... );
• The CONCAT function converts all arguments to the
string type before concatenating. If any argument is
NULL, the CONCAT function returns a NULL value.
10. CONCAT function examples
• SELECT
• concat(contactFirstName,' ',contactLastName) Fullname
• FROM
• customers;
11. CONCAT_WS
function
• MySQL provides a special form of the CONCAT function:
CONCAT_WS function.
• The CONCAT_WS function concatenates two or more
string values with a predefined separator.
• The syntax of the CONCAT_WS function:
• CONCAT_WS(seperator,string1,string2, ... );
• The first argument is the separator for other arguments:
string1, string2, …
• The CONCAT_WS function adds the separator between
string arguments and returns a single string with the
separator inserted between string arguments.
• The CONCAT_WS function returns NULL if and only if
the first argument, which is the separator, is NULL.
13. CONCAT_WS function examples
• Using Customer Table:
Customer_Address
---------------------------------------------------
Schmitt Carine
54, rue Royale
44000 Nantes
France
King Jean
8489 Strong St.
83030 Las Vegas
USA
14. FIND_IN_SET
function
• Allows you to find the position of a string within a
comma-separated list of strings.
• The syntax of the FIND_IN_SET function.
• FIND_IN_SET(needle,haystack);
• The FIND_IN_SET function returns an integer or a NULL
value depending on the value of the arguments:
• Return a NULL value if either needle or haystack is
NULL.
• Return zero if the needle is not in the haystack or
the haystack is an empty string.
• Return a positive integer if the needle is in the
haystack.
21. AVG function
• The MySQL AVG() function is an aggregate function that
allows you to calculate the average value of a set of
values or an expression.
22. COUNT function
• Count the number rows in a table.
• The COUNT function returns the number of rows in a
table. The COUNT function allows you to count all rows
in a table or rows that match a particular condition.
• There are several forms of the COUNT function:
COUNT(*), COUNT(expression) and COUNT(DISTINCT
expression).
23. SUM function
• calculate the sum of a set of values or an expression.
• If you use the SUM() function in a SELECT statement
that returns no matching row, the SUM() function
returns NULL, not zero.
• The DISTINCT operator allows you to calculate distinct
values in the set.
• The SUM() function ignores the NULL values in the
calculation.
24. MIN function
• find the minimum value in a set of values.
• The MIN function returns the minimum value in a set of
values. The MIN function is very useful in some
scenarios such as finding the smallest number,
selecting the least expensive product, getting the
lowest credit limit, etc.
25. MAX function
• The MySQL MAX function returns the maximum value in
a set of values. The MAX function is handy in many
queries such as finding the greatest number, the most
expensive product, and the largest payment from
customers.
27. Group By
• Group rows into subgroups based on values of columns or expressions.
• The GROUP BY clause returns one row for each group. In other words, it
reduces the number of rows in the result set.
• The GROUP BY clause must appear after the FROM and WHERE clauses.
• Syntax:
SELECT
c1, c2,..., cn, aggregate_function(ci)
FROM
table
WHERE
where_conditions
GROUP BY c1 , c2,...,cn;
29. Having
• specify a filter condition for groups of rows or aggregates.
• The HAVING clause is used in the SELECT statement to specify filter
conditions for a group of rows or aggregates.
• The HAVING clause is often used with the GROUP BY clause to filter
groups based on a specified condition.
• Notice that the HAVING clause applies a filter condition to each group
of rows, while the WHERE clause applies the filter condition to each
individual row.
Editor's Notes
SELECT
productname
FROM
products
WHERE
productname LIKE '%Car%';
SELECT
CONCAT_WS(CHAR(13),
CONCAT_WS(' ', contactLastname, contactFirstname),
addressLine1,
addressLine2,
CONCAT_WS(' ', postalCode, city),
country,
CONCAT_WS(CHAR(13), '')) AS Customer_Address
FROM
customers;