Chapter 14 More On SQL
Chapter 14 More On SQL
SQL
ii
l4 .1 Introduction
14.2 Ordering R ec or
aa
14 .3 Aggregate Fu nc
14.4 Type s of SQ L
14.5 Grouping Re su
ds in R es ult- Ord er
tio ns
Fu nc tio ns
lt - G R O U P BY
by Cl ause
dh
14.l INTRODUCTION
You have w or ke d w
it h ba si c SQ L co m
This ch ap te r w il l ta m an ds in yo ur pr ev
lk ab ou t so m e SQ L io us class an d us ed
to di sp la y gr ou ps co m m an ds in de ta ils da ta ba se M yS Q l
of re co rd s, su m m . After this ch ap te r, yo
records. ar y of gr ou ps of re u shall be ablt
co rd s an d display se
lected gr ou ps o.
le t us be gi n w ith ou
pa
r di sc us si on on ho w
yo u ca n or de r record
s of a table using ORD
14 2 ER BY clause.
· ORDER ING RECOR
DS IN RESULT - O
RD ER BY CLAUSE
~l th ou gh yo u ha ve
re ad ab ou t O R D E
c ass, he re w e ar e co R BY cl au se of SQL
ve ri ng it ag ai n in SELECT st at em en t in
a de yo ur pr ev io us
14.2 l ta il ed m an ne r.
. Recalling SQL SE
Th LECT ORDER BY C
u e s
la us e
re ul t se
.i. 1owev . t ge ne ra te d by th e SQ L SELECT st t · ot
SE at em en is n or de re d in any form by
er, if yo u w an t to
so rt or or de r th e re lt t yo u can us e th ORDER BY clause deof
fault.
L E ct st at em en t as su se ' e SQL
pe r fo ll ow in g fo rm
SELECT <comma se at :
pa ra te d se le ct
[WHERE li s t> FROM <t ab le
o · . >
<c on d1 .t1 .o n> ]
RDER BY <f ie ld na
m e> [A SC IDESC sc lD ES C] , ... ] ;
] [, <f ie ld na m e>
[A
458 COMPUTER SCIENCE WITH PYT
H01 ~
1.,
Keywords ASC and DESC denote the order - ASC stands for ascending and the DEsc
for descending. If you do not specify any order keyword ASC or DESC, then by defa st'111 clc;
ORDER BY clause sorts the result set in ascending order. u t, th~
For example, consider the table Data having records as shown below :
+-- --- -- - -+- -- - - -- -- -+-- -- - ----- + -- --- - -+ - - --- - -- -- - +
I rollno I name I marks I grade I section
+-- ---- -- - + - --- - ---- - +- -- - -- - -- -+-------+---- ------ -+
101 Ruhani 76.80 I A I A
ii
106 Arsiya 91.60 A+ I B
aa
111
112
113
115
11 7
118
Simran
Adam
Gurnoor
Rabiya
Rahil
Neha
66.00
74.20
93.50
72.50
32 . 00
59.50
I F
I C
I B
I B
I A+
I B
I
I B
I
I C
I A
I A
C
ORDER BY <fieldnamel> [A
SC/DESCJ [ .
for example, the following t ., <fleldname1> [ASC/
s atem DESCJ
and then on the basis of de . ent Will sort ., ... 1 ;
ii
scend1ng ord the records f
mysql> SELECT* FROM d er of column mark1rstly on the column
ata - - - - - - - s. name Section
st
->ORDER By section
+---------+----------+---
1 r oll no I name
+- --------+--- -------+---
· ...,
AS(
I mark
aa
s
. - - - - and
----+-
Fir sort field i
for all
------+------
I gra d e I se c . -----+
.
s section in ascend ·
, marks DES( . with descen;.e records of same sec:? orter (as section ASC)
, ing order (marks DES~;, t e sort field is marks
14.2.3
Ordering Data on the basis of an Expression
Soineti
1
result ;es! you need to display the result of a calculation or a mathematical expression in the
calculated. n SUch cases, you may want or need to arrange your result set in the order of the
express· express1on. · The ORDER BY clause allows you to include • the mathematical
lt\athe ion. t0 order the result set by it. However, to arrange a result set on the basis of a
the 11\a~:~al _expression, you should preferably (though not a necessity but preferably) include
atical expression in the select list so that it becomes easy to comprehend the result.
COi\\P UTER SC IENCE WITH PYih Q
4b 0 O
_
ii
1 106 I Arsiy a A+ I B
10 3 I B 28.4 200
1
simra n I A 25 . 3750
I 11 5 Rabiy a I B 1
I B I c I 25 . 9700
~-~~ ~ ----- ~- ~~~~ - ---- ~-~ ---- ---- +---
aa
---- --+- ---- - - ----+
. et ( 0 02 sec)
s rows in s ·
. . h
If You want th math emat ical expr ess10 n mt e select
. a co1
you can provide alias name to e b .
mnn h
' wing state ment w1·11 a Iso produce t e sam
list, e.g., follo e resul t as ·11
abov e ut it w1 name the
colum n rnarks*.35 as Terr nl: . arks * as Term l FROMdat a
mysql> SELEC T r ollno , name , grad e, secti
••••••on , . 35
••••••m•••••• • ••• •• •••• ••
-> WHERE marks > 70 "Y ..
- > ORDER BY secti on ASC , Term l DESC ;
dh
•••• • • • •. •. • •• • • • • • • • • • • • •••
- +- - - - - - - - - - +- - - - - - - - - - +- - - - - - - - - +
+ •••••
+I r o ll no I name I gra d e I sect ion I Terrn l I ••:
-+-- ----- ---+ - - ---- - ---+ ---- ---- -+-
+--- - ---- . A I A
- --- -- - - --+ • •••••
101 Ruhan 1. 26.8 800 I
1 02 Geor ge I B I A 24.9 200
108 Me era A+ I B 34.0 200
11 3 Gurn oor A+ I B 32.7 250
106 Ar siy a I A+ I B 32.0 600
pa
i
Meera I A+ I B
108 Ev a lu at ed
Amaal 57.20 I C I B
10 9 Pen d i ng
i
Simran 66.00 B I A
11 1 Pe nding
Adam 74.20 B I C
115
I 117
I 11 8
15 ro ws
112
113
Neha
aa
Gurnoor
Rabiya
Rahil
93.50
72.50
32 ' OO
59.50
I
C
A+
B
F
I A
I B
I B
I C
Pe nd ing
Assi g ne d
Ass i gn ed
S u bm i tted
Ev aluate d I
+-- -- - - - - - + - - - - - - - - - - + - - - - - - - - - - + - - - - - - - + - - - - - - - - - + - - - - - - - - - - - +
in set (0. 00 sec)
Now to order the above table as per the mentioned order above i.e., as per Project field having
dh
values in this order : 'Evaluate d', 'Pending', 'Submitted', 'Assigned', you can write the SELECT
statement's ORDER BY clause with FIELD() as shown below:
~ The sort field
mysql> select * from data ~
-> ORDER BY FIELD(Pro ject, 'Evaluate d', 'Pending' , ' Submitted ', 'Assigne d' ) ;
'
The desired sort order;
+---- - - - - - + - - - - - - - - - - + - - - - - - - - - - + - - - - - - - + - - - - - - - - - + - - - - - - - - - - - + Notice, string values
I grade I secti o n I pr o ject
I are given in quotes
pa
'
Simran Pending
109 C B
Amaal 57.20 su bm itted Result set order as per
102 B A the custom order as
Ge o rge 71.20 sub mit t ed
107 B specified by you.
Raunaq 32 .50 F su bm i t t e d
106 B
Arsiya 91.60 A+ s u b mi tted
117 C
Rahil 3 2 .00 F As s igned
113 B
Gur n o o r 93.50 A+ As s i gned
115 B
Rabi y a 7 2 .50 B Assig n ed
104 C
I
>, Ali B --+----- ------ +
I 61. 2 0
15 r ows -~-+---- ------+- --------- +------- + ----
1n se t
( 0. 0 4 s e c )
462 COMPUTER SCIENCE WITH PYTHON
- Xii
ii
The usage of these options will become clear with the coverage of example s in this section.
All the examples that we'll be using here, shall be based upon followin g table empl.
I EMPNO I
+-
aa
T able 14.1 Database table empt
+-----+ -------- --+--- - ------+ ----- +- -------- --+---- ----+-- ----- - +------+
ENAME JOB I MGR I HIRE DATE SAL
--+---- ------+ -------- --+---- -+----- ------+ -------- +------ --+---- --+
I 8 3 6 9 I SMITH
I 8 4 9 9 I ANYA
I 8 521 I SE T H
I CL E RK
I SALESMAN
I 8 9 o2
I 8698
1990-12 -18 I 800.00
1991-02 -20 I 16 00 . 00
I NULL
COMM
I 300.00
I DEPTNO I
20
30
dh
I SALESMAN I 8698 19 91 -02- 22 I 1250.00
I 8 5 6 6 I MA HADEVAN I 500.00 30
I MANAGER I 8839 19 91 -04-0 2 I 2985 .00
I 8 6 5 4 I MOMIN I NULL 20
I SAL ESMAN I 8698 1991-09 -28 11 250 .0 0
I 8 698 I BINA I MANAGER I 1400.00 30
I 8839 199 1- 05 - 01 I 2850.00
I 8 8 3 9 I AMIR I PRESIDEN T I NU LL
I NULL 30
1991 -11-18 I 5000.00
I 8 8 4 4 I KU LDEEP I SALESMAN I NULL 10
I 8 698 1991-09- 08 1150 0.00
I 8 8 8 2 I S HI AVNSH I MANAGER I 0. 0 0 30
I 8839 1991-06 -09 I 2450.00
I 8 8 8 6 I ANOOP I CL ER K I NULL 10
I 8888 1993-01- 12 I 1100 .00
I 8 8 8 8 I SCOTT I ANALYST I NULL 20
I 8566 1992-12 -09 I 3000
I 8 90 0 I JAT I N I CLERK .00 I NULL
I 8 6 98 20
pa
1991-12 -03 I
I 8 9 o2 I FAKIR I ANALYST 950.00 I NULL
I 8566 1991-12 -03 30
I 8 9 3 4 I MITA I CLERK I 3000 .00 I NULL
I 8882 1 992-0 1-2 3 20
+-----+ -------- --+----- ----- +----- 11300.0 0 I NULL
10
-------- ---+--- ---- - +------ --+---- --+
l . AVG
This function computes the average of given data.
S3/nta;<
AVG( [DISTINC T I ALL] n)
J.. Returns average value of parameter (s) n.
Argu~enI !_yp~ : Numeric 13-~~u!n !lE_{~ : Numeric
+-- -- -- -- -- -r-
1 ~0t al
ii
mysq l > SELECT COUNT( *) ''To tal "
l-l
FROM empl ; +-- --- --- -- +
f\.\t--lPl E
Sol11 til>11.
mysql> SELECT COUNT(job) "Jo b Cou
nt"
aa
l -U . Ctm 11t 111111,bcr c>fjobs in tn[,/ c
cmpl.
l 1' 0\\" i n set
This fu t·
nc ion returns the ma xim um val ue f · en column or expression.
rom a giv
~
MAX( [DISTINCT I ALL] exp r)
" Rett1r
ns n1aximum val ue of arg um ent expr.
Aro..
o•111Ie11t t, '.
YPt · Nu me ric Ret11r11 vnl11e : Nu meric
~
,Xi\~ +---- --- --- -- -- --+
So/ 11 •PLE 14 •S• D1sp
· lay max
fio11 •
imu m salary j.rom tabl e empl. I Max imu m Sal ar y I
+-- --- --- -- -- ---- +
500 0 . 00 I
SELECT MAX(sal) "Maximum Sal ary" +-- - -- --- --- --- - - +
·n s et (0. 0 1 s ec )
FROM em pl; .l r ow 1
4. MIN . . alue from a given column or e xpression .
This function returns the m1mrnurn v
1 990 - 1 2-l a
Solution~ysql> SELECT MIN(hi redate ) "Minimum Hire Date" +---- - --- --- -- --
1 row i n s e t ( O ( ·
FROM empl; • I6 ,
ii
5. SUM . ·
. given
This function returns the sum of va1ues m colum n or express10n.
J,..
SUM( [DISTIN CT
EXAMPLE 14.7.
I ALL]
Returns sum of values of n.
aa n)
.
aggregate func tions bec ausethe1
1. To calculate the total gross for employ ees of grade 'E2', operate on agg regates 0 f tuples
the comma nd is : The result of an aggrega ~
SELECT SUM(gross) FROM employee function is a single va lue.
WHERE grade = 'E2' ;
.
2. To display the average gross of employ ees with grades 'El' or 'E2', the comman d us ed is:
SELECT AVG(gr oss) FROM employ ee
WHERE (grade = 'El' OR grade= 'E2') ;
3. To count the numbe r of employ ees in employee table, the SQL comma nd is :
SELECT COUNT(*)
FROM employ ee ;
4. To count the numbe r of cities, the differe nt me b b the follo 1' ir~
comma nd: m ers e1ong to, you use
SELECT COUNT(DISTINCT city) FROM members .
,
c!Jll/"
ii
Now that you have learnt different types of fu t·
SQL supports many and many functions All thnc ions,
f
let us talk abot1t th . b d
. eu roa categories.
following two types : · ese unctions can be generally categorized into
aa ¢)
¢)
Single Row (or Scalar) functions.
Multiple Row (or Group or Aggregate) functions.
(i) Single Row functions work with a single row at a time. A single row function rehirns a
result for every row of a queried table.
(ii) Multiple Row or Group functions work with data of multiple rows at a time and return
aggregated value.
dh
Examples of multiple row functions are the group functions that you have learnt in
previous section i.e., sum( ), count( ), max( ), min( ), Avg( ) etc.
The difference between these two types of functions is in the number of rows they act upon. A
single row function works with the data of a single row at a time and returns a single result for
each row queried upon; a multiple row function works with a group of rows and returns a
single result for that group.
pa
145
· GROUPIN G RESULT - GROUP BY
The GROUP BY c1ause comb'mes a II those records that have identical values in a particular field
.
or a group of fields This rou in results into one summary record per group if group-
functi 0 · g P g d h GROUP BY clause is used in SELECT statements
to d1.v·dns are used with it. In other wor . s, t e b ame or with ao-o-reo-ate
1 e
the table into groups. Groupmg can be done y a co1umn 11 , oo o
functi .
ons m which case the aggregate pro d uces av alue for each group.
For ·
example, to calculate the number of employees in +---------- - -+ --- - - -- --- +
ea.ch
grade, you use the command I job I COUNT ( *) I
+--------- - -- +------ --- - ~
:ELECT job, COUNT( * )
I ANALYST 2
ROM empl
CLE RK 4
GRoup BY J' ob . 3
CR_ , MANAGER
. OlJp BY .
applies the aggregate f t mctions I PRES I DENT I 1
ind . 4
epende tl h •
by h . n Y to a series of groups t at are defined I SALESMAN
+----- --- -- -- +--- ------ - +
aving a f'1e1d value in common.
. h f ll
Now cons ider t e o ow ing quer y ' whic h is also grou ping reco rd s base d on deptn o_
mysq l> SELE CTde ptno , COUNT( * ), SUM (sal )
FROM empl
GROU P BY dept no ;
+-- ---- -- ---- +--- ---- --- +--- -- -- -
+
dept no I CO UN T (.,, l SUM (sal) I
I
+--- ---- ---- - +--- ---- --- +- -- - - - ---- - -
+
10 3 8750 . 00 I
20 5 1 088 5 . 00 I
I 30 I 6
9 4 00 . 00
+--- ---- -- ---+ - -- ---- --- +--- ---- ---- -
+
As you can mak e out that the abov e quer y is d ispla
ying coun t of reco rds and sum of salari es in
ii
each grou p and the grou ps are form ed on the basi
s of deptno. Thu s, from the abov e output, yoJ
can mak e out that in depa rtme nt num ber 10, ther
e are 3 emp loye es (reco rd s) and total of al:
salaries is 8750.00 ; in depa rtme nt num ber 20, ther
e are 5 emp loye es and total of salaries i~
aa
10885.00; and so on.
3
5
And the resu lt prod uced by this quer y is
6
+--- ---- ---- -- +
But can you mak e out, these are emp loye e-co
unts for
infor mati on, you may mod ify the SELECT list as:
whic h depa rtme nts ? To get this
ii
+ - - - - - - - - - + - - - - - - - - - - - - - - +- - - - - - + See, first record of group with dept no IO has value .'V l., 1./, in mgr
I dept no I c O un t ( e rn P no ) I rn g r I field : first record of group with dept no 20 has R902 in mgr field :
+ ___ _ _ - - - - + - - - - - - - - - - - - _ _ + _ _ _ __ -~ a_nd first record of group with cleptno 30 has va lue 8698 in mgr
1 10 I 3 fi eld. Hence this output.
aa 20
30
I 5
6
I
14.5 2 . G
G Clause HAVIN
· Placing Conditions on roups - . t t to WHERE clause that places
'1'1..
tne HAVING clause places con ditions on groups m con ras f .
c .. d' . cannot include aggre gate unct10ns,
ondihons on indivi du al rows. Wh 1·1e WHERE con it10ns
liAVING condit ions can do so.
If
U lM l) LJ I l R 'i( IL NC ~- WI I I I ,
I Yi I Ir JI
l•'o r l'X'-trnpk, to calcu blL' lhc .:ivera gl' gross and
l
c m p Ioycc s t.oc Iong1. ng to '1,4'
j gra d<.', tlw con- -., JOl,1 1~,i•rl,.,\f,
101 u
ll Woui
·1. Can you arrange the result set of an sr: 1 ECT AVG( gr'n ss ) , SUM( cro ss )
db1,
SOL query on multiple columns ? rnoMemplo yee
2_ What is the significa nce of "ORDER 13Y" GROUP BY gr ade
- 'l'liis rn11diticn 1 w"
in the given query? .
r11111I1c11h/e 011 ,,,. ll 1r/ h•
HAVI NG grad e = 'E4' ,
. . r, UlfJ >UIJ,(
SE LECT emp_ i d , f namc, l nJmr u 11 llltli JJi</1w/ ro 111.1 '''"
FROM empl
ii
descending order JOn I COU iJ'J', .
1
(b) Data of table person on the basis of GROUP BY job I -- - - - - - - - - -1---
I /\NAJ. Y ST'
column emp_id will be sorted in HAVI NG coun t(*) < 3 ;
I PH l::S [ Dl~N•r
I
ascending order
aa
(c) Only data of column emp_id will be
sorted in descending order
(d) Only data of column emp_id will be
sorted in ascending order
3. What will be the order of sorting in the
given query?
SELECT emp_i d , emp_ name
FROM perso n
Thi s will prod uce the
fo11o wing outp ut : 2
1--- - - - ----- +--- -- - - -
r o ws
expr essio n (i.e., an expr essio n or cond ition that results into
true or false) or use aggre gate function in the having condition.
You can inclu de more than one condition in HAVIN
G
i
se t ( D. 1I :,c
dh
clause, of cours e, by using logic al operators. Consider this:
ORDER BY emp_ id , emp_name ;
SELECT Dept no, AVG(Comm), AVG( Sal)
(a) Firstly on emp_id and then on
emp_name FROM empl
(b) Firstly on emp_name and then on GROUP BY Dept no
emp_id HAVING AVG(Comm) > 750 AND
(c) Firstly on emp_id but not on AVG( Sal) > 2000 ;
emp_name
Also, you can use an aggr egate fu nct10· nm
· the HA
, YINGhe
(d) None of the mentioned
pa
(d) All of these HAVING SUM (sal) BETWEEN 3000 AND 7000 ;
: MORE ON SQ L
14
r ~Jpfef
~
SMITH'S 10885.00 and AN YA 'S 940 0.00 ary- sum . 8750 I SM I T H I l G O 8 '.J • [, ,.
? is .00, I ANYA 9400 . u'
Thus we reco m men d not to u se non -gro + - -- - - - - - + -
ii
' up . .
necessary. expr essi ons m GROUP BY
query unless otherwi se
LET US REVISE
<&
-:i
l. Th SQL
e SELECT prov ides clauses for sortr.ng data andfior summari zing res ults. Writ e the 11aJ11 cs of cln 11 scs
for these.
S
olution. The ORD ER BY clau se of SQL SEL t 11 ws to sort the data of resu lt se t.
Th ECT statemen a o .
t0 create sum marized results of g10 . "'
e GROUP BY clau se of SQL SELECT state u ped
men t allow s
data from table.
2. y
ou Al
Want to group the result set based on some co l unm 's value. so, yo u wnnt thnt tile gro 11 pcrf rcs11. lt s/1011./d
appear in a sorted order. In which order will you (fo r sorting n11d fo r S; /'O ll f!/ 11 ~). G, t,c
write the two c1n11ses ,
exain p/e tO ,
support your answer. .
Solut· d ) d ORD ER BY clau se (for sort mg
ion. Whe n we use GRO UP BY clau se (for ino- of ata an
data) grou p o . .
w1·n toge ther, the ORD ER BY clau se alw ays fo II 0 ws othe1 c1auses· That is, the GROUP BY cbu se
come before ORD ER BY clau se.
p 470
COMPUTER SCIENCE WITH
PYTH ON
For exa mp le, . ,.
SELECT use rid , SU M( sco re) AS
tot al_ sco re
FROM use r _sc ore
GROUP BY use rid
3.
'P .
ORDER BY use r id ASC ,·
, . . . er : 'Govt-sector-exp . ,
Publzc-sector-experze . , ,
nce', 'Przvate-sector-experze nce , Fresher ,. enence· 1
Write an SQL que ry to achieve this . ··
ii
Sol utio n.
SELECT * FROM Apply
ORDER BY FIELD (Ex per ien ce, 'Go
4.
aa
What are different types of SQL functio
Sol utio n. (i) Sin gle Ro w (or Sca
(ii)
vt. -se cto r-e xpe rie nce ', 'Pu blic
'Pr iva te- sec tor -ex per ien ce' ,
ns ?
lar) fun ctio ns.
(ii) Mu ltip le Row (or Gro up or Ag
wit h a
for eve ry row of a que ried table.
Multiple Row or Group Functions
'Fr esh er' );
Sol utio n.
SELECT statement ?
The diff ere nce bet we en WH ER
E and HA VIN G cla use is tha
app lica ble on ind ivid ual row s wh t WH ER E conditions are
ere as HA YIN G con diti ons are app
GR OU P BY clau se. lica ble on gro ups as formed by
Sol uti on . l SELECT SUM (sa l), AVG (sa l), MAX
mysq > (sa l), MIN (sa l)
FROM empl .
GROUP BY dep t no, Job ;
, i
4 : MORE ON SQ L
,,. ;:)·i
ii
1-• as 1e ollowzng columns ·
Code) Name ) Salary J Deptcode [CBSE D 2014]
·
He wants to display maximum salary department- · H
aa
wise. e wrote the following command :
SELECT Deptcode J Max(Sala ry) FROM Employee ;
15. W . GROUP BY ord_date ; . lfind the highest rating in each city. Put the output in this Jann:
rzte a query on the customers table that wzl .
For the city (city), the highest rating is : (rating). . h t rating is : J J MAX ( rating)
S1 ·t 'the h1g es
o Ution. SELECT 'For the ci tyJ J Cl y J
FROM customer s
GROUP BY city ;
CO MPUTER SCIENCE WI
TH PYTH0 1
GLOSSARY ~ - I i!
::.:::::i
Single Ro w function
A fun cti on tha t wo rks
Multiple Ro w function on a value in si ngl e row
.
A fun cti on tha t works
Group function on valu es of a group
of rows.
Function tha t works on
Aggrega te function a gro up of row s. A mu
ltiple row function .
Functio n tha t works on
ag gre ga te of row s. A
multiple row fun ctio n.
ii
DER BY clause?
2. W ha t is the d efa ult
so rt or de r of ORDER BY
3. W hic h fu nc tio n do clause?
yo u us e in ORDER BY
clauses to specify cu sto
m so rt ord er?
aa
4. W rit e an ex am ple qu
5. W rit e a qu ery tha t
ing
on the basis of Pr oje ct- Gr
ord er)
d WHERE clause?
.
ii
,,ri.,ch SQL statement lets you list all stores whose t t .
- J
3. vviu O a1 sa1es amount
1s over 5000 ?
(a) SELECT STORE_I D, SUM(SALES_AMOUNT) FROM SALES GROUP BY ST .
SUM(SALES_AMOUNT) > 5000; ORE_ID HAVING
aa
(b) SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES
GROUP BY STORE_ID HAVING SALES_AMOUNT > 5000;
(c) SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES
WHERE SUM(SALES_AMOUNT) > 5000 GROUP BY STORE_ID;
(d) SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES
WHERE SALES_AMOUNT > 5000 GROUP BY STORE_ID;
dh
4. Which SQL statement lets you find the total number of stores in the SALES table?
(a) SELECT COUNT(STORE_ID) FROM SALES;
ii
(d) SELECT * FROM EXAM_RESULTS WHERE LNAM
E <> 'L';
9. Wha t is the resul t of the follo wing SQL
state men t?
(a) 90 (b) 85
COACH-ID COACHNAME
L TS GROUP BY EXAM_ID HAVING EXAM_ ID
(c) 100
AGE
(d) 95
Table : CLU B
SPOR TS
= 1;
ii
10 M oh an Nomn di
300.00
C A
Give the ou tp ut e cal 92.0 12A
of fo ll ow in SQ
(i) SELECT MIN
.. (A L
g ark) F
}M
st at aaommerce
ements :
67.S 12A
12C
(u ) SELECT SU
M(Stipe d) F R O M ~
n ROM St...,ud
TUDENT WHER
(iii) SELECT AV E AvgM
G(Stipend) FRO ark < 75 .
.1v ) SELE M ent WHERE Gra
de= 'B' .
( CT COUNT(DIS
TINC Student WHER ,
12. In a Databas e, T) FROM Stud E Class = '12A'' .
th ere are two tabl
es gi.ve ,
n below : ent ;
dh
T able · EMPLOY
EE
EMPLOYEE ID
NAME - Tab le : JOB
SALES JOBID
El SUMIT S IN H A - JOBID
JOBTITLE
1100000 102 SALARY
E2 VlJAY S IN G H 101 President
T OM A R 1300000 200000
101 102
E3 AJAY RAJPAL Vice President
1400000 125000
103 103 Administration A
pa
ii
19. List the total of custo mers ' order s grou ped by custo
mer (id).
20. List all custo mers (nam e) who have orde rs (use EXIS
TS).
21. List the sum of the totals of order s grou ped by
custo mer and state .
aa
22. List the sum of the totals of order s wher e this sum
is great er than $1000 grou ped by custo mer (id
state and order ed by state.
) an
23. List the custo mers (nam e) and their orde rs' detai ls.
24. List the custo mers (nam e) and the total amou nt of
all their orde rs.