0% found this document useful (0 votes)
12 views

CBSE Class 12 Informatics Practices More On SQL-Grouping Records and Table Joins

joining questions of mysql

Uploaded by

Monika Vashisht
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

CBSE Class 12 Informatics Practices More On SQL-Grouping Records and Table Joins

joining questions of mysql

Uploaded by

Monika Vashisht
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Downloaded from www.studiestoday.

com

CHAPTER 15: MORE ON SQL-GROUPING RECORDS AND TABLE JOINS


SQL Aggregate (Group) Function: These function operates on group of rows instead of single
row that is why these functions are known as group or aggregate functions. Some important group
functions are as follows:
TABLE:Student
Rollno Sname Subject Marks grade
001 SUMIT MATHS 95 A
002 SHERRY IP 96 A
003 SUMAN IP 75
004 LALIT HINDI 84 B
005 RAHUL MATHS 88 B

Name of Purpose/Use Syntax or Example OUTPUT


the
MAX() Returns the MAXIMUM values in a Mysql> select max(marks) 96
specified column from student;
MIN() Returns the MINIMUM values in a Mysql> select min(marks) 75
specified column from student;
SUM() Returns the SUM of values in given Mysql> select sum(marks) 438
column/expression. from student;
AVG() Returns the AVERAGE value in the Mysql> select avg(marks) 87.6
specified column/expression. from student;
COUNT() Returns the total number of non null Mysql> select count(grade) 4
values in a column. from student;
COUNT(*) Returns the total no of rows Select Count(*) from student; 5

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

Downloaded from www.studiestoday.com


Downloaded from www.studiestoday.com

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

Downloaded from www.studiestoday.com


Downloaded from www.studiestoday.com

NON EQUI JOINS:


Non equi join is a query that specifies some relationship other than equality between the columns.
It means that records are joined on the condition other than Equal operator (<,>,<>,>=,<=) for
joining column.

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)

SN Pcode scode Name qty


1 P101 S002 SOAP 20
2 P102 S003 SOAP 20

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

Using multiple tables with FROM clause


Select RNO, Name from student join streams on student.scode= streams.scode where
stream=”Science”
OUTPUT
RNO Name Stream
101 RAM Science
104 SHERRY Science

66

Downloaded from www.studiestoday.com


Downloaded from www.studiestoday.com

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

SELECT column_name(s) FROM table1


UNION
SELECT column_name(s) FROM table2;

Note: The UNION operator selects only distinct values by default. To allow duplicate values, use the
ALL keyword with UNION.

Syntax :- UNION ALL

SELECT column_name(s) FROM table1


UNION ALL
SELECT column_name(s) FROM table2;

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

Downloaded from www.studiestoday.com


Downloaded from www.studiestoday.com

|2|B|
|3|C|
|4|D|
+------+------+

SELECT * FROM a UNION ALL SELECT * FROM b;


+------+------+
|x|y|
+------+------+
|1|A|
|2|B|
|3|C|
|4|D|
|1|A|
|3|C|
+------+------+

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.

Syntax The syntax for the INTERSECT operator in MySQL is:

SELECT expression1, expression2, ... expression_n


FROM tables
[WHERE conditions]
INTERSECT
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions];

68

Downloaded from www.studiestoday.com


Downloaded from www.studiestoday.com

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.

Very Short Answer Type Question (1 Marks)


1. What is single row and multiple row functions?
2. What is the significance of Group By clause in MYSQL?
3. What is Join? How many types of joins are there?
4. What are joins? Why are they used?
5. How natural join differs from Equi Join?
6. What is the Cartesian product of two tables? Is it same as an Equi-join?
7. There is a column C1 in a table T1. The following two statements:
SELECT COUNT (*) FROM T1; and SELECT COUNT(C1) from T1;
are giving different outputs. What may be the possible reason? What is the significance of NOT
NULL constraints?
8. There are two tables T1 and T2 in a database. Cardinality and degree of T1 are 2 and 4
respectively. Cardinality and degree of T2 are 3 and 2 respectively. What will be the degree and
Cardinality of their Cartesian product?
9. Do aggregate Functions consider Null values? Does NULL play any role in actual calculations?
10. Write a query to delete a column pincode from the table employee?
11. Write a query to display the highest marks of each subject where Max marks is more than 90
from table student
12. Write a statement to disable the constraints of table.
13. Write a query to display the number of employees in each department in table emp.

69

Downloaded from www.studiestoday.com


Downloaded from www.studiestoday.com

Short Answer Type Question (2 Marks)


1. Difference between WHERE and HAVING clause in MySQL? Explain with the help of an example.
2. Consider the tables Doctors and Patient given below:
TABLE: DOCTORS
DocID DocName Department OPDdays
101 K.K.Mathur ENT TTS
102 Ashish Sharma Paed MWF
201 Vivek Khurana Ortho MWF
TABLE: PATIENT
PatNo PatName Department DocID
1 AKASH ENT 101
2 NEHA Ortho 102
3 SUNITA ENT 101
3. With reference to these two tables, write a SQL query for (i) and (ii) and output for (iii).
(1) Display Patient Name, Patient No and corresponding doctor name for each patient.
(2) Display the list of all patients who’s OPDdays are ‘TTS’.
(3) SELECT OPDdays, count(*) FROM Doctors, Patients WHERE
Doctors.Department=Patients.Department GROUP BY OPDdays;
4. In a database there are two table BOOKS and ISSUES.
Table: BOOKS
Book_ID Book_Name Author_Name Publisher Price Qty
L01 Maths Raman ABC 70 20
L02 Science Agarkar DEF 90 15
L03 Social Suresh XYZ 85 30
L04 Computer Sumita ABC 75 7
L05 Telugu Nannayya DEF 60 25
L06 English Wordsworth DEF 55 12
Table: ISSUES
ISSUE_ID Book_ID Qty_Issued
14 L02 13
19 L04 5
3 L05 21
i. How many rows and how many columns will be there in the Cartesian product of these two
tables?
ii. Which column in the 'ISSUES' table is the foreign key?
5. Consider the table staff and salary given below:-
Table: staff Table: Salary

ID NAME DEPT SEX DATE_OF_J


101 Siddharth SALES M 2001-01-01
104 Raghav FINANCE M 2006-02-14
107 Prateek RESEARCH M 2002-07-02
114 Dilip SALES M 2003-05-15
70

Downloaded from www.studiestoday.com


Downloaded from www.studiestoday.com

109 Nupur FINANCE F 2004-11-11


105 Binoy RESEARCH F 2002-10-10
117 Vaibhav SALES M 2005-08-11
111 Rama FINANCE F 2004-02-23

ID BASIC ALLOWANCE COMM


101 12000 1000 3
104 23000 2300 5
107 32000 4000 5
114 12000 5200 10
109 42000 1700 20
105 18900 1690 3

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

Downloaded from www.studiestoday.com

You might also like