SQL - and and or Conjunctive Operators
SQL - and and or Conjunctive Operators
SQL - and and or Conjunctive Operators
T he SQL AND and O R operators are used to combine multiple conditions to narrow data in an SQL statement. T hese two operators are called conjunctive operators. T hese operators provide a means to make multiple comparisons with different operators in the same SQL statement.
Syntax:
T he basic syntax of AND operator with WHERE clause is as follows:
SELECT column1, column2, columnN FROM table_name WHERE [condition1] AND [condition2]...AND [conditionN];
You can combine N number of conditions using AND operator. For an action to be taken by the SQL statement, whether it be a transaction or query, all conditions separated by the AND must be T RUE.
Example:
Consider the CUST OMERS table having the following records:
+----+----------+-----+-----------+----------+ | ID | NAME | AGE | ADDRESS | SALARY | +----+----------+-----+-----------+----------+ | 1 | Ramesh | 32 | Ahmedabad | 2000.00 | | 2 | Khilan | 25 | Delhi | 1500.00 | | 3 | kaushik | 23 | Kota | 2000.00 | | 4 | Chaitali | 25 | Mumbai | 6500.00 | | 5 | Hardik | 27 | Bhopal | 8500.00 | | 6 | Komal | 22 | MP | 4500.00 | | 7 | Muffy | 24 | Indore | 10000.00 | +----+----------+-----+-----------+----------+
Following is an example, which would fetch ID, Name and Salary fields from the CUST OMERS table where salary is g reater than 2000 AND ag e is less tan 25 years:
SQL> SELECT ID, NAME, SALARY FROM CUSTOMERS WHERE SALARY > 2000 AND age < 25;
The OR Operator:
T he OR operator is used to combine multiple conditions in an SQL statement's WHERE clause.
Syntax:
You can combine N number of conditions using OR operator. For an action to be taken by the SQL statement, whether it be a transaction or query, only any ONE of the conditions separated by the OR must be T RUE.
Example:
Consider the CUST OMERS table having the following records:
+----+----------+-----+-----------+----------+ | ID | NAME | AGE | ADDRESS | SALARY | +----+----------+-----+-----------+----------+ | 1 | Ramesh | 32 | Ahmedabad | 2000.00 | | 2 | Khilan | 25 | Delhi | 1500.00 | | 3 | kaushik | 23 | Kota | 2000.00 | | 4 | Chaitali | 25 | Mumbai | 6500.00 | | 5 | Hardik | 27 | Bhopal | 8500.00 | | 6 | Komal | 22 | MP | 4500.00 | | 7 | Muffy | 24 | Indore | 10000.00 | +----+----------+-----+-----------+----------+
Following is an example, which would fetch ID, Name and Salary fields from the CUST OMERS table where salary is g reater than 2000 OR ag e is less tan 25 years:
SQL> SELECT ID, NAME, SALARY FROM CUSTOMERS WHERE SALARY > 2000 OR age < 25;