SELECT SIGN(X)
Output will -1,0,1 based on the value of XSELECT SIGN(-4.5); Output -1
SELECT SIGN(+4.5); Output 1
SELECT SIGN(4.5); Output 1
SELECT SIGN(0); Output 0
If we try a sting we will get 0 as output
SELECT SIGN('abc'); // Output 0
What is the output of sign(0) ?
SELECT product, buy_price, sell_price, sell_price - buy_price AS difference FROM plus2_price
| product | buy_price | sell_price | difference | |
|---|---|---|---|---|
| Product1 | 10 | 15 | 5 | |
| Product1 | 20 | 15 | -5 | |
| Product1 | 10 | 10 | 0 | |
| Product1 | 20 | 25 | 5 |
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 Prift No Loss' WHEN -1 THEN 'Loss'
END AS Result FROM plus2_price
More about CASE condition Query
| product | buy_price | sell_price | Difference | Result | |
|---|---|---|---|---|---|
| Product1 | 10 | 15 | 1 | Profit | |
| Product1 | 20 | 15 | -1 | Loss | |
| Product1 | 10 | 10 | 0 | No Prift No Loss | |
| Product1 | 20 | 25 | 1 | Profit |
CREATE TABLE IF NOT EXISTS `plus2_price` (
`product` varchar(10) NOT NULL,
`buy_price` int(3) NOT NULL,
`sell_price` int(3) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `plus2_price`
--
INSERT INTO `plus2_price` (`product`, `buy_price`, `sell_price`) VALUES
('Product1', 10, 15),
('Product1', 20, 15),
('Product1', 10, 10),
('Product1', 20, 25);
SQL Math References
ABS() to get Absolute value
Standard deviation
Average value Query
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.