SQL OR Operator

SQL OR Operator क्या है?

SQL OR Operator का उपयोग WHERE clause में multiple conditions में से किसी एक condition को true होने पर records select करने के लिए किया जाता है। OR operator तब result return करता है जब कम से कम एक condition true हो।

OR operator का उपयोग SELECT, UPDATE और DELETE statements के साथ किया जाता है।

OR Operator Syntax

SELECT column1, column2
FROM table_name
WHERE condition1 OR condition2;

Example Table

मान लीजिए हमारे पास employees नाम की table है:

idnamedepartmentsalarycity
1AmitIT50000Delhi
2NehaHR35000Jaipur
3RohitIT60000Mumbai
4PoojaFinance30000Delhi

Example 1: OR with SELECT Statement

SELECT * FROM employees
WHERE department = 'IT' OR department = 'HR';

यह query IT या HR department के सभी employees को return करेगी।

Example 2: OR with Different Columns

SELECT * FROM employees
WHERE city = 'Delhi' OR salary > 50000;

यह query उन employees को दिखाएगी जो Delhi में रहते हैं या जिनकी salary 50000 से ज्यादा है।

OR with Multiple Conditions

OR operator को दो से ज्यादा conditions के लिए भी use किया जा सकता है।

SELECT * FROM employees
WHERE department = 'IT'
OR city = 'Delhi'
OR salary < 35000;

OR with IN Operator

Multiple OR conditions को IN operator से simplify किया जा सकता है।

SELECT * FROM employees
WHERE department IN ('IT', 'HR');

OR with LIKE Operator

SELECT * FROM employees
WHERE name LIKE 'A%' OR name LIKE 'P%';

OR Operator with UPDATE

UPDATE employees
SET salary = salary + 5000
WHERE department = 'Finance' OR department = 'HR';

यह query Finance या HR department के employees की salary update करेगी।

OR Operator with DELETE

DELETE FROM employees
WHERE city = 'Jaipur' OR salary < 30000;

यह query Jaipur city के employees या 30000 से कम salary वाले employees को delete करेगी।

AND और OR को साथ में उपयोग करना

AND और OR operators को एक साथ use करते समय parentheses () का उपयोग करना जरूरी होता है।

SELECT * FROM employees
WHERE department = 'IT'
AND (city = 'Delhi' OR city = 'Mumbai');

Common Mistakes

  • AND और OR को बिना parentheses use करना
  • OR condition को overly broad बना देना
  • String values को quotes में न लिखना

Incorrect example:

SELECT * FROM employees
WHERE department = IT OR city = Delhi;

Correct example:

SELECT * FROM employees
WHERE department = 'IT' OR city = 'Delhi';

Summary

  • OR operator में किसी एक condition का true होना काफी होता है
  • Broad filtering के लिए OR useful है
  • AND के साथ use करते समय parentheses जरूरी हैं
  • SELECT, UPDATE और DELETE में support करता है
Share your love