CBSE Class 12 Informatics Practices More On SQL-Grouping Records and Table Joins
CBSE Class 12 Informatics Practices More On SQL-Grouping Records and Table Joins
com
TYPES OF FUNCTIONS:
Single Row Function: It works with a single row at a time. A single row function returns a result for
every row of the queried table
Multiple Row or Group Function: It works with data of multiple rows at a time and returns
aggregate values.
The main difference between these two functions is in the number of rows they act upon.
Grouping Result by using Group By: The GROUP BY clause is used in a select statement in
conjunction with aggregate functions to group the result based on DISTINCT or ALL values in a
column. Grouping can be done by column name, or with aggregate functions in which case the
aggregate produces a value for each group. Example:
Mysql> select count (marks) from student Group By subject;
OUTPUT:
HINDI 1
MATHS 2
IP 2
Mysql> select SUM (marks) from student Group by Subject;
OUTPUT:
HINDI 84
64
MATHS 183
IP 171
Conditions on Group-Having Clause: We can use any condition on group, if required. HAVING
clause is used to apply a condition on a group.
Mysql> select SUM (marks) from student Group By subject having MAX(marks)>80;
OUTPUT:
HINDI 84
MATHS 183
IP 96
Mysql> Select SUM (marks) from student Group By subject having COUNT (*)>1;
OUTPUT:
MATHS 2
IP 2
JOINS: A join is a query that combines rows from two or more tables. In a query involving join,
more than one table is listed in FROM clause separated by comma (,).
EXAMPLE: SELECT * FROM EMP1, DEPT;
CROSS JOIN (Cartesian product): Cartesian product of two tables is a table obtained by pairing each
row of one table with each row of the other. It contains all the columns of both tables.
e.g. mysql > SELECT * FROM Order, product ;
Note:-In final table cardinality will be multiplication of rows of both tables and degree will be
addition of columns of both tables.
Order Product
SN Pcode scode code Name qty
1 P101 S002 P101 SOAP 20
2 P102 S003 P102 OIL 10
(After Cross Join following output will be produced)
SN Pcode scode code Name qty
1 P101 S002 P101 SOAP 20
2 P102 S003 P101 SOAP 20
1 P101 S002 P102 OIL 10
2 P102 S003 P102 OIL 10
This table will contain (2*2=4) rows and (3+3=6) Columns.
EQUI JOINS:
The Joins, in which a column is compared for equality, is called Equi-Join. The Join Column is a
column which is common in both tables.
SQL Statement: mysql> SELECT * FROM Order, product where order.pcode=product.pcode;
(After Equi Join following output will be produced)
SN Pcode scode Pcode Name qty
1 P101 S002 P101 SOAP 20
2 P102 S003 P102 OIL 10
MySQL creates a Cartesian product of the tables; from this Cartesian product MySQL select only
those records for which Pcode of order table matches with code of product table.
65
NATURAL JOINS: The join in which one of the identical columns exist, is called Natural Join. The
natural join is much similar to Equi-Join, records are joined on the equality condition of joining
column except that the common column appears one time.
(After Natural Join following output will be produced)
Joining Tables Using Join Clause of SQL Statement: MySQL offer two ways by which you may join
two or more tables. One is using multiple tables with FROM clause and using JOIN keyword with
FROM clause.
Using multiple tables with FROM clause
Select * from Student, Stream where student.scode= stream.scode;
Table: Student Table: Stream
RNO Name Subject Fee Scode
Scode Stream
101 RAM MATHS 1000 S101
S101 Science
102 SHAM ECO 800 C102
C102 Commerce
103 RITU ENG 500 H103
H103 Humanities
104 SHERRY PHY 1200 S101
OUTPUT
RNO Name Subject Fee Scode Stream
101 RAM MATHS 1000 S101 Science
102 SHAM ECO 800 C102 Commerce
103 RITU ENG 500 H103 Humanities
104 SHERRY PHY 1200 S101 Science
66
UNION:-The UNION operator is used to combine the result-set of two or more SELECT statements.
Notice that each SELECT statement within the UNION must have the same number of columns. The
columns must also have similar data types. Also, the columns in each SELECT statement must be in
the same order.
Syntax : UNION
Note: The UNION operator selects only distinct values by default. To allow duplicate values, use the
ALL keyword with UNION.
Note: The column names in the result-set of a UNION are usually equal to the column names in the
first SELECT statement in the UNION. The number of Columns must be same In both the tables.
SELECT * FROM a;
+------+------+
|x|y|
+------+------+
|1|A|
|2|B|
|3|C|
|4|D|
+------+------+
SELECT * FROM b;
+------+------+
|x|y|
+------+------+
|1|A|
|3|C|
+------+------+
Union:
SELECT * FROM a UNION SELECT * FROM b;
+------+------+
|x|y|
+------+------+
|1|A|
67
|2|B|
|3|C|
|4|D|
+------+------+
INTERSECTION
First, let's explain what an INTERSECT query is. An INTERSECT query returns the intersection of 2 or
more data sets. If a record exists in both data sets, it will be included in the INTERSECT results.
However, if a record exists in one data set and not in the other, it will be omitted from the
INTERSECT results.
Intersect Query
Explanation: The INTERSECT query will return the records in the shaded area. These are the records
that exist in both Dataset1 and Dataset2.
68
Although there is no INTERSECT operator in MySQL, you can easily simulate this type of query using
either the IN clause or the EXISTS clause, depending on the complexity of the INTERSECT query.
69
6. With reference to these tables, Write commands in SQL for (i) and (ii) and output for (iii) below:
i. Display NAME, BASIC, ALLOWANCE of all staff who are in “SALES” department
ii. Display the average salary of all the staff working in “FINANCE” department
using the table staff and salary. SALARY=BASIC+ALLOWANCE.
iii. SELECT NAME, COMM FROM staff, salary where (staff.ID=salary.ID);
71