0% found this document useful (0 votes)
42 views4 pages

Experiment No: 04 Aim: To Use Some SQL Aggregate Functions

The document describes using SQL aggregate functions like AVG(), COUNT(), MAX(), MIN(), and SUM() to return aggregated values from columns in a table. It provides examples of implementing these functions on a sample PRODUCT table. It also covers defining foreign key constraints using the FOREIGN KEY clause to link columns between tables and prevent invalid data. Examples are given of creating tables with foreign keys and inserting records.

Uploaded by

Anuj Uppal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views4 pages

Experiment No: 04 Aim: To Use Some SQL Aggregate Functions

The document describes using SQL aggregate functions like AVG(), COUNT(), MAX(), MIN(), and SUM() to return aggregated values from columns in a table. It provides examples of implementing these functions on a sample PRODUCT table. It also covers defining foreign key constraints using the FOREIGN KEY clause to link columns between tables and prevent invalid data. Examples are given of creating tables with foreign keys and inserting records.

Uploaded by

Anuj Uppal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

EXPERIMENT NO: 04

AIM: TO USE SOME SQL AGGREGATE FUNCTIONS:


SQL aggregate functions return a single value, calculated from values in a column.
Useful aggregate functions:
i. AVG() Returns the average value
ii. !U"#() Returns the num$er of ro%s
iii. &'RS#() Returns the (rst value
iv. LAS#() Returns the last value
v. )A*() Returns the largest value
vi. )'"() Returns the smallest value
vii. SU)() Returns the sum
SYNTAX:
SELECT AVG(column_name) FROM table_name
SELECT COUNT(column_name) FROM table_name;
SELECT COUNT(*) FROM table_name;
SELECT COUNT(DSTNCT column_name) FROM table_name;
SELECT FRST(column_name) FROM table_name;
SELECT LAST(column_name) FROM table_name;
SELECT MA!(column_name) FROM table_name;
SELECT MN(column_name) FROM table_name;
SELECT SUM(column_name) FROM table_name;
IMPLEMENTATION"
S#L*$lu%" Relea%e &'()('(&(' * $+o,uct-on on Sat Au. /& &/"&0")' )'&/
Co12+-.3t (c) &45)6 )''76 O+acle( All +-.3t% +e%e+8e,(
S#L9 CONNECT
Ente+ u%e+*name" S:STEM
Ente+ 1a%%;o+,"
Connecte,(
S#L9 CREATE TA<LE $RODUCT
) ( $_D NT NOT NULL $RMAR: =E:6
/ TTLE VARC>AR)(&7) 6
0 #T: NT NOT NULL6
7 $RCE NT NOT NULL6
? S_D NT NOT NULL);
Table c+eate,(
S#L9 NSERT NTO $RODUCT VALUES(&6@SOA$@6)''6&'6&'&);
& +o; c+eate,(
S#L9 NSERT NTO $RODUCT VALUES()6@S>AM$OO@6&'67'6&'&);
& +o; c+eate,(
S#L9 NSERT NTO $RODUCT VALUES(/6@TOOT>$ASTE@6/76?76&'));
& +o; c+eate,(
S#L9 NSERT NTO $RODUCT VALUES(06@<RUS>@64'6&)6&'));
& +o; c+eate,(
S#L9 NSERT NTO $RODUCT VALUES(76@>AR_OL@67'65'6&'/);
& +o; c+eate,(
S#L9 SELECT * FROM $RODUCT;
$_D TTLE #T: $RCE S_D
********** *************** ********** ********** **********
& SOA$ )'' &' &'&
) S>AM$OO &' 7' &'&
/ TOOT>$ASTE /7 ?7 &')
0 <RUS> 4' &) &')
7 >AR_OL 7' 5' &'/
S#L9 SELECT AVG($RCE) FROM $RODUCT;
AVG($RCE)
**********
0/(0
S#L9 SELECT COUNT(*) FROM $RODUCT;
COUNT(*)
**********
7
S#L9 SELECT COUNT(DSTNCT S_D) FROM $RODUCT;
COUNT(DSTNCTS_D)
*******************
/
S#L9 SELECT MA!(#T:) FROM $RODUCT;
MA!(#T:)
**********
)''
S#L9 SELECT MN($RCE) FROM $RODUCT;
MN($RCE)
**********
&'
S#L9 SELECT SUM(#T:) FROM $RODUCT;
SUM(#T:)
**********
/57
EXPERIMENT NO: 04
AIM: TO USE THE CONCEPT OF FOREIGN KEY:
A &!R+'G" ,+- in one ta$le .oints to a /R')AR- ,+- in another ta$le. #he &!R+'G"
,+- constraint is used to .revent actions that %ould destro0 lin1s $et%een ta$les.
#he &!R+'G" ,+- constraint also .revents invalid data from $eing inserted into the
foreign 1e0 column, $ecause it has to $e one of the values contained in the ta$le it
.oints to.
&!R+'G" ,+- constraint using R+A#+ statement"
CREATE TA<LE O+,e+%
(
O_, -nt NOT NULL $RMAR: =E:6
O+,e+No -nt NOT NULL6
$_, -nt FOREGN =E: REFERENCES $e+%on%($_,)
);
&!R+'G" ,+- constraint using AL#+R statement:
ALTER TA<LE O+,e+%
ADD FOREGN =E: ($_,)
REFERENCES $e+%on%($_,);
#o allo% naming of a &!R+'G" ,+- constraint, and for de(ning a &!R+'G" ,+-
constraint on multi.le columns, use the follo%ing SQL s0nta2:
ALTER TA<LE O+,e+%
ADD CONSTRANT AB_$e+O+,e+%
FOREGN =E: ($_,)
REFERENCES $e+%on%($_,);
#o dro. a &!R+'G" ,+- constraint, use the follo%ing SQL:
ALTER TA<LE O+,e+%
DRO$ CONSTRANT AB_$e+O+,e+%;
IMPLEMENTATION:
S#L9 CREATE TA<LE $ROCECT
) (
/ $_D NT NOT NULL $RMAR: =E:6
0 $_TTLE VARC>AR(&') NOT NULL6
7 ALLOTED_ON DATE NOT NULL6
? DEADLNE DATE NOT NULL6
D GUDE VARC>AR)()') NOT NULL
5 );
Table c+eate,(
S#L9 NSERT NTO $ROCECT VALUES
) (&''&6@TEST@6@'7*SE$*)'&/@6@7*DEC*)'&/@6@MR( RA>UL@);
& +o; c+eate,(
S#L9 NSERT NTO $ROCECT VALUES
) (&'')6@TUTORAL@6@'D*SE$*)'&/@6@&7*DEC*)'&/@6@MR( RAC@);
& +o; c+eate,(
S#L9 NSERT NTO $ROCECT VALUES
) (&''06@>TML@6@'D*SE$*)'&/@6@&'*DEC*)'&/@6@MS( EO:A@);
& +o; c+eate,(
S#L9 SELECT * FROM $ROCECT;
$_D $_TTLE ALLOTED_ON DEADLNE GUDE
********** ********** ********** ********* ********************
&''& TEST '7*SE$*&/ '7*DEC*&/ MR( RA>UL
&'') TUTORAL 'D*SE$*&/ &7*DEC*&/ MR( RAC
&''0 >TML 'D*SE$*&/ &'*DEC*&/ MS( EO:A
S#L9 CREATE TA<LE GUDE
) ( GD NT $RMAR: =E:6
/ NAME VARC>AR()')6
0 ROOM NT 6
7 $_D NT 6CONSTRANT F= FOREGN =E:($_D) REFERENCES
$ROCECT($_D) );
Table c+eate,(
S#L9 NSERT NTO GUDE VALUES()'&6@MR(RAC@6&)&6&''));
& +o; c+eate,(
S#L9 NSERT NTO GUDE VALUES()')6@MR( RA>UL@6&)/6&''&);
& +o; c+eate,(
S#L9 NSERT NTO GUDE VALUES(0'06@MS( EO:A@6&)76&''0);
& +o; c+eate,(
S#L9 SELECT * FROM GUDE;
GD NAME ROOM $_D
********** ******************** ********** **********
)'& MR(RAC /// &'')
)') MR(RA>UL /0/ &''&
0'0 MS(EO:A /0D &''0

You might also like