0% found this document useful (0 votes)
5 views

Experiment 5

Data Base Management System 5

Uploaded by

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

Experiment 5

Data Base Management System 5

Uploaded by

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

‭KJSCE/IT/SYBTECH/SEMIII/DMS/2024-25‬

‭Experiment No.: 05‬

‭Title:‬‭To implement aggregate functions with order‬


‭by, group by, like and having clause.‬

‭(‬‭A Constituent College of Somaiya VidyaviharUniversity‬‭)‬


‭KJSCE/IT/SYBTECH/SEMIII/DMS/2024-25‬

‭Batch:‬‭C-2‬ ‭Roll No. :‬‭16014223049‬ ‭Experiment No: 05‬

‭ im:‬‭To implement aggregate functions with order by,‬‭group by, like and having clause.‬
A
‭___________________________________________________________________________‬

‭Resources needed:‬‭PostgreSQL PgAdmin4‬

_‭ __________________________________________________________________________‬
‭Theory:‬
‭The‬ ‭ORDER‬ ‭BY clause‬ ‭is‬ ‭used‬ ‭to‬ ‭sort‬‭the‬‭data‬‭in‬‭ascending‬‭or‬‭descending‬‭order,‬‭based‬‭on‬
‭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
‭together‬‭those‬‭rows‬‭in‬‭a‬‭table‬‭that‬‭have‬‭identical‬‭data.‬‭This‬‭is‬‭done‬‭to‬‭eliminate‬‭redundancy‬
‭in the output and/or compute aggregates that apply to these groups.‬
‭The‬ ‭GROUP‬ ‭BY‬ ‭clause‬ ‭follows‬ ‭the‬ ‭WHERE‬ ‭clause‬ ‭in‬ ‭a‬ ‭SELECT‬‭statement‬‭and‬‭precedes‬
‭the ORDER BY clause.‬
SELECT column‬
‭ -‭
‭l‬ist‬
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‬ ‭will‬‭return‬
‭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‬ ‭two‬‭signs‬‭is‬‭not‬‭used‬‭in‬‭conjunction‬‭with‬‭the‬‭LIKE‬‭clause,‬‭then‬‭the‬‭LIKE‬
‭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‬

‭ HERE SALARY::text LIKE‬


W ‭Finds any values that start with 200‬
‭'200%'‬

‭ HERE SALARY::text LIKE‬


W ‭Finds any values that have 200 in any position‬
‭'%200%'‬

‭ HERE SALARY::text LIKE‬


W ‭ inds any values that have 00 in the second and third‬
F
‭'_00%'‬ ‭positions‬

‭ HERE SALARY::text LIKE‬


W ‭ inds any values that start with 2 and are at least 3‬
F
‭'2_%_%'‬ ‭characters in length‬

‭ HERE SALARY::text LIKE‬


W ‭Finds any values that end with 2‬
‭'%2'‬

‭ HERE SALARY::text LIKE‬


W ‭ inds any values that have a 2 in the second position and‬
F
‭'_2%3'‬ ‭end with a 3‬

‭(Autonomous College Affiliated to University of Mumbai)‬


‭KJSCE/IT/SYBTECH/SEMIII/DMS/2024-25‬
‭ HERE SALARY::text LIKE‬
W ‭ inds any values in a five-digit number that start with 2 and‬
F
‭'2___3'‬ ‭end with 3‬

‭ he‬ ‭HAVING‬‭clause‬‭allows‬‭us‬‭to‬‭pick‬‭out‬‭particular‬‭rows‬‭where‬‭the‬‭function's‬‭result‬‭meets‬
T
‭some condition.‬
‭The‬‭WHERE‬‭clause‬‭places‬‭conditions‬‭on‬‭the‬‭selected‬‭columns,‬‭whereas‬‭the‬‭HAVING‬‭clause‬
‭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‬

‭__________________________________________________________________________‬

‭Results: (Queries printout with output)‬


‭1.‬ ‭Write 13 queries using ‘order by’, ‘group by’, ‘like’ and ‘having’ clause.‬
‭5 with normal aggregate fun,3 with clauses and aggregate function and 5 with like‬
‭operator‬

‭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‭
;‬‬

