Assignment 7 - Modified
Assignment 7 - Modified
Aim
• Group functions
Objective
Theory
Data Manipulation Language (DML) allows you to modify the database instance by inserting,
modifying, and deleting its data. It is responsible for performing all types of data modification in
a database.
There are three basic constructs which allow database program and user to enter data and
information are:
INSERT
UPDATE
DELETE
Insert
INSERT INTO table_name
(col1, col2,…..)
values
(col1_value, col2_value, ……)
Update
update table_name
set
column_name::expression
….
where condition
Delete
delete from table_name
where condition;
MySQL Inbuilt Functions
The MID() function extracts a substring from a string (starting at any position).
Aggregate Functions
The data that you need is not always stored in the tables. However, you can get it by performing
the calculations of the stored data when you select it.
For example, you cannot get the total amount of each order by simply querying from the order
details table because the order details table stores only quantity and price of each item. You have
to select the quantity and price of an item for each order and calculate the order’s total.
By definition, an aggregate function performs a calculation on a set of values and returns a single
value.
MySQL provides many aggregate functions that include AVG, COUNT, SUM, MIN, MAX, etc.
An aggregate function ignores NULL values when it performs calculation except for the
COUNT function.
AVG function
The AVG function calculates the average value of a set of values. It ignores NULL values in the
calculation.
select avg(Salary) from Employee;
SUM function
The SUM function returns the sum of a set of values. The SUM function ignores NULL values.
If no matching row found, the SUM function returns a NULL value.
MAX function
MIN function
Nested Queries
A Subquery or Inner query or a Nested query is a query within another SQL query and
embedded within the WHERE clause.
A subquery is used to return data that will be used in the main query as a condition to further
restrict the data to be retrieved.
Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE statements along
with the operators like =, <, >, >=, <=, IN, BETWEEN, etc.
There are a few rules that subqueries must follow −
Subqueries must be enclosed within parentheses.
A subquery can have only one column in the SELECT clause, unless multiple columns
are in the main query for the subquery to compare its selected columns.
An ORDER BY command cannot be used in a subquery, although the main query can
use an ORDER BY. The GROUP BY command can be used to perform the same
function as the ORDER BY in a subquery.
Subqueries that return more than one row can only be used with multiple value operators
such as the IN operator.
The SELECT list cannot include any references to values that evaluate to a BLOB,
ARRAY, CLOB, or NCLOB.
A subquery cannot be immediately enclosed in a set function.
The BETWEEN operator cannot be used with a subquery. However, the BETWEEN
operator can be used within the subquery.
Example Queries:
Inbuilt Functions
4. select count(EmpID) from Employee where DOJ between '2016-01-01' and '2016-12-31';
8. select DId, sum(salary) as totalsal from Employee group by DId having totalsal>150000;
9. select DId, sum(salary) as totalsal from Employee where DOJ <= now() group by DId;
10. select DId, sum(salary) as totalsal from Employee where DOJ between '1992-01-01' and
date() group by DId;
Nested Queries
1. select * from employee where Salary in (select max(Salary) from employee);
2. select * from employee where Salary not in (select max(Salary) from employee);
3. select Salary from employee where Salary <> any (select e.Salary from employee as e where
e.did=11);
4. select Salary from employee where Salary > any (select e.Salary from employee as e where e.did=11);
5. select Salary from employee where Salary > some (select e.Salary from employee as e where
e.did=11);
6. select Salary from employee where Salary = some (select e.Salary from employee as e where
e.did=11);
7. select * from employee where Salary >= all (select Salary from employee);
8. select Salary from employee where Salary > all (select e.Salary from employee as e where e.did=11);
9. select Salary from employee where exists (select e.Salary from employee as e where
e.did=employee.did and did=11);
10. select Salary from employee where not exists (select e.Salary from employee as e where e.did=11
and e.did=employee.did);
Join
Natural Join
Inner Join