Grouping Records, Joins in SQL
Grouping Records, Joins in SQL
Structured Query
Language
GROUP BY clause
(returns a single value for a set of specified rows)
The GROUP BY clause can be used in a SELECT statement to collect data across multiple records
and group the results by one or more columns.
It groups the rows on the basis of the values present in one of the columns and then the
aggregate functions are applied on any column of these groups to obtain the result of the query.
Syntax:
select <col1, col2, …, colN>, <aggregate/statistical function>
from <tables>
where <conditions>
group by <col1, col2, …, colN>
1
18-10-2023
GROUP BY clause
Select deptno, count(*), max(sal)
from employee
GROUP BY deptno;
Note:
In a select statement containing GROUP BY clause only statistical
function and the attribute on which groups are formed can be used.
GROUP BY clause
2
18-10-2023
GROUP BY clause
HAVING clause
Having clause is used to filter the groups created by group by clause.
HAVING places condition only on the field on which group is made or on statistical function
3
18-10-2023
A={a, b, c} B={1,2,3}
AxB={(a,1), (a,2), (a,3), (b,1), (b,2),…..}
4
18-10-2023
Cartesian Product
Required for a SQL query when data needs to be extracted from two tables.
e.g. To display names of students along with the names of their stream from the following tables.
STUDENT STREAM
SID SNAME STR_CODE STR_CODE STR_NAME
111 ABHISHEK 01 01 SCIENCE
123 VINEET 02 02 COMMERCE
135 RAHUL 03 03 HUMANITIES
139 BARSHA 01
Cartesian product forms a new table of degree (c1+c2) and of cardinality (r1 x r2), where c1 and r1
are the degree and cardinality (respectively) of student and c2 and r2 is that of stream.
5
18-10-2023
STUDENT x STREAM
SID SNAME STR_CODE STR_CODE STR_NAME
111 ABHISHEK 01 01 SCIENCE
111 ABHISHEK 01 02 COMMERCE
111 ABHISHEK 01 03 HUMANITIES
123 VINEET 02 01 SCIENCE
123 VINEET 02 02 COMMERCE
123 VINEET 02 03 HUMANITIES
135 RAHUL 03 01 SCIENCE
135 RAHUL 03 02 COMMERCE
135 RAHUL 03 03 HUMANITIES
139 BARSHA 01 01 SCIENCE
139 BARSHA 01 02 COMMERCE
139 BARSHA 01 03 HUMANITIES
JOIN
JOIN operation combines tuples from two tables on specified conditions. This is unlike cartesian product
which make all possible combinations of tuples. While using the JOIN clause of SQL, we specify
conditions on the related attributes of two tables within the FROM clause. Usually, such an attribute is
the primary key in one table and foreign key in another table.
6
18-10-2023
E and D are table alias, temporary names of tables till the execution of SQL command.
EXAMPLE-2:
7
18-10-2023
NATURAL JOIN
The output of JOIN operation returns a table that has a repetitive column
having the same values. This redundant column provides no additional
information.
This is a much riskier join as with this join the query attempts to use all
columns with common names to create a join.
NATURAL JOIN
8
18-10-2023
9
18-10-2023
Following are some of the points to be considered while applying JOIN operations
on two or more relations:
10
18-10-2023
11