‭(Autonomous College Affiliated to University of Mumbai)‬


‭ JSCE/IT/SYBTECH/SEMIII/DMS/2024-25‬
K
‭___________________________________________________________________________‬

‭Outcomes:‬

‭ ELECT RestroID, AVG(Ratings) AS AvgRating‬


S
‭FROM Reviews‬
‭GROUP BY RestroID;‬

‭ ELECT RestroID, COUNT(ReviewID) AS ReviewCount‬


S
‭FROM Reviews‬
‭GROUP BY RestroID;‬

‭ ELECT RestroID, SUM(Price) AS TotalDishCost‬


S
‭FROM Specials‬
‭GROUP BY RestroID;‬

‭ ELECT RestroID, MIN(Ratings) AS MinRating‬


S
‭FROM Reviews‬
‭GROUP BY RestroID;‬

‭ ELECT RestroID, MAX(Ratings) AS MaxRating‬


S
‭FROM Reviews‬
‭GROUP BY RestroID;‬

‭ ELECT RestroID, AVG(Ratings) AS AvgRating‬


S
‭FROM Reviews‬
‭GROUP BY RestroID‬
‭HAVING AVG(Ratings) > 4;‬

‭ ELECT RestroID, COUNT(ReviewID) AS ReviewCount‬


S
‭FROM Reviews‬
‭GROUP BY RestroID‬
‭HAVING COUNT(ReviewID) > 2;‬

‭ ELECT RestroID, SUM(Price) AS TotalDishCost‬


S
‭FROM Specials‬
‭GROUP BY RestroID‬
‭HAVING SUM(Price) > 1000;‬

‭ ELECT RestroName, Cuisine‬


S
‭FROM Restaurant‬
‭WHERE RestroName LIKE '%Pizza%';‬

‭ ELECT UserName, EMail‬


S
‭FROM Users‬
‭WHERE EMail LIKE '%gmail.com';‬

‭(Autonomous College Affiliated to University of Mumbai)‬


‭KJSCE/IT/SYBTECH/SEMIII/DMS/2024-25‬
‭ ELECT RestroName, Address‬
S
‭FROM Restaurant‬
‭WHERE Cuisine LIKE '%Indian%';‬

‭ ELECT DishName, Price‬


S
‭FROM Specials‬
‭WHERE DishName LIKE '%Chicken%';‬

‭ ELECT Comments, Ratings‬


S
‭FROM Reviews‬
‭WHERE Comments LIKE '%good%';‬

‭(Autonomous College Affiliated to University of Mumbai)‬


‭KJSCE/IT/SYBTECH/SEMIII/DMS/2024-25‬

‭(Autonomous College Affiliated to University of Mumbai)‬


‭KJSCE/IT/SYBTECH/SEMIII/DMS/2024-25‬

‭(Autonomous College Affiliated to University of Mumbai)‬


‭KJSCE/IT/SYBTECH/SEMIII/DMS/2024-25‬

‭(Autonomous College Affiliated to University of Mumbai)‬


‭KJSCE/IT/SYBTECH/SEMIII/DMS/2024-25‬

‭(Autonomous College Affiliated to University of Mumbai)‬


‭KJSCE/IT/SYBTECH/SEMIII/DMS/2024-25‬
‭Questions:‬

‭Q1 Can you apply like operator on integer value? explain with example how?‬

‭ 2‬‭Why‬‭aggregate‬‭functions‬‭are‬‭more‬‭used‬‭with‬‭order‬‭by,‬‭group‬‭by‬‭and‬‭having‬‭clauses?‬
Q
‭Can we change order of these clauses when used in single query‬

‭Answers :‬

‭Ans 1.‬

