Scalar Function to Calculate Discount Price
USE SalesInventoryDB → Tells MySQL to work inside the "SalesInventoryDB" database.
DELIMITER // → Temporarily changes the command separator so MySQL knows where the function ends.
CREATE FUNCTION → Defines a new function named GetDiscountPrice
.
Parameters:
Original_Price
: Actual product price.
Discount_Percent
: Discount to be applied (in percentage).
Formula used:
Discounted Price = Original_Price - (Original_Price * Discount_Percent / 100)
RETURN → Gives the final calculated price.
CALL / SELECT → You test it by passing values like 1000 and 10 → Output will be 900.00
.
SQL Query:
-- Step 1: Set the database first
USE SalesInventoryDB;
-- Step 2: Change delimiter for function definition
DELIMITER //
-- Step 3: Create function
CREATE FUNCTION GetDiscountPrice(
Original_Price DECIMAL(10,2),
Discount_Percent DECIMAL(5,2)
)
RETURNS DECIMAL(10,2)
DETERMINISTIC
BEGIN
DECLARE discount_price DECIMAL(10,2);
SET discount_price = Original_Price - (Original_Price * Discount_Percent / 100);
RETURN discount_price;
END //
-- Step 4: Reset the delimiter back
DELIMITER ;
-- Step 5: Use the function
SELECT GetDiscountPrice(1000, 10) AS DiscountedPrice;
Output:
Trainings :
Data Science Training in Indore | Data Analytics Training in Indore | Python Training in Indore | Blockchain Training in Indore | React JS Training in Indore | Web Development Training in Indore | Full Stack Development Training in Indore |Free Courses and Resource :
Verbal Aptitude | Matplotlib Tutorials | Examples | Interview Questions | Pandas Tutorials | Projects | Interview Questions | OpenCV Tutorials | Projects | Interview Questions | ExpressJS Tutorials | Projects | Interview Questions | Dart | Python Data Structures and Algorithms | Rust Tutorials | Projects | Interview Questions | Aptitude Tests | HackerRank Python | Django | MongoDB Tutorials | Examples | Interview Questions | NumPy Practice Questions | Python Tutorials by CodersDaily | Golang Tutorials | Projects | Interview Questions | React.js Tutorials | Verbal Ability Tutorial | MS Sql Server Tutorials | Examples | Interview Questions | Power BI Tutorials | Projects | Interview Questions | Example Dashboards | Numpy Tutorials | Projects | Interview Questions | Django REST Framework Tutorial | HackerRank C++ Solutions | Tensor Flow | Pandas Practice Questions | Python Practice Questions | C++ Tutorials | Quantitative Ability Tutorial | Javascript | Node.js Tutorials | HackerRank SQL Solutions | HackerRank DSA Solutions | Verbal Aptitude 2 | HackerRank Java Solutions | HTML Tutorial | TCS NQT Mock Test Series | Reasoning Ability Tutorial | CodeChef Python Solutions | HackerRank C Program Solutions | Leetcode Python Solutions | SQL Practice Question | Matplotlib Practice Questions |Interview Questions :
Pandas Tutorials | Projects | Interview Questions | ExpressJS Tutorials | Projects | Interview Questions | Django | Python Tutorials by CodersDaily | Golang Tutorials | Projects | Interview Questions | Numpy Tutorials | Projects | Interview Questions | Django REST Framework Tutorial |Top Colleges in India :
Indian Institute of Technology Bombay | Jaypee University of Engineering and Technology - Guna |