6-12 here

Week 6:

 Aggregate Functions:


1. Find the total sales amount for each product category:


SELECT category, SUM(sales_amount) AS total_sales

FROM sales

GROUP BY category;


JOINs:


2. Retrieve customer information along with their orders:


SELECT c.customer_id, c.name, o.order_id, o.order_date

FROM customers c

INNER JOIN orders o ON c.customer_id = o.customer_id;


Subqueries:


3.Find customers who have placed orders in the last month:


SELECT customer_id, name

FROM customers

WHERE customer_id IN (

    SELECT DISTINCT customer_id

    FROM orders

    WHERE order_date >= TRUNC(SYSDATE, 'MM') - INTERVAL '1' MONTH

);


Common Table Expressions (CTEs):


4.Find the top 3 best-selling products:


WITH ranked_products AS (

    SELECT product_id, SUM(quantity) AS total_sold,

           RANK() OVER (ORDER BY SUM(quantity) DESC) AS sales_rank

    FROM order_details

    GROUP BY product_id

)

SELECT p.product_name, rp.total_sold

FROM products p

INNER JOIN ranked_products rp ON p.product_id = rp.product_id

WHERE rp.sales_rank <= 3;



Window Functions:


5.Calculate the running total of sales for each month:


SELECT sales_date, SUM(sales_amount) OVER (ORDER BY sales_date) AS running_total

FROM sales_data;


Week 7:

1. Retrieve the length of the names of employees:


SELECT first_name, LENGTH(first_name) AS name_length

FROM employees;


2.Convert the hire dates of employees to a different format:


SELECT employee_id, TO_CHAR(hire_date, 'DD-MON-YYYY') AS formatted_hire_date

FROM employees;


3. Get the uppercase version of employee last names:


SELECT last_name, UPPER(last_name) AS upper_last_name

FROM employees;


4. Calculate the age of employees based on their birth dates:


SELECT employee_id, TRUNC(MONTHS_BETWEEN(SYSDATE, birth_date) / 12) AS age

FROM employees;


5.Concatenate employee first and last names with a space in between:


SELECT first_name, last_name, CONCAT(first_name, ' ', last_name) AS full_name

FROM employees;


6.Extract the month from the hire date of employees:


SELECT employee_id, TO_CHAR(hire_date, 'MM') AS hire_month

FROM employees;


7.Get the initials of employee names:


SELECT first_name, last_name, SUBSTR(first_name, 1, 1) || SUBSTR(last_name, 1, 1) AS initials

FROM employees;


Week 8:

Unnamed PL/SQL Block:


DECLARE

    v_employee_count NUMBER;

BEGIN

    -- Count the number of employees in the employee table

    SELECT COUNT(*) INTO v_employee_count FROM employees;

    

    -- Print the number of employees

    DBMS_OUTPUT.PUT_LINE('Total number of employees: ' || v_employee_count);

END;

/


Named PL/SQL Block:


CREATE OR REPLACE PROCEDURE display_employee_count IS

    v_employee_count NUMBER;

BEGIN

    -- Count the number of employees in the employees table

    SELECT COUNT(*) INTO v_employee_count FROM employee;

    

    -- Print the number of employees

    DBMS_OUTPUT.PUT_LINE('Total number of employees: ' || v_employee_count);

END display_employee_count;

/


BEGIN

    display_employee_count;

END;


Week 9:

Implicit Cursor:


DECLARE

    v_employee_id emp.empno%TYPE;

    v_employee_name emp.ename%TYPE;

BEGIN

    

    SELECT empno, ename INTO v_employee_id, v_employee_name

    FROM emp

    WHERE empno = 7844; -- Assuming employee_id 100 exists


    

    DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_employee_id);

    DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_employee_name);

END;

/


Explicit Cursor:


DECLARE

    CURSOR emp_cursor IS

        SELECT empno, ename

        FROM emp

        WHERE deptno = 30; -- Assuming department_id 30 exists

    v_employee_id emp.empno%TYPE;

    v_employee_name emp.ename%TYPE;

