MySQL SIGN() function

SIGN(X) returns -1, 0, or 1 depending on whether X is negative, zero, or positive.

SELECT SIGN(X);
SELECT SIGN(-4.5); -- Output: -1
SELECT SIGN(4.5); -- Output: 1
SELECT SIGN(0); -- Output: 0

Current MySQL can coerce a nonnumeric string to zero while issuing a warning. The following legacy behavior is shown so you can recognize it, but application queries should use numeric data explicitly.

SELECT SIGN('abc'); -- Result may be 0 with a conversion warning; avoid this pattern

Profit or loss using a price difference

SELECT product, buy_price, sell_price,
       sell_price - buy_price AS difference
FROM plus2_price;
productbuy_pricesell_pricedifference
Product110155
Product12015-5
Product110100
Product120255

CASE with SIGN()

SELECT product, buy_price, sell_price,
       SIGN(sell_price - buy_price) AS Difference,
       CASE SIGN(sell_price - buy_price)
         WHEN 1 THEN 'Profit'
         WHEN 0 THEN 'No Profit No Loss'
         WHEN -1 THEN 'Loss'
       END AS Result
FROM plus2_price;

See CASE expressions for more conditional-query examples.

productbuy_pricesell_priceDifferenceResult
Product110151Profit
Product12015-1Loss
Product110100No Profit No Loss
Product120251Profit

Sample table

CREATE TABLE IF NOT EXISTS plus2_price (
  product VARCHAR(10) NOT NULL,
  buy_price INT NOT NULL,
  sell_price INT NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO plus2_price (product, buy_price, sell_price) VALUES
('Product1', 10, 15),
('Product1', 20, 15),
('Product1', 10, 10),
('Product1', 20, 25);



Subscribe to our YouTube Channel here



plus2net.com




SQL Video Tutorials










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer