Home

11: Utility Operators Keywords And Functions

The Greatest Value in a List

SELECT GREATEST(200, 10, 40); -- returns 200

An example of needing this might be to calculate shipping costs.

SELECT name, weight, GREATEST(30, 2 * weight) AS shipping_cost FROM products;

The Least Value in a List

SELECT LEAST(200, 10, 40); -- returns 10

The CASE keyword

"If price > 600 then 'high, if price > 300 then 'medium' else 'cheap'"

SELECT name, price, CASE price WHEN 600 THEN 'high' WHEN 300 THEN 'medium' ELSE 'cheap' END AS price_category FROM products;

Repository

https://github.com/okeeffed/developer-notes-nextjs/content/postgresql/SQL-And-PostgreSQL-The-Complete-Developers-Guide/11-Utility-Operators-Keywords-And-Functions

Sections


Related