Code Monkey home page Code Monkey logo

dbms-ex04's Introduction

Dbms-ex04 - AGGREGATE FUNCTION

DATE: 26-03-24

REGISTER NUMBER:212222040005

AIM:

To study and write aggregation functions, group by and having clause with suitable examples.

THEORY

Aggregation Functions

*An aggregate function is a function that performs a calculation on a set of values, and returns a single value. *Aggregate functions are often used with the GROUP BY clause of the SELECT statement.

List of Aggregation functions:

  1. MIN() - returns the smallest value within the selected column Syntax: SELECT MIN(column_name) FROM table_name WHERE condition;
  2. MAX() - returns the largest value within the selected column Syntax: SELECT MAX(column_name) FROM table_name WHERE condition; 3.COUNT() - returns the number of rows in a set Syntax: SELECT COUNT(column_name) FROM table_name WHERE condition;
  3. SUM() - returns the total sum of a numerical column Syntax: SELECT SUM(column_name) FROM table_name WHERE condition; 5.AVG() - returns the average value of a numerical column Syntax: SELECT AVG(column_name) FROM table_name WHERE condition;

GROUP BY CLAUSE

GROUP BY: This query is used to group all the records in a relation together for each and every value of a specific key(s) and then display them for a selected set of fields in the relation. Syntax: SELECT column_name(s) FROM table_name WHERE condition GROUP BY column_name(s) ORDER BY column_name(s);

HAVING CLAUSE

The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions. The HAVING clause must follow the GROUP BY clause in a query and must also precede the ORDER BY clause if used. Syntax: SELECT column_name(s) FROM table_name WHERE condition GROUP BY column_name(s) HAVING condition ORDER BY column_name(s);

#PROCEDURE:

  1. Start the program.
  2. Read the given query.
  3. Perform the given aggregation function using MIN(),MAX(),COUNT(),SUM(),AVG().
  4. To group all the records in a relation together by GROUP BY clause. SELECT column_name(s) FROM table_name WHERE condition GROUP BY column_name(s);
  5. To specify the conditions in a grouped records by HAVING clause. SELECT column_name(s) FROM table_name WHERE condition GROUP BY column_name(s) HAVING condition;
  6. Show the output
  7. Stop the program

QUERY:

QUERY 1

image

SQL QUERY

SELECT SUM(workhour) AS "Total working hours" FROM employee1;

TEST QUERY AND ITS OUTPUT

image

QUERY 2

image

SQL QUERY

SELECT COUNT(DISTINCT salesman_id) AS COUNT FROM orders;

TEST QUERY AND ITS OUTPUT

image

QUERY 3

image

SQL QUERY

SELECT name, MAX(LENGTH(name)) AS length FROM customer;

TEST QUERY AND ITS OUTPUT

image

QUERY 4

image

SQL QUERY

SELECT 
    CASE
        WHEN (julianday('now') - julianday(DateOfBirth)) / 365.25 < 20 THEN 'Under 20'
        WHEN (julianday('now') - julianday(DateOfBirth)) / 365.25 BETWEEN 20 AND 30 THEN '20-30'
        WHEN (julianday('now') - julianday(DateOfBirth)) / 365.25 BETWEEN 31 AND 40 THEN '31-40'
        WHEN (julianday('now') - julianday(DateOfBirth)) / 365.25 BETWEEN 41 AND 50 THEN '41-50'
        ELSE 'Above 50'
    END AS AgeGroup,
    COUNT(*) AS TotalPatients
FROM Patients
GROUP BY AgeGroup;

TEST QUERY AND ITS OUTPUT

image

QUERY 5

image

SQL QUERY

SELECT DoctorID, COUNT(*) AS TotalAppointments
FROM Appointments
GROUP BY DoctorID;

TEST QUERY AND ITS OUTPUT

image

QUERY 6

image

SQL QUERY

SELECT Specialty, Gender, COUNT(*) AS TotalDoctors FROM Doctors GROUP BY Specialty, Gender;

TEST QUERY AND ITS OUTPUT

image

QUERY 7

image

SQL QUERY

SELECT occupation, SUM(workhour)
FROM employee1
GROUP BY occupation
HAVING SUM(workhour) > 20;;

TEST QUERY AND ITS OUTPUT

image

QUERY 8

image

SQL QUERY

SELECT age, AVG(income)
FROM employee
GROUP BY age
HAVING AVG(income) BETWEEN 300000 AND 500000;

TEST QUERY AND ITS OUTPUT

image

QUERY 9

image

SQL QUERY

SELECT PatientID, COUNT(*) AS TotalRecords
FROM MedicalRecords
GROUP BY PatientID
HAVING COUNT(*) > 3;

TEST QUERY AND ITS OUTPUT

image

QUERY 10

image

SQL QUERY

SELECT category_id, AVG(Price)
FROM products
GROUP BY category_id
HAVING AVG(Price) BETWEEN 10 AND 15;

TEST QUERY AND ITS OUTPUT

image

RESULT :

Thus the basic aggregation commands are executed.

dbms-ex04's People

Contributors

akshayalakshmivs avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.