Class XII
Computer Science
Gist-05
THE RELATIONAL DATABASE MODEL-2 (RDBMS contd…)
AGGREGATE Functions:
• The Aggregate Functions are used to work on columns of data
• You can do various calculations on a column of data using these functions
• The functions are used with the SELECT clause or with the HAVING clause
• There are 5 Aggregate Functions: Count, Sum, Avg, Max, Min
• COUNT can work on any data-type
• SUM, AVG, MAX, MIN work on numeric data only
• You should not mix aggregate functions with other attributes under the select clause
• You cannot use the aggregate functions under the WHERE clause
The COUNT aggregate function is used to count the number of rows in a
table that satisfies one or more conditions.
SELECT Query-1:
Count the number of rows in marks table (i.e. find cardinality of the table).
SELECT COUNT(*) AS Rows
FROM marks ;
SELECT Query-2:
Count how many students scored more than 70 in subject S1.
SELECT COUNT(*) AS S1_Count
FROM marks
WHERE S1 > 70 ;
The SUM aggregate function finds the sum of the values under a given
column. You may or may not use conditions.
SELECT Query-3:
Find the total collection for the month of Feb 2020
SELECT SUM( Amount ) AS FebColln
FROM Fees
WHERE Year = 2020
AND Month = ‘Feb’ ;
The AVG aggregate function finds the average of the values under a
given column.
SELECT Query-4:
Find the average marks scored in subject S2 by the students with ID values
1210, 1230, 1460 and 1370.
SELECT AVG( S2 ) AS S2_Avg
FROM marks
WHERE ID IN ( 1210, 1230, 1460, 1370 ) ;
The MAX and MIN aggregate functions are used to find the maximum
and minimum of the values under a given column.
SELECT Query-5:
Find the maximum and minimum marks got in subject S3 by the students.
SELECT MAX(S3) AS S3Max , MIN(S3) AS S3Min
FROM marks ;
Use of Group By Having
Using GROUP BY Clause
• The GROUP BY clause is generally used along with a SELECT statement to arrange identical data sets into groups
• The aggregate functions can then be applied on groups of values
• In can be used both with and without a WHERE clause. However, when used along with the WHERE clause, it
should be placed after the WHERE clause
• GROUP BY is similar to ORDER BY in that both are used to basically sort data – Order By rows, & Group By groups
• Whereas the ORDER BY clause usually sorts a general set of data, the GROUP BY clause basically sorts similar
groups of data
SELECT Query-1:
Find a count of each individual city from where the
students are coming.
• The above query first groups the tuples based on
the City attribute
• As the student table has two different entries from
City i.e. ‘Howrah’ and ‘Kolkata’, hence the query
will first group the tuples with similar values of City
to form two different groups
• Then it will count the rows in each group formed
SELECT City, COUNT(*) AS Number
FROM student
GROUP BY City;
SELECT Query-2:
Find student strength class and section wise.
• The above query first groups the tuples based on
Class attribute
• As student table has 2 different classes, 11 and 12,
hence the query first groups the tuples with similar
class values to form 2 different groups
• Next, for all rows with same class values, it will group
tuples with same sections
• Then it will count rows in each such group
SELECT Class, Sec, COUNT(*) AS Total
FROM student
GROUP BY Class, Sec;
Using HAVING clause
• The HAVING clause is used to apply a condition on groups of tuples
• The aggregate functions can then be used in the condition used with the HAVING clause on the groups of values
• REMEMBER that the WHERE clause conditions work on individual tuples. Hence you CANNOT use an aggregate
function in the condition of a WHERE clause
• But, as the HAVING clause works on groups of values, hence you can use the aggregate functions in a HAVING
clause condition
SELECT Query-3:
Find student strength class and section wise for sections with more than 1 student strength.
• The above query first groups the tuples based on same Class values
• Then it groups tuples based on Sec values for tuples with same Class values
• Then it will count rows in each such group
• Finally, it checks those groups to see the Total count
to be more than 1 and displays only those groups
FROM student
GROUP BY Class , Sec
HAVING COUNT(*) > 1 ;
Writing JOIN QUERIES
A JOIN is used to query data from more than one tables, based on one or more conditions.
Join SELECT Query-1:
Display the Purchase ID, ICode, Quantity, Description and Price of all items purchased
Steps to follow:
1) Find the primary key and foreign key pair from the tables to be joined. Here, ICode under item table is the
Primary Key, which is used as the attribute ICode in the purchase table
2) Under the WHERE clause equate both the primary key and the
foreign key attributes from the two tables
3) Apply any other condition or clause as per the question
SELECT PID , purchase . ICode , Qty , Descp , Price
FROM purchase , item
WHERE purchase . ICode = item . ICode ;
[When using the primary or foreign key under select, prefix it with
table-name and dot, as shown in the above query]
Join SELECT Query-2:
Display the Purchase ID, Description and Qty of all items purchased
where quantity > 1.
SELECT PID , Descp , Qty
FROM purchase , item
WHERE purchase . ICode = item . ICode
AND Qty > 1 ;
Join SELECT Query-3:
Display the Purchase ID, Description, Quantity, and total amount of all
items purchased before 15/9/2020
SELECT PID , Descp , Qty , Qty*Price AS Amt
FROM purchase , item
WHERE purchase . ICode = item . ICode
AND Date < ‘2020-09-15’ ;
SQL Joins: Types of JOINS
1. Cartesian Product: It is an SQL join without any condition. Here each row of first table will combine with each
row of second table. Final table has all attributes from both tables.
2. Equi-Join: It is an SQL join that joins two or more tables based on a condition using equality operator
SELECT *
FROM Table1, Table2
WHERE Table1.B = Table2.D ;
3. Natural Join: It is an Equi join where the join condition compares all the same name columns in both the tables.
The duplicate columns are then removed from the result.
SELECT * SELECT *
FROM Table1, Table2 OR FROM Table1
WHERE Table1.C = Table2.C; NATURAL JOIN Table2;
A NATURAL JOIN can be an INNER join, a
LEFT OUTER join, or a RIGHT OUTER join.
The default is INNER join.
4. Inner Join: It is an Equi join where only those rows are selected from both tables that satisfy the join condition. It
may involve more than one column and condition. All columns are kept.
SELECT * SELECT columns
FROM table1 FROM table1
i.e.
INNER JOIN table2 ON table1.C = ‘mat’; INNER JOIN table2 ON condition;
5. Left Join: It is type of join that selects rows from both left and right tables that are matched, plus rows from left
table even with no matching rows in the right table.
SELECT *
FROM Table1
LEFT OUTER JOIN Table2 ON Table1.C = Table2.C;
6. Right Join: It is type of join that selects rows from both left and right tables that are matched, plus rows from
right table even with no matching rows in the left table.
SELECT *
FROM Table1
RIGHT OUTER JOIN Table2 ON Table1.C = Table2.C
7. Full Outer Join: It is type of join that selects rows from both left and right tables that are matched plus rows
from the right and left tables even without matching rows.
SELECT *
FROM Table1
FULL OUTER JOIN Table2 ON Table1.C = Table2.C;
Difference between Natural Join and Inner Join
SOME MYSQL Commands
Some MySQL commands to work with a database are given below:
• Create new database: (to create a database first, before the tables are created inside the database)
create database database_name;
Example: to create employee database
• Select a database: (to create or view contents of a table, you need to enter the database first using this
command)
use database_name;
Example: to move to the database employee
• To determine what database is in use: Once you have entered a database, to see the name of the database you
are in, this command is used.
select database();
Example: Once you have entered the employee database, to view the name of the database:
• Deleting a database: To remove or delete an existing database this command is used.
drop database database_name;
Example: To delete the employee database the following command is to be used:
• Show all tables in a database: To view the names of all the tables in a database this command is used
show tables;
Example: To see the names of all the tables under the shop database the following is to be written:
• Show a table structure: To see the structure of a given database table this command is used.
describe table_name; OR desc table_name;
Example: To see the field names, data types, and constraints of the item table the following is to be written: