0% found this document useful (0 votes)
2 views19 pages

7-12_merged

The document contains SQL code snippets demonstrating various database operations, including UNION, INTERSECT, and EXCEPT for querying data from customers and employees. It also includes procedures for classifying products, handling exceptions, and updating tables using functions. Additionally, there are examples of creating views and triggers to manage and log database changes.
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)
2 views19 pages

7-12_merged

The document contains SQL code snippets demonstrating various database operations, including UNION, INTERSECT, and EXCEPT for querying data from customers and employees. It also includes procedures for classifying products, handling exceptions, and updating tables using functions. Additionally, there are examples of creating views and triggers to manage and log database changes.
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/ 19

CODE :

-- UNION: All unique rows from both tables


SELECT name, age, city FROM customers
UNION
SELECT name, age, city FROM employees;

-- INTERSECT: Common rows (name must match)


SELECT customers.name, customers.age, customers.city
FROM customers
INNER JOIN employees ON customers.name = employees.name;

-- EXCEPT: Rows in customers not in employees


SELECT customers.name, customers.age, customers.city
FROM customers
LEFT JOIN employees ON customers.name = employees.name
WHERE employees.name IS NULL;

OUTPUT :
CODE :

-- Step 1: Create the view to calculate total orders per customer


CREATE VIEW customer_order_totals AS
SELECT customer_id, SUM(total_amount) AS total_amount
FROM orders
GROUP BY customer_id;

-- Step 2: Query the view to get top 10 customers by total amount


SELECT customer_id, total_amount
FROM customer_order_totals
ORDER BY total_amount DESC
LIMIT 10;

OUTPUT :
CODE :

CREATE PROCEDURE classify_products


AS
BEGIN
DECLARE @id INT = 1;
DECLARE @maxId INT;
DECLARE @name VARCHAR(100);
DECLARE @price DECIMAL(10,2);
DECLARE @category VARCHAR(50);
DECLARE @label VARCHAR(50);

-- Get the maximum product_id to control the loop


SELECT @maxId = MAX(product_id) FROM products;

-- Temporary table to store results


CREATE TABLE #ProductClassification (
product_name VARCHAR(100),
category VARCHAR(50),
label VARCHAR(50)
);

-- Loop through each product and classify it


WHILE @id <= @maxId
BEGIN
-- Get the product name and price for the current product
SELECT @name = product_name, @price = price
FROM products
WHERE product_id = @id;

-- Classify the product based on its price


IF @price > 500
SET @category = 'Expensive';
ELSE IF @price BETWEEN 100 AND 500
SET @category = 'Moderate';
ELSE
SET @category = 'Cheap';

-- Assign a label based on the price


SET @label = CASE
WHEN @price < 20 THEN 'Very Low Cost'
WHEN @price BETWEEN 20 AND 100 THEN 'Affordable'
ELSE 'Premium'
END;

-- Insert the result into the temporary table


INSERT INTO #ProductClassification (product_name, category, label)
VALUES (@name, @category, @label);

-- Increment the id to process the next product


SET @id = @id + 1;
END;

-- Return the classified product data


SELECT * FROM #ProductClassification;

-- Clean up the temporary table


DROP TABLE #ProductClassification;
END;
GO

EXEC classify_products;

OUTPUT :
CODE :

DELIMITER //

CREATE PROCEDURE handle_exception_demo()


BEGIN
DECLARE custom_exception CONDITION FOR SQLSTATE '45000';

DECLARE EXIT HANDLER FOR custom_exception


BEGIN
SELECT '⚠️ Exception caught: An error occurred while processing.' AS
error_message;
END;

IF EXISTS (SELECT * FROM demo_table WHERE value > 15) THEN


SIGNAL custom_exception SET MESSAGE_TEXT = 'Value greater than 15
found.';
END IF;

SELECT '✅ No exception occurred.' AS status_message;


END //

DELIMITER ;
CALL handle_exception_demo();

OUTPUT :
CODE :

DELIMITER $$

CREATE FUNCTION my_function(param1 INT, param2 INT) RETURNS INT


DETERMINISTIC
BEGIN
DECLARE result INT;
SET result = param1 + param2;
RETURN result;
END$$

DELIMITER ;

-- Step 4: Create a procedure that uses the function to update data


DELIMITER $$

CREATE PROCEDURE my_procedure(IN val1 INT, IN val2 INT)


BEGIN
DECLARE total INT;
-- Call the function to get the sum
SET total = my_function(val1, val2);

-- Update the table where column2 = val2


UPDATE my_table
SET column1 = total
WHERE column2 = val2;
END$$

DELIMITER ;

-- Step 5: Call the procedure


CALL my_procedure(5, 40);

-- Step 6: View the updated table


SELECT * FROM my_table;
OUTPUT :
CODE :

DELIMITER $$

CREATE TRIGGER my_trigger


AFTER INSERT ON my_table
FOR EACH ROW
BEGIN
INSERT INTO my_log_table (timestamp, message)
VALUES (NOW(), CONCAT('A new row was inserted into my_table. Inserted
name: ', NEW.name));
END$$

DELIMITER ;

-- Insert data into my_table


INSERT INTO my_table (name) VALUES ('John Doe');

-- Check logs
SELECT * FROM my_log_table;

OUTPUT :

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy