Introduction To Database
Introduction To Database
Databases are everywhere. So what is a database? By The ORDER BY clause allows you to sort data using a
definition, a database is merely a structured collection of custom list by using the FIELD() function.
data.
SELECT orderNumber, status FROM orders ORDER BY
FIELD(status, 'In Process', 'On Hold',
A relational database is a collection of data items with pre-
'Cancelled’, 'Resolved', 'Disputed', 'Shipped');
defined relationships between them. These items are
organized as a set of tables with columns and rows. Tables
are used to hold information about the objects to be Suppose that you want to sort the sales orders based on
represented in the database. their statuses in the following order:
ORDER BY
c1.city;
The HAVING clause is often used with the GROUP BY clause
to filter groups based on a specified condition. If
the GROUP BY clause is omitted, the HAVING clause
behaves like the WHERE clause.
SELECT
ordernumber,
SUM(quantityOrdered) AS itemsCount,
SUM(priceeach*quantityOrdered) AS total
FROM orderdetails
GROUP BY ordernumber;
SELECT
YEAR(orderDate) AS year,
SUM(quantityOrdered * priceEach) AS total
FROM orders
INNER JOIN orderdetails
USING (orderNumber)
WHERE status = 'Shipped'
GROUP BY YEAR(orderDate);
SELECT
ordernumber,
SUM(quantityOrdered) AS itemsCount,
Using MySQL GROUP BY with HAVING clause example SUM(priceeach*quantityOrdered) AS total
FROM orderdetails
GROUP BY ordernumber
The HAVING clause is used in the SELECT statement to HAVING
specify filter conditions for a group of rows or aggregates. total > 1000 ;
SELECT productLine, orderYear, SUM(orderValue)
totalOrderValue
FROM sales
WITH ROLLUP;
SELECT
ordernumber,
SUM(quantityOrdered) AS itemsCount,
SUM(priceeach*quantityOrdered) AS total
FROM orderdetails
GROUP BY ordernumber
HAVING
total > 1000
AND itemscount>600 ;
SELECT
productLine,
SUM(orderValue) totalOrderValue
FROM
sales
GROUP BY
productline WITH ROLLUP;
SELECT orderYear,
productLine,
SUM(orderValue) totalOrderValue
FROMsales
GROUP BY
orderYear,
productline
WITH ROLLUP;
orderYear,
productline
WITH ROLLUP;
SELECT
IF(GROUPING(orderYear),
The ROLLUP generates the subtotal every time the 'All Years',
year changes and the grand total at the end of the orderYear) orderYear,
result set. IF(GROUPING(productLine),
'All Product Lines',
productLine) productLine,
SUM(orderValue) totalOrderValue
GROUPING() function
FROM
To check whether NULL in the result set represents the
sales
subtotals or grand totals, you use
GROUP BY
the GROUPING() function.
orderYear ,
The GROUPING() function returns 1 when NULL occurs in a
productline
supper-aggregate row, otherwise, it returns 0.
WITH ROLLUP;
The GROUPING() function can be used in the select
list, HAVING clause, and (as of MySQL 8.0.12 ) ORDER
BY clause.
SELECT
orderYear,
productLine,
SUM(orderValue) totalOrderValue,
GROUPING(orderYear),
GROUPING(productLine)
FROM
sales
GROUP BY
When the query is executed, the subquery runs first and
returns a result set. Then, this result set is used as an input for
the outer query.
SELECT
customerNumber, checkNumber, amount
FROM
payments
WHERE
amount = (SELECT MAX(amount) FROM payments);
MySQL Subquery
A MySQL subquery is a query nested within another query
such as SELECT, INSERT, UPDATE or DELETE. In addition, a
subquery can be nested inside another subquery.
A MySQL subquery is called an inner query while the query
that contains the subquery is called an outer query. A
subquery can be used anywhere that expression is used and
must be closed in parentheses.
SELECT orderNumber,
SUM(priceEach * quantityOrdered) total
FROM orderdetails
INNER JOIN orders USING (orderNumber)
GROUP BY orderNumber
HAVING SUM(priceEach * quantityOrdered) > 60000;
Unlike a subquery, a derived table must have a an alias so that
you can reference its name later in the query. If a derived
table does not have an alias, MySQL will issue the following SELECT productName, sales
error: FROM
(SELECT
1 Every derived table must have its own alias. productCode,
ROUND(SUM(quantityOrdered * priceEach)) sales
FROM
orderdetails
INNER JOIN orders USING (orderNumber)
WHERE
YEAR(shippedDate) = 2003
GROUP BY productCode
ORDER BY sales DESC
LIMIT 5) top5products2003
INNER JOIN
products USING (productCode);
The following query gets the top 5 products by sales revenue
in 2003 from the orders and orderdetails tables in the sample
database:
SELECT productCode,
ROUND(SUM(quantityOrdered * priceEach)) sales
FROM orderdetails
INNER JOIN
orders USING (orderNumber)
In this example:
WHERE YEAR(shippedDate) = 2003
GROUP BY productCode
ORDER BY sales DESC 1. First, the subquery executed to create a result set or
LIMIT 5; derived table.
2. Then, the outer query executed that joined
the top5product2003 derived table with
the products table using the productCode column.
GROUP BY productCode
LIMIT 5;
SELECT
select_list
FROM
a_table
WHERE
Then, you can use this query as the derived table and perform [NOT] EXISTS(subquery);
grouping as follows:
If the subquery returns at least one row, the EXISTS operator
returns true, otherwise, it returns false.
In addition, the EXISTS operator terminates further processing
immediately once it finds a matching row, which can help
improve the performance of the query.
The NOT operator negates the EXISTS operator. In other words,
the NOT EXISTS returns true if the subquery returns no row,
otherwise it returns false.
MySQL UPDATE EXISTS examples
SELECT customerNumber, customerName Suppose that you have to update the phone’s extensions of
FROM customers the employees who work at the office in San Francisco.
WHERE
EXISTS (SELECT
The following statement finds employees who work at the
1
office in San Franciso:
FROM orders
WHERE
SELECT employeenumber, firstname, lastname,
orders.customernumber
Extension FROM employees
= customers.customernumber);
WHERE EXISTS ( SELECT 1
FROM offices
WHERE
city = 'San Francisco' AND
offices.officeCode = employees.officeCode);
( OR )
SELECT employeenumber,firstname,lastname,
extension FROM employees
INNER JOIN offices
USING(officeCode)
WHERE city = 'San Francisco' ;
In this example, for each row in the customers table, the
query checks the customerNumber in the orders table. If
the customerNumber, which appears in
the customers table, exists in the orders table, the
subquery returns the first matching row. As a result,
the EXISTS operator returns true and stops examining
the orders table. Otherwise, the subquery returns no row
and the EXISTS operator returns false.
SELECT
customerName, city, COALESCE(state, 'N/A'), MySQL GREATEST
country FROM customers;
and LEAST
Introduction to MySQL GREATEST
and LEAST functions
Both GREATEST and LEAST functions take N
arguments and return the greatest and smallest
values respectively.:
Another typical example of using
the COALESCE function is to substitute value in one
column by another when the first one is NULL. If any argument is NULL, the both functions
return NULLs immediately without doing any
comparison.
If functions are used in the INT or REAL
contexts, or all arguments are integer-valued
or REAL-valued, they are compared as INT
and REAL respectively.
If arguments consists of both numbers and IFNULL(1,0) returns 1 because 1 is not NULL .
strings, the functions will compare them as
numbers. SELECT IFNULL(NULL,'IFNULL function'); --
returns IFNULL function
If a least an argument is a non-binary
(character) string, the functions will compare IFNULL(NULL,'IFNULL
the arguments as non-binary strings. function') returns IFNULL function string
In all other cases, the functions compare because the first argument is NULL.
arguments as binary strings. SELECT company_id,
LEAST(IFNULL(q1, 0),
SELECT GREATEST(10, 20, 30), ----- 30 IFNULL(q2, 0),
LEAST(10, 20, 30); -- 10 IFNULL(q3, 0),
IFNULL(q4, 0)) low,
GREATEST(IFNULL(q1, 0),
SELECT GREATEST(10, null, 30), ----- null IFNULL(q2, 0),
LEAST(10, null , 30); -- null IFNULL(q3, 0),
IFNULL(q4, 0)) high
FROM revenues;
INSERT INTO revenues(company_id,q1,q2,q3,q4)
VALUES (1,100,120,110,130),
(2,250,260,300,310);
SELECT company_id,
LEAST(q1, q2, q3, q4) low,
GREATEST(q1, q2, q3, q4) high
FROM revenues;
IF NULL
SELECT
As you can see, the low and high values of the contactname, IFNULL(bizphone, homephone)
company id 3 are NULLs. phone FROM contacts;
DATE_ADD function
SELECT
DATE_ADD('1999-12-31 23:59:59',
DAY() function INTERVAL 1 SECOND) result;
+---------------------+
The DAY() function returns the day of the month | result |
of a given date. +---------------------+
| 2000-01-01 00:00:00 |
SELECT DAY('2010-01-15'); +---------------------+
SELECT DAY(orderdate) dayofmonth, We can add hour day month second by giving interval
COUNT(*) FROM orders and date_add etc..
WHERE YEAR(orderdate) = 2004
GROUP BY dayofmonth
ORDER BY dayofmonth;
DATE_SUB function +------------------------+
The DATE_SUB() function subtracts a time value SELECT LAST_DAY(CURDATE());
(or an interval) from a DATE or DATETIME value.
It will display the last date of that month
SELECT DATE_SUB('2017-07-04',INTERVAL 1
DAY) result; SELECT LAST_DAY(CURDATE() + INTERVAL 1
+------------+ MONTH);
| result |
+------------+ It will display the last date of next month
| 2017-07-03 |
DAYOFWEEK Function
If you want to get exact time at which the
The DAYOFWEEK function returns the weekday statement executes, you need to
index for a date i.e., 1 for Sunday, 2 for Monday, use SYSDATE()
… 7 for Saturday.
SELECT SYSDATE(), SLEEP(5), SYSDATE();
SELECT DAYNAME('2012-12-01'),
DAYOFWEEK('2012-12-01');
+-----------------------+-------------------------+
| DAYNAME('2012-12-01') | DAYOFWEEK('2012-12-
01') |
+-------------------------------+-------------------------+ It seems that
| Saturday | 7| both SYSDATE() and NOW() functions return a
same value which is the current date and time
LAST_DAY() function at which it is executed.
The LAST_DAY() function takes However, the SYSDATE() function actually
a DATE or DATETIME value and returns the last day of returns the time at which it executes while
the month for the input date. the NOW() function returns a constant time at
which the statement began to execute.
SELECT LAST_DAY('2016-02-03');
Output: 5 +-----------------------+-----------------------+
| DAYNAME('2010-01-01') | WEEKDAY('2010-01-
SELECT 01') |
TIMESTAMPDIFF (DAY, '2010-01-01', '2010-06- +-----------------------+-----------------------+
01') result; | Friday | 4|
+-----------------------+-----------------------+
Output: 151
SELECT
TIMESTAMPDIFF(MINUTE, '2010-01-01
10:00:00', '2010-01-01 10:45:00') result;
Output: 45
Note that the TIMESTAMPDIFF only considers the
time part that is relevant to the unit argument.
MySQL Window
Functions
CUME_DIST Function
In both examples, the aggregate functions
reduce the number of rows returned by the ROW_NUMBER() / total_rows
query.
SELECT name, score,
ROW_NUMBER() OVER (ORDER BY score)
Like the aggregate functions with the GROUP row_num,
BY clause, window functions also operate on a CUME_DIST() OVER (ORDER BY score)
subset of rows but they do not reduce the cume_dist_val
FROM scores;
number of rows returned by the query.
SELECT val,
DENSE_RANK() OVER ( ORDER BY val
) my_rank
For the third row, the function finds the number FROM t;
of rows with the values less than or equal to 62.
There are four rows. Then the result of
the CUME_DIST() function is: 4/10 = 0.4 which is
40%.
The same calculation logic is applied to the
remaining rows.
ROW_NUMBER
Function Rank
SELECT val,
RANK() OVER ( ORDER BY val ) my_rank
FROM t ;
SELECT ROW_NUMBER() OVER (
ORDER BY productName
) row_num, productName, msrp
FROM products
ORDER BY productName;
Sales table
Percentile Rank:
SELECT * FROM
(select e.employee_id,
rank() over(partition by
d.department_name order by salary
desc) RNK ,
e.salary ,
d.department_name
from employees e join departments d
on e.department_id = d.department_id)
A where A.RNK=2;