Experiment 5
Experiment 5
im:To implement aggregate functions with order by,group by, like and having clause.
A
___________________________________________________________________________
_ __________________________________________________________________________
Theory:
The ORDER BY clause is used to sortthedatainascendingordescendingorder,basedon
one or more columns.
SELECT column-list
FROM table_name
[WHERE condition]
[ORDER BY column1, column2, .. columnN] [ASC | DESC];
he GROUP BY clause is used in collaboration with the SELECT statement to group
T
togetherthoserowsinatablethathaveidenticaldata.Thisisdonetoeliminateredundancy
in the output and/or compute aggregates that apply to these groups.
The GROUP BY clause follows the WHERE clause in a SELECTstatementandprecedes
the ORDER BY clause.
SELECT column
-
list
FROM table_name
WHERE
[
conditions
]
GROUP BY column1
,
column2
....
columnN
ORDER BY column1
,
column2
....
columnN
he LIKE operator is used to match text values against a pattern using wildcards. If the
T
search expression can be matched to the pattern expression, the LIKE operator willreturn
true, which is 1. There are two wildcards used in conjunction with the LIKE operator:
● The percent sign (%)
● The underscore (_)
The percent sign represents zero, one, or multiple numbers or characters. The underscore
represents a single number or character. These symbols can be used in combinations.
If either of these twosignsisnotusedinconjunctionwiththeLIKEclause,thentheLIKE
acts like the equals operator.
SELECT FROM table_name
WHERE column LIKE
'XXXX%'
(Autonomous College Affiliated to University of Mumbai)
KJSCE/IT/SYBTECH/SEMIII/DMS/2024-25
or
SELECT FROM table_name
WHERE column LIKE
'%XXXX%'
or
SELECT FROM table_name
WHERE column LIKE
'XXXX_'
or
SELECT FROM table_name
WHERE column LIKE
'_XXXX'
or
SELECT FROM table_name
WHERE column LIKE
'_XXXX_'
ere are examples showing WHERE part having different LIKE clause with '%' and '_'
H
operators:
Statement Description
he HAVINGclauseallowsustopickoutparticularrowswherethefunction'sresultmeets
T
some condition.
TheWHEREclauseplacesconditionsontheselectedcolumns,whereastheHAVINGclause
places conditions on groups created by the GROUP BY clause.
SELECT column1
,
column2
FROM table1
,
table2
WHERE
[
conditions
]
GROUP BY column1
,
column2
HAVING
[
conditions
]
ORDER BY column1
,
column2
__________________________________________________________________________
Example:
1.
SELECT
*
FROM COMPANY ORDER BY NAME
,
SALARY ASC
;
2.
SELECT NAME
,
SUM
(
SALARY
)
FROM COMPANY GROUP BY NAME
;
3.
SELECT
*
FROM COMPANY WHERE AGE
::
text LIKE
'2%'
;
4.
SELECT
*
FROM COMPANY WHERE ADDRESS LIKE
'%-%'
;
5.
SELECT NAME FROM COMPANY GROUP BY name HAVING count
(
name
)
>
1
;
Outcomes:
Q1 Can you apply like operator on integer value? explain with example how?
2Whyaggregatefunctionsaremoreusedwithorderby,groupbyandhavingclauses?
Q
Can we change order of these clauses when used in single query
Answers :
Ans 1.
hèLIKE`operatorisgenerallyusedwithstringdatatypestosearchforaspecifiedpattern
T
within a column. It is not typically used with integervaluesbecausèLIKE`isdesignedto
handletext-basedpatternmatching.However,youcanusèLIKE`withintegervaluesifthe
integer column is implicitly or explicitly cast to a string data type within the query. For
instance, if you have a column oftypèINT`andyouwanttosearchfornumbersthatstart
with a certain digit, you would first convert theintegercolumntoastringusingafunction
like `CAST` or `CONVERT` and then apply the `LIKE` operator.
Ans 2.
ggregate functions, such as ̀COUNT`, ̀SUM`, ̀AVG`, ̀MIN`, and ̀MAX`, are used to
A
perform calculations on a set of values andreturnasingleresult.Thesefunctionsareoften
employed in conjunction with the ̀ORDER BY`,̀GROUPBY`,and̀HAVING`clausesto
summarizeandorganizedatameaningfully.ThèGROUPBY`clausegroupsrowsthathave
thesamevaluesintosummaryrows,suchascalculatingthetotalsalesperproductcategory.
The ̀HAVING` clause is used to filter groups based on a condition applied to aggregate
functions, like finding categories with total sales above a certain threshold. Finally, the
̀ORDERBY`clausearrangestheresultsetinaspecificorder,suchassortingcategoriesby
their total sales in descending order.
he order of these clauses in a SQL query is fixed and cannot be changed: ̀GROUPBY`
T
comes before ̀HAVING`, which comes before ̀ORDER BY`. The ̀GROUPBY`clauseis
used to aggregate data into groups, ̀HAVING` filters these groups based on aggregate
conditions, and ̀ORDER BY` sorts the final result set. This sequence is crucial for SQL's
logical processing of queries, ensuring that data is grouped and filtered correctly before
sorting.Forexample,inaquerythatcalculatesthetotalsalespercategory,filterscategories
withsalesabove1000,andsortsthembytotalsales,youmustusèGROUPBY`toaggregate
data, ̀HAVING` to filter aggregated results, and ̀ORDER BY` to sort the final output.
Changing the order of these clauses would result in a syntactical error.
__________________________________________________________________________________
Conclusion:
Grade: AA / AB / BB / BC / CC / CD /DD
__________________________________________________________________________________
References:
Books:
1. E lmasri and Navathe, “Fundamentals of Database Systems”, 6th Edition, Pearson
Education
2. Korth, Slberchatz,Sudarshan, :”Database System Concepts”, 6th Edition, McGraw –
Hill.