← Back to PostgreSQL Course | Chapter 2: Basic Queries | Lesson 4 of 7

तार्किक Operators (AND OR NOT)

AND, OR, और NOT conditions combine करते हैं, parentheses order control करते हुए।
Syntax
sql
WHERE condition1 AND condition2
WHERE condition1 OR condition2
WHERE NOT condition

Logical Operators (AND OR NOT)

AND को दोनों conditions चाहिए, OR को कम से कम एक चाहिए, और NOT एक condition reverse करता है। AND, OR से पहले evaluate होता है, इसलिए इन्हें mix करते वक्त parentheses इस्तेमाल करें। NULL के साथ कोई भी comparison NULL (unknown) देता है, जिसे WHERE false की तरह treat करता है।

Note: जब भी आप AND और OR mix करें parentheses add करें।

उदाहरण: Logical operators (AND OR NOT)

sql
CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, city TEXT, age INTEGER);
INSERT INTO users VALUES (1, 'Ada', 'London', 36), (2, 'Bob', 'Oslo', 25), (3, 'Cy', 'Oslo', 41), (4, 'Di', 'Rome', 30);
SELECT name FROM users WHERE city = 'Oslo' AND age > 30;
SELECT name FROM users WHERE city = 'Rome' OR age > 40;
SELECT name FROM users WHERE NOT city = 'Oslo';
SELECT name FROM users WHERE (city = 'Oslo' OR city = 'Rome') AND age < 35;

-- Output:
-- name
-- Cy
-- name
-- Cy
-- Di
-- name
-- Ada
-- Di
-- name
-- Bob
-- Di
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह भूल जाना कि AND, OR से ज़्यादा precedence रखता है
  2. NOT conditions में NULL ignore करना
  3. बहुत लंबी unparenthesised conditions
चैप्टर सारांश
  • AND दोनों, OR कोई भी, NOT reverse
  • AND, OR से ज़्यादा tightly bind करता है
  • Explicit होने के लिए parentheses इस्तेमाल करें
  • NULL एक condition को unknown बनाता है
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.