DBMS –26
(Database Management System )
In continuation of Part – 25
Select
It is very important query command of SQL which
is used to retrieve the rows/tuples from one or more
tables. i.e. user can get back the data from database
using this command.
Many different clauses can be specified with it to get
data in different ways from database.
Syntax :
>Select [*/ columns/expression] from <Table name>
[where <condition> ]
[group by <col>[having<condition>]]
[order by <col> [ Asc / Desc ]] ;
[group by <col>[having<condition>]]: Group by clause
is used to create sub-groups of rows within the table which
contain same value for the given column on which group is
created. We can apply group or aggregation functions (eg.
Count , max, avg etc.) on these sub-groups of rows. It helps
in analysis of data.
[having<condition>] : It is used to give a condition for
selecting rows which will become part of different sub-
groups. It can be written with group by clause only, but it is
optional. Also condition should be defined on the same
column which was used with group by, for creating
groups. ( Note : Group should be created on column which contains duplicate values.)
Examples for group by:
> Select avg(price) from book group by type ;
> Select estn, max(fare) from train where sstn=‘pnp’ or sstn=
‘snp’ group by estn having estn in ( ‘del’, ‘krl’, ‘amb’ ) ;
> Select avg(qty), min(price) from inventory where price between
10000 and 20000 group by itnm ;
> Select rno, max(tot) from stud where class = 12
group by rno having rno between 30 and 100 ;
> Select dept, stddev(sal), count(sal) from emp
where sal > 25000
group by dept having dept in ( ‘Sales’, ‘Marketing’, ‘Account’ ) ;
[order by <col> [ Asc / Desc ]]: This clause is used to see
the result of select command in ascending or descending
order of values of the given column. By default, the order
will be ascending ( ASC). For descending order user must
write the option DESC or Descending. eg.
>Select rno, nm from first order by nm ; -- Ascending
>Select bname, author from book where bno > 100
order by author desc ; -- Descending
>Select type, max(price),min(qty) from inventory
group by type having type in (‘Cpu’,’KB’)
order by type ;
Multiple Columns for sorting can be used :
>Select rno, nm, dob from first order by dob, rno ;
Wherever dob is same the rows will be sorted on rno.
Home Work
Write and revise the topic
S.M. Hindu Sr. Sec. School ( Since 1914 )
Sonepat 131001 – (Hr.)
Ph. 0130-2222014, 2222608