bhylu.com

Advertisements

SQL Or

OR keeps a row when at least one condition is true. Use parentheses when a question mixes OR with AND so another developer can see the intended rule without remembering operator precedence.

Understand the idea

Suppose a shop promotes Home and Accessories products, but only when they are in stock. The category alternatives belong inside parentheses. The stock test then applies to both alternatives. Without those parentheses, AND binds more tightly than OR and can change which rows are admitted.

Where to try it

Run this example in the SQL playground. The lesson link loads the shop tables and sample rows before this query. Select Run when you are ready. Every run starts fresh, so your changes do not affect the website or a previous run.

SQLite sample data · MySQL / MariaDB sample data · See the sample tables

A worked example

SQLite

SELECT name, category
FROM products
WHERE (category = 'Home' OR category = 'Accessories')
  AND stock > 0
ORDER BY product_id;

Read the result

Only Desk lamp is returned. Canvas bag belongs to a promoted category but has no stock. This is a useful small dataset for checking that the parentheses express the real rule.

Try one change

Remove the parentheses, swap the two category conditions, and compare the result. Restore the version that matches the stated promotion rule.