BEGIN

    OPEN emp_cursor; -- Open the cursor


    LOOP

        FETCH emp_cursor INTO v_employee_id, v_employee_name; -- Fetch data into variables

        EXIT WHEN emp_cursor%NOTFOUND; -- Exit loop when no more rows

        -- Print the employee ID and name

        DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_employee_id);

        DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_employee_name);

    END LOOP;


Week 10:

Predefined Exception:


DECLARE

    v_salary emp.sal%TYPE;

BEGIN

    -- Retrieve salary of an employee with ID 9999 (assuming employee_id 9999 doesn't exist)

    SELECT sal INTO v_salary

    FROM emp

    WHERE empno = 9999;

EXCEPTION

    WHEN NO_DATA_FOUND THEN

        DBMS_OUTPUT.PUT_LINE('Employee not found.');

    WHEN OTHERS THEN

        DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);

END;

/


User-defined Exception:


DECLARE

    v_age emp.age%TYPE;

    v_min_age CONSTANT NUMBER := 18;

    v_max_age CONSTANT NUMBER := 60;

    -- Define a user-defined exception

    age_out_of_range EXCEPTION;

BEGIN

    -- Retrieve age of an employee with ID 100 (assuming employee_id 100 exists)

    SELECT age INTO v_age

    FROM emp

    WHERE empno = 7934;

    

    -- Check if age is within the acceptable range

    IF v_age < v_min_age OR v_age > v_max_age THEN

        -- Raise the user-defined exception if age is out of range

        RAISE age_out_of_range;

    ELSE

        DBMS_OUTPUT.PUT_LINE('Employee age is within the acceptable range.');

    END IF;

EXCEPTION

    WHEN age_out_of_range THEN

        DBMS_OUTPUT.PUT_LINE('Employee age is out of range.');

    WHEN OTHERS THEN

        DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);

END;

/


Week 11:

Stored Procedure:


CREATE OR REPLACE PROCEDURE get_employee_info (

    p_employee_id IN emp.empno%TYPE,

    p_salary OUT emp.sal%TYPE,

    p_job_title OUT emp.job%TYPE

)

IS

BEGIN

    -- Retrieve salary and job title of the employee with the given ID

    SELECT sal, job INTO p_salary, p_job_title

    FROM emp

    WHERE empno = p_employee_id;

END get_employee_info;

/


Function:


CREATE OR REPLACE FUNCTION calculate_bonus (

    p_salary IN emp.sal%TYPE

)

RETURN NUMBER

IS

    v_bonus NUMBER;

BEGIN

    -- Calculate bonus as 10% of the salary

    v_bonus := p_salary * 0.1;

    RETURN v_bonus;

END calculate_bonus;

/


CREATE OR REPLACE PACKAGE employee_package AS

    -- Procedure to update employee's salary

    PROCEDURE update_salary (

        p_employee_id IN emp.empno%TYPE,

        p_new_salary IN emp.sal%TYPE

    );

    

    -- Function to get employee's age

    FUNCTION get_employee_age (

        p_employee_id IN emp.empno%TYPE

    ) RETURN NUMBER;

END employee_package;

/


Package:


CREATE OR REPLACE PACKAGE BODY employee_package AS

    -- Procedure implementation to update employee's salary

    PROCEDURE update_salary (

        p_employee_id IN employees.employee_id%TYPE,

        p_new_salary IN employees.salary%TYPE

    )

    IS

    BEGIN

        UPDATE employees

        SET salary = p_new_salary

        WHERE employee_id = p_employee_id;

    END update_salary;

    

    -- Function implementation to get employee's age

    FUNCTION get_employee_age (

        p_employee_id IN employees.employee_id%TYPE

    ) RETURN NUMBER

    IS

        v_age NUMBER;

    BEGIN

        -- Calculate employee's age (example logic)

        SELECT TRUNC(MONTHS_BETWEEN(SYSDATE, hire_date) / 12)

        INTO v_age

        FROM employees

        WHERE employee_id = p_employee_id;

        

        RETURN v_age;

    END get_employee_age;

END employee_package;

/


Week 12:

CREATE OR REPLACE TRIGGER update_last_modified

BEFORE UPDATE ON emp

FOR EACH ROW

BEGIN

    :NEW.last_modified := SYSDATE;

END;

/

Comments