18-10-2023
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>
Set of columns with select has to be same as with group by.
1
18-10-2023
GROUP BY clause
Select deptno, count(*), max(sal)
from employee
GROUP BY deptno;
DEPTNO COUNT(*) MAX(SAL)
30 6 2850
20 5 3000
10 3 5000
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
select deptno, job, count(*)
from employee
group by deptno, job;
2
18-10-2023
GROUP BY clause
select deptno, job, count(*)
from employee
group by deptno, job
order by deptno, job;
HAVING clause
Having clause is used to filter the groups created by group by clause.
select job, count(*), avg(sal)
JOB COUNT(*) AVG(SAL)
from employee
group by job CLERK 4 1037.5
having job = 'CLERK';
select job, count(*), avg(sal) JOB COUNT(*) AVG(SAL)
from employee
PRESIDENT 1 5000
group by job
having count(*)<3; ANALYST 2 3000
HAVING places condition only on the field on which group is made or on statistical function
3
18-10-2023
Order of combining clauses
SELECT column list
FROM <table name>
WHERE <predicate>
GROUP BY <column name(s)>
HAVING <search condition>
ORDER BY column_name;
Extracting data from two tables
Department table is:
A={a, b, c} B={1,2,3}
AxB={(a,1), (a,2), (a,3), (b,1), (b,2),…..}
4
18-10-2023
Sample Relation: Employee
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
Cartesian product of tables Student and Stream
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.
SELECT ENAME, DNAME FROM EMPLOYEE E JOIN DEPARTMENT D ON [Link] = [Link];
6
18-10-2023
EQUI JOIN (join in which columns are compared for equality)
Join is Cartesian Product of two tables ENAME DNAME
Equi join returns data from two tables using equality operator SMITH RESEARCH
ALLEN SALES
select ename, dname WARD SALES
from employee, department JONES RESEARCH
where [Link] = [Link] MARTIN SALES
OR BLAKE SALES
select ename, dname CLARK ACCOUNTING
from employee E, department D SCOTT RESEARCH
KING ACCOUNTING
where [Link] = [Link]
TURNER SALES
OR
ADAMS RESEARCH
select ename, dname
JAMES SALES
from employee E JOIN department D
FORD RESEARCH
ON [Link] = [Link]
MILLER ACCOUNTING
E and D are table alias, temporary names of tables till the execution of SQL command.
EXAMPLE-2:
SELECT ENAME, DNAME, JOB FROM EMPLOYEE E, DEPARTMENT D
WHERE [Link] = [Link] AND JOB IN ('CLERK', 'SALESMAN', 'ANALYST');
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.
NATURAL JOIN is an extension of JOIN which works similar to JOIN clause
in SQL but removes the redundant attribute. This operator can be used to
join the contents of two tables if there is one common attribute in both
the tables. Common columns are columns that have the same name in
both tables.
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:
If two tables are to be joined on equality condition on the common attribute,
then one may use JOIN with ON clause or NATURAL JOIN in FROM clause. If
three tables are to be joined on equality condition, then two JOIN or NATURAL
JOIN are required.
In general, N-1 joins are needed to combine N tables on equality condition.
With JOIN clause, we may use any relational operators to combine tuples of
two tables.
A sample SQL command:
SELECT [Link], DNAME, EMPNO, [Link], JOB, SAL
FROM EMPLOYEE E, DEPARTMENT D
WHERE [Link] = [Link]
AND DNAME='SALES' AND SAL>=1500
ORDER BY [Link], EMPNO;
10
18-10-2023
1) Create Suppliers with SuppNo as
primary key, supp_Name as NOT
NULL, status (value>=10)
2) Create table items with
itemNo as primary key,
item_Name not null
3) Create table Shipments
with SuppNo+ItemNo as
primary key, where each of
SuppNo and ItemNo are
Foreign key and
QtySupplied is >=10.
11