SQL Tutorial

SQL Clauses / Operators

SQL-Injection

SQL Functions

SQL Queries

PL/SQL

MySQL

SQL Server

Misc

SQL | AND and OR operators

In SQL, the AND and OR operators are used to filter records based on more than one condition:

  1. AND Operator: It allows you to filter records based on multiple conditions. All of the conditions must be true for a record to be selected.

  2. OR Operator: It allows you to filter records based on multiple conditions too. However, only one of the conditions must be true for a record to be selected.

AND Operator:

Syntax:

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

Example: Suppose you have a students table and you want to select students who are in grade 10 and whose age is 15:

SELECT * 
FROM students
WHERE grade = 10 AND age = 15;

OR Operator:

Syntax:

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

Example: Using the same students table, let's say you want to select students who are either in grade 10 or grade 11:

SELECT * 
FROM students
WHERE grade = 10 OR grade = 11;

Combining AND and OR:

You can use parentheses to group conditions when you mix AND and OR to ensure the correct logic is applied.

Example: Suppose you want to select students who are either in grade 10 and age 15, or in grade 11 and age 16:

SELECT * 
FROM students
WHERE (grade = 10 AND age = 15) OR (grade = 11 AND age = 16);

Points to Note:

  • When combining AND and OR, it's important to use parentheses appropriately to avoid logic errors. Without parentheses, conditions are evaluated left to right by default, which may not always yield the desired results.

  • Always test your queries, especially when using multiple conditions, to ensure they are retrieving the expected results.

  • Understanding the precedence (AND is evaluated before OR when no parentheses are used) and how to correctly group conditions is crucial for writing correct and efficient SQL queries.

  1. How to use AND and OR in SQL queries:

    • Use AND and OR to combine conditions.
    SELECT column1, column2
    FROM your_table
    WHERE condition1 AND condition2
       OR condition3;
    
  2. SQL WHERE clause with AND and OR:

    • Apply AND and OR in the WHERE clause.
    SELECT column1, column2
    FROM your_table
    WHERE condition1 AND condition2
       OR condition3;
    
  3. Combining multiple conditions with AND in SQL:

    • Combine multiple conditions using AND.
    SELECT column1, column2
    FROM your_table
    WHERE condition1 AND condition2;
    
  4. Using OR operator for conditional statements in SQL:

    • Use OR for conditional statements.
    SELECT column1, column2
    FROM your_table
    WHERE condition1 OR condition2;
    
  5. SQL AND and OR precedence rules:

    • Understand the precedence rules of AND and OR.
    SELECT column1, column2
    FROM your_table
    WHERE condition1 AND condition2
       OR condition3;
    
  6. Nested conditions with AND and OR in SQL:

    • Nest conditions for complex queries.
    SELECT column1, column2
    FROM your_table
    WHERE (condition1 AND condition2)
       OR (condition3 AND condition4);
    
  7. SQL AND and OR operators in SELECT statement:

    • Use AND and OR in the SELECT statement.
    SELECT column1,
           CASE WHEN condition1 AND condition2 THEN 'True' ELSE 'False' END AS result
    FROM your_table;
    
  8. Multiple conditions with AND and OR in WHERE clause:

    • Combine multiple conditions in the WHERE clause.
    SELECT column1, column2
    FROM your_table
    WHERE condition1 AND condition2
       OR condition3;
    
  9. SQL AND and OR operators with parentheses:

    • Use parentheses to control the logical order.
    SELECT column1, column2
    FROM your_table
    WHERE (condition1 AND condition2)
       OR condition3;
    
  10. Chaining multiple conditions in SQL queries:

    • Chain multiple conditions for complex filtering.
    SELECT column1, column2
    FROM your_table
    WHERE condition1
      AND condition2
      OR (condition3 AND condition4);
    
  11. Advanced filtering with AND and OR in SQL:

    • Apply advanced filtering using AND and OR.
    SELECT column1, column2
    FROM your_table
    WHERE (condition1 OR condition2)
      AND (condition3 OR condition4);
    
  12. Common mistakes with AND and OR in SQL queries:

    • Be cautious of common mistakes in AND and OR usage.
    -- Incorrect: Misuse of parentheses
    SELECT column1, column2
    FROM your_table
    WHERE condition1 AND (condition2 OR condition3);