SQL AND and OR operators

In SQL, AND and OR are logical operators that allow you to combine or negate conditions in a SQL statement. They can be used in the WHERE clause to filter the data returned from the SELECT, UPDATE, DELETE, and INSERT INTO statements.

  • AND Operator

The AND operator allows you to select a record if both the first condition AND the second condition are true.

Syntax:

SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2;

Example:

SELECT * 
FROM Customers
WHERE Country='Germany' AND City='Berlin';

In the above example, only the customers who are from Berlin, Germany will be selected.

  • OR Operator

The OR operator allows you to select a record if either the first condition OR the second condition is true.

Syntax:

SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2;

Example:

SELECT * 
FROM Customers
WHERE Country='Germany' OR Country='Spain';

In this example, the customers who are from either Germany or Spain will be selected.

  • Combining AND & OR

You can also combine the AND and OR operators. When combining them, it's important to use parentheses to ensure the logic is being executed in the correct order.

Example:

SELECT * 
FROM Customers
WHERE Country='Germany' AND (City='Berlin' OR City='M��nchen');

In this example, customers who are from Germany and live either in Berlin or Munich will be selected. The parentheses ensure that the OR condition is evaluated before the AND condition.

Remember, the AND operator has a higher precedence in evaluation than the OR operator, so it is good practice to use parentheses for clarity and to avoid logical errors.

  1. Combining AND and OR operators in SQL: Combine multiple conditions using both AND and OR operators.

    SELECT * FROM TableName
    WHERE (Condition1 AND Condition2) OR Condition3;
    
  2. Multiple conditions with AND and OR in SQL: Specify multiple conditions in a WHERE clause.

    SELECT * FROM TableName
    WHERE Condition1 AND Condition2 OR Condition3;
    
  3. Order of precedence for AND and OR in SQL: Understand the order of precedence. AND has higher precedence than OR.

    SELECT * FROM TableName
    WHERE Condition1 AND Condition2 OR Condition3;
    
  4. SQL WHERE clause with AND and OR: Use the WHERE clause with both AND and OR for filtering rows.

    SELECT * FROM TableName
    WHERE (Condition1 AND Condition2) OR Condition3;
    
  5. Complex conditions with parentheses in SQL: Use parentheses to create complex conditions and control the logical order.

    SELECT * FROM TableName
    WHERE (Condition1 OR Condition2) AND (Condition3 AND Condition4);
    
  6. Using NOT with AND and OR in SQL: Use NOT to negate a condition in combination with AND and OR.

    SELECT * FROM TableName
    WHERE NOT (Condition1 AND Condition2) OR NOT Condition3;
    
  7. SQL AND and OR operators in JOIN conditions: Use AND and OR operators in JOIN conditions for more complex joins.

    SELECT *
    FROM Table1
    JOIN Table2 ON Table1.Column1 = Table2.Column1 AND Table1.Column2 = Table2.Column2
    WHERE Table1.Condition1 OR Table2.Condition2;