Database Management and MySQL Part2 CS
Database Management and MySQL Part2 CS
- Aggregate functions
- Group by clause with Having
- Joins
SQL commands
► Aggregate Functions (MULTI ROW FUNCTIONS)
Sum()
Sum() calculates the arithmetic SUM of all selected values of a given column holding
numeric entries.
Example
Select SUM (Basic) from employee;
Output :
SUM(BASIC)
19500
MySQL commands
► Aggregate Functions
Example :
AVG(BASIC)
5500
MySQL functions
► Aggregate Functions
Example :
Output:
MAX(BASIC)
6000
MySQL functions
► Aggregate Functions
Example :
Output:
MIN(BASIC)
3000
MySQL functions
► Multiple Row functions / Aggregate Functions
Count() - Count() counts the number of rows in the output table. Count() with the asterisk(*)
counts the number of rows in the table including NULLs.
Example 1:
Select count(*) from employee; Select count(DEPT) from employee; WILL DISPLAY 3
will count the number of records present in a table including Nulls.
Example 2:
will count the number of employee records who are in admn dept.
Output:
COUNT(*) Table : employee
2 empno name basic dept dob
101 Ravi 5000.00 accts 2001-02-09
102 Khyati 3000.00 admn 2002-09-08
103 Surbhi 6000.00 NULL 2003-08-03
104 Rohit 5500.00 admn 2001-09-08
MySQL functions
► Multiple Row functions / Aggregate Functions
Example 3:
will count the number of employee records with non Null dept values.
Output:
COUNT(DEPT) Table : employee
empno name basic dept dob
3
101 Ravi 5000.00 accts 2001-02-09
102 Khyati 3000.00 admn 2002-09-08
103 Surbhi NULL NULL 2003-08-03
104 Rohit 5500.00 admn 2001-09-08
SQL commands
► Group By clause
The GROUP BY clause used with SELECT statement is used to group rows that have
the same values.
Example :
This command will group all records which have the same value of DEPT and then
displays the average of salary for each of all groups.
The HAVING clause places condition on groups . These include group functions while
placing conditions.
This statement will display no of records in each department where the number of
employees is greater than 1 Table : employee
The HAVING clause places condition on groups . These include group functions while
placing conditions.
MAIL PROJECTS AT :
computerdepartmentdpsvk@gmail.com
SQL commands
► Joins
When information from more than one tables are to be extracted JOIN concept is
used. A join command combines rows from two or more tables. The main purpose of
joining tables is to establish a link between the tables.
➢ Equi join
➢ Natural join
SQL commands - Joins
OR
Select * from <table 1> join <table 2>
on <table 1>.column_name = <table 2>.column_name;
Q2. DISPLAY THE DETAILS OF ALL EMPLOYEES WHOSE NAMES BEGIN WITH R.
ANS: SELECT * FROM EMPL WHERE NAME LIKE “R%”;
Example :
deptno from
select * from empl,dept_ment where empl.dptno=dept_ment.deptno; both the tables
The output would be: displayed
select * from empl , dept_ment where empl.deptno=dept_ment.deptno and basic > 5000;
Command with selective columns and additional condition along with join condition.
Example :
and the primary key. Where will be used only if the query contains a
specific condition to select specific tuples.
[WHERE <Conditon>]
Table : STORE
Table : Suppliers
Itemno Item Scode Qty Rate Lastbuy Scode Sname
2005 Sharpener Classic 23 60 8 2009-06-31 21 Premium Stationers
2003 Ball Pen 0.25 22 50 25 2010-02-01 23 Soft Plastics
2002 Gel Pen Premium 21 150 12 2010-02-24 22 Tetra Supply
2006 Gel Pen Classic 21 250 20 2009-03-11
(ii) To increase the Qty by 10 of all records of Store whose Lastbuy is before 01-feb-
2010
(iii) To display minimum rate of items for each supplier as per Scode.
(viii) To display all records from Store table whose Item name starts from G
(xi) To find sum of rates of all items whose item names starts from ‘Gel’;
(xii) To delete all records from Store table whose rate is less than 10.
scode max(qty)
21 250
22 220
23 60
(ii) To display the details of all the items whose quantity is more than 10.
(iii) To display the name, (price * quantity) with the column heading “Total Value” of
all the items
(iv) To increase price of all items by 10% which are in “cos” category
Update shop set price = price + 0.1 * price where category = “cos”;
Solved Example
(v) To display all the records which are starting with the letter “B”.
Select * from shop where name like ‘B%’;
(vi) Delete all the records which are purchased in the year 2014.
Delete from shop where Purchase_date between ‘2014-01-01’ and ‘2014-12-31’ ;
(vii) To display all the item names whose price is less than 50.
Select name from shop where price < 50;
(ix) Display all the records whose quantity is more than 50 in increasing order of price;
(xi) Display the records of all the items whose salesman name contains the word
“singh” .
(xiv) Add a new column “Discount” to store discount with 5 integer positions and 2
decimal places
category min(price)
cos 200
gro 100
sta 40
b. Select category, max(quantity) from shop group by category having count(*) > 1;
category max(quantity
gro 130
sta 100
Solved Example
Consider the table shown below:
SCHOOL
CODE TEACHERNAME SUBJECT DOJ PERIOD EXPERIENCE
1001 RAVI SHANKAR ENGLISH 2000-12-03 S24 10
1009 PRIYA RAI PHYSICS 1998-09-03 26 12
1203 LISA ANAND ENGLISH 2000-04-20 27 5
1045 YASHRAJ MATHS 2000-08-02 24 15
1123 GANAN PHYSICS 1999-07-19 28 3
1167 HARISH B CHEMISTRY 1999-10-19 27 5
1215 UMESH PHYSICS 1998-05-11 22 16
Solved Example
(i) To display TEACHERNAME, PERIOD of all teachers whose periods are less than
equals to 25.
Select teachername ,period from school where periods <=25;
(ii) To display the subject wise minimum PERIOD.
(iii) To display CODE, TEACHERNAME and SUBJECT of all teachers who have joined the
school in the year 1999.
Select code, teachername, subject from school where doj between ‘1999-01-01’
and ‘1999-12-31’;
(vi) Remove all the records from the table whose teachername begins with ‘LI’.
(xi) To display average experience of those who have joined in the year 2000.
‘2000-12-31’;
(xii) To increase the period value by 5 of all teachers whose subject is ‘ENGLISH’.
Update school set period=period + 5 where subject=‘ENGLISH’;
Solved Example
Give the outputs for the following according to the tables given below:
MAX(EXPERIENCE) SUBJECT
5 CHEMISTRY
10 ENGLISH
15 MATHS
16 PHYSICS
b. SELECT COUNT(*) FROM SCHOOL WHERE PERIOD > 25;
COUNT(*)
4
Solved Example
Give the outputs for the following according to the tables given below:
COUNT(DISTINCT SUBJECT)
b. SELECT 0.5*PERIOD “REDUCED NO” FROM SCHOOL WHERE SUBJECT LIKE “%S”;
REDUCED NO
13
12
14
11
Assessment
Which of the following is not an aggregate function?
a. AVG()
b. SUM()
c. TOTAL()
d. MIN()
Answer : c. TOTAL()
If a table Course has columns- Admno, Name, Aggregate, Stream. Give command
to display highest aggregate obtained in each stream.
a. SELECT STREAM, MAX(AGGREGATE) FROM COURSE;
b. SELECT STREAM, MAX(AGGREGATE) FROM COURSE GROUP BY STREAM;
c. SELECT STREAM, MAX(AGGREGATE) FROM COURSE ORDER BY STREAM;
Answer : b . SELECT STREAM, MAX(AGGREGATE) FROM COURSE GROUP BY STREAM;
Assessment
Which command is used to select a database to make it current?
a. Open databasename
b. Use databasename
c. Select databasename
d. None of the above
Answer : b Use databasename
Answer : b where
Assessment
In which join , there are no duplicate columns?
a. Equi join
b. Natural join
c. Cross join
d. None of the above
Answer : b Natural join
Aggregate functions can be used in the select list or the _____ clause of a select statement
.They cannot be used in a _______ clause.
a. Where, having
b. Having, where
c. Group by, having
d. Group by , where