SQL Tutorial
SQL Clauses / Operators
SQL-Injection
SQL Functions
SQL Queries
PL/SQL
MySQL
SQL Server
Misc
To compute the sum and average of three numbers in PL/SQL, you can use an anonymous block to declare the variables, compute the required values, and then display the results using the DBMS_OUTPUT.PUT_LINE
method.
Here's a simple PL/SQL block to calculate the sum and average of three numbers:
DECLARE num1 NUMBER := 10; -- you can change these values num2 NUMBER := 20; -- you can change these values num3 NUMBER := 30; -- you can change these values total_sum NUMBER; avg_value NUMBER; BEGIN -- Calculate sum total_sum := num1 + num2 + num3; -- Calculate average avg_value := total_sum / 3; -- Display results DBMS_OUTPUT.PUT_LINE('Sum of the numbers: ' || total_sum); DBMS_OUTPUT.PUT_LINE('Average of the numbers: ' || avg_value); END; /
You can modify the values of num1
, num2
, and num3
as per your requirements. After executing the above block in an Oracle environment with server output enabled, you'll get the sum and average of the given numbers.
PL/SQL Program to Calculate Sum and Average of Three Numbers:
DECLARE num1 NUMBER := 10; num2 NUMBER := 20; num3 NUMBER := 30; total NUMBER; average NUMBER; BEGIN -- Calculate sum total := num1 + num2 + num3; -- Calculate average average := total / 3; -- Display results DBMS_OUTPUT.PUT_LINE('Sum: ' || total); DBMS_OUTPUT.PUT_LINE('Average: ' || average); END; /
Oracle SQL Function for Finding Sum and Average of Three Numbers:
CREATE OR REPLACE FUNCTION calculate_sum_avg ( num1 IN NUMBER, num2 IN NUMBER, num3 IN NUMBER ) RETURN VARCHAR2 IS total NUMBER; average NUMBER; BEGIN -- Calculate sum total := num1 + num2 + num3; -- Calculate average average := total / 3; -- Return results RETURN 'Sum: ' || total || ', Average: ' || average; END calculate_sum_avg; /
Calculating Sum and Average in PL/SQL with Three Input Values:
DECLARE num1 NUMBER := &input1; -- Prompt for user input num2 NUMBER := &input2; num3 NUMBER := &input3; total NUMBER; average NUMBER; BEGIN -- Calculate sum total := num1 + num2 + num3; -- Calculate average average := total / 3; -- Display results DBMS_OUTPUT.PUT_LINE('Sum: ' || total); DBMS_OUTPUT.PUT_LINE('Average: ' || average); END; /
PL/SQL Procedure for Sum and Average Computation of Three Numbers:
CREATE OR REPLACE PROCEDURE calculate_sum_avg_proc ( num1 IN NUMBER, num2 IN NUMBER, num3 IN NUMBER ) IS total NUMBER; average NUMBER; BEGIN -- Calculate sum total := num1 + num2 + num3; -- Calculate average average := total / 3; -- Display results DBMS_OUTPUT.PUT_LINE('Sum: ' || total); DBMS_OUTPUT.PUT_LINE('Average: ' || average); END calculate_sum_avg_proc; /
Oracle SQL Query to Find Sum and Average of Three Numeric Values:
SELECT num1 + num2 + num3 AS total, (num1 + num2 + num3) / 3 AS average FROM your_table;
Oracle SQL Script to Calculate Sum and Average of Three Integers:
DECLARE num1 NUMBER := 15; num2 NUMBER := 25; num3 NUMBER := 35; total NUMBER; average NUMBER; BEGIN -- Calculate sum total := num1 + num2 + num3; -- Calculate average average := total / 3; -- Display results DBMS_OUTPUT.PUT_LINE('Sum: ' || total); DBMS_OUTPUT.PUT_LINE('Average: ' || average); END; /
PL/SQL Function for Sum and Average of Three Positive Numbers:
CREATE OR REPLACE FUNCTION calculate_sum_avg_positive ( num1 IN NUMBER, num2 IN NUMBER, num3 IN NUMBER ) RETURN VARCHAR2 IS total NUMBER; average NUMBER; BEGIN -- Check if numbers are positive IF num1 > 0 AND num2 > 0 AND num3 > 0 THEN -- Calculate sum total := num1 + num2 + num3; -- Calculate average average := total / 3; -- Return results RETURN 'Sum: ' || total || ', Average: ' || average; ELSE RETURN 'Please provide three positive numbers.'; END IF; END calculate_sum_avg_positive; /