‭ he‬‭̀LIKE`‬‭operator‬‭is‬‭generally‬‭used‬‭with‬‭string‬‭data‬‭types‬‭to‬‭search‬‭for‬‭a‬‭specified‬‭pattern‬
T
‭within‬ ‭a‬ ‭column.‬ ‭It‬ ‭is‬ ‭not‬ ‭typically‬ ‭used‬ ‭with‬ ‭integer‬‭values‬‭because‬‭̀LIKE`‬‭is‬‭designed‬‭to‬
‭handle‬‭text-based‬‭pattern‬‭matching.‬‭However,‬‭you‬‭can‬‭use‬‭̀LIKE`‬‭with‬‭integer‬‭values‬‭if‬‭the‬
‭integer‬ ‭column‬ ‭is‬ ‭implicitly‬ ‭or‬ ‭explicitly‬ ‭cast‬ ‭to‬ ‭a‬ ‭string‬ ‭data‬ ‭type‬ ‭within‬ ‭the‬ ‭query.‬ ‭For‬
‭instance,‬ ‭if‬ ‭you‬ ‭have‬ ‭a‬ ‭column‬ ‭of‬‭type‬‭̀INT`‬‭and‬‭you‬‭want‬‭to‬‭search‬‭for‬‭numbers‬‭that‬‭start‬
‭with‬ ‭a‬ ‭certain‬ ‭digit,‬ ‭you‬ ‭would‬ ‭first‬ ‭convert‬ ‭the‬‭integer‬‭column‬‭to‬‭a‬‭string‬‭using‬‭a‬‭function‬
‭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‬ ‭and‬‭return‬‭a‬‭single‬‭result.‬‭These‬‭functions‬‭are‬‭often‬
‭employed‬ ‭in‬ ‭conjunction‬ ‭with‬ ‭the‬ ‭̀ORDER‬ ‭BY`,‬‭̀GROUP‬‭BY`,‬‭and‬‭̀HAVING`‬‭clauses‬‭to‬
‭summarize‬‭and‬‭organize‬‭data‬‭meaningfully.‬‭The‬‭̀GROUP‬‭BY`‬‭clause‬‭groups‬‭rows‬‭that‬‭have‬
‭the‬‭same‬‭values‬‭into‬‭summary‬‭rows,‬‭such‬‭as‬‭calculating‬‭the‬‭total‬‭sales‬‭per‬‭product‬‭category.‬
‭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‬
‭̀ORDER‬‭BY`‬‭clause‬‭arranges‬‭the‬‭result‬‭set‬‭in‬‭a‬‭specific‬‭order,‬‭such‬‭as‬‭sorting‬‭categories‬‭by‬
‭their total sales in descending order.‬

‭ he‬ ‭order‬ ‭of‬ ‭these‬ ‭clauses‬ ‭in‬ ‭a‬ ‭SQL‬ ‭query‬ ‭is‬ ‭fixed‬ ‭and‬ ‭cannot‬ ‭be‬ ‭changed:‬ ‭̀GROUP‬‭BY`‬
T
‭comes‬ ‭before‬ ‭̀HAVING`,‬ ‭which‬ ‭comes‬ ‭before‬ ‭̀ORDER‬ ‭BY`.‬ ‭The‬ ‭̀GROUP‬‭BY`‬‭clause‬‭is‬
‭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.‬‭For‬‭example,‬‭in‬‭a‬‭query‬‭that‬‭calculates‬‭the‬‭total‬‭sales‬‭per‬‭category,‬‭filters‬‭categories‬
‭with‬‭sales‬‭above‬‭1000,‬‭and‬‭sorts‬‭them‬‭by‬‭total‬‭sales,‬‭you‬‭must‬‭use‬‭̀GROUP‬‭BY`‬‭to‬‭aggregate‬
‭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:‬

‭(Autonomous College Affiliated to University of Mumbai)‬


‭KJSCE/IT/SYBTECH/SEMIII/DMS/2024-25‬

‭Grade: AA / AB / BB / BC / CC / CD /DD‬

‭Signature of faculty in-charge with date‬

‭__________________________________________________________________________________‬

‭References:‬

‭Books:‬

‭1.‬ E ‭ lmasri‬ ‭and‬ ‭Navathe,‬ ‭“Fundamentals‬ ‭of‬ ‭Database‬ ‭Systems”,‬ ‭6‭t‬h‬ ‭Edition,‬ ‭Pearson‬
‭Education‬
‭2.‬ ‭Korth, Slberchatz,Sudarshan, :”Database System Concepts”, 6th Edition, McGraw –‬
‭Hill.‬

‭(Autonomous College Affiliated to University of Mumbai)‬

You might also like