SQL Tutorial

SQL Clauses / Operators

SQL-Injection

SQL Functions

SQL Queries

PL/SQL

MySQL

SQL Server

Misc

SQL | NOT Operator

The NOT operator in SQL is used to negate a condition, essentially reversing its result. It's employed in various clauses and scenarios to filter the data contrary to a given condition.

Usage:

  1. NOT in WHERE clause:

    You can use the NOT operator in the WHERE clause to exclude rows that match a particular condition.

    SELECT * FROM employees WHERE NOT department = 'IT';
    

    This query selects all employees who do not work in the 'IT' department.

  2. NOT LIKE:

    This combination is used to exclude rows that match a particular pattern.

    SELECT * FROM employees WHERE name NOT LIKE 'A%';
    

    This retrieves all employees whose names don't start with the letter 'A'.

  3. NOT IN:

    Used to filter out rows that match any value in a list.

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

    This fetches employees who aren't in the 'IT' or 'HR' departments.

  4. NOT EXISTS:

    Used in conjunction with a subquery to filter out rows that would result in rows from the subquery.

    SELECT * FROM employees e
    WHERE NOT EXISTS (
        SELECT 1 FROM department_leads d
        WHERE d.employee_id = e.id
    );
    

    This example would fetch all employees who aren't department leads.

  5. NOT BETWEEN:

    Retrieves rows outside of a specified range.

    SELECT * FROM products WHERE price NOT BETWEEN 10 AND 50;
    

    This gets products that are either priced below $10 or above $50.

  6. Combining NOT with AND & OR:

    NOT can be combined with AND and OR logical operators.

    SELECT * FROM employees 
    WHERE NOT (department = 'IT' OR salary > 60000);
    

    This query fetches employees who are neither in the 'IT' department nor have salaries greater than 60,000.

Note:

While using the NOT operator, it's essential to ensure clarity in the conditions, especially when multiple conditions are combined. It might be beneficial in complex scenarios to use parentheses () to group conditions clearly and ensure the intended logical evaluation order.

  1. How to use NOT in SQL:

    • Description: The NOT operator in SQL is used to negate a condition, essentially reversing its logic.
    • Example Code:
      SELECT * FROM employees
      WHERE NOT department_id = 2;
      
  2. Negating conditions with NOT in WHERE clause:

    • Description: Use NOT in the WHERE clause to negate a condition, excluding rows that satisfy the specified condition.
    • Example Code:
      SELECT * FROM orders
      WHERE NOT order_status = 'Shipped';
      
  3. Using NOT with AND and OR operators in SQL:

    • Description: NOT can be combined with AND and OR operators to create complex conditions.
    • Example Code:
      SELECT * FROM products
      WHERE category = 'Electronics' AND NOT (price > 1000 OR stock_quantity < 10);
      
  4. Dealing with NULL values using NOT in SQL:

    • Description: Use NOT to filter out rows where a column is NULL or is not NULL.
    • Example Code:
      SELECT * FROM customers
      WHERE NOT email IS NULL;
      
  5. NOT LIKE and NOT IN operators in SQL:

    • Description: Use NOT LIKE to exclude rows based on a pattern and NOT IN to exclude rows where a column value matches any value in a specified list.
    • Example Code:
      SELECT * FROM products
      WHERE NOT product_name LIKE 'Old%'
      AND NOT category_id IN (1, 3, 5);
      
  6. NOT EXISTS and NOT BETWEEN in SQL:

    • Description: Use NOT EXISTS to exclude rows based on the non-existence of a subquery result, and NOT BETWEEN to exclude rows within a specified range.
    • Example Code:
      SELECT * FROM employees e
      WHERE NOT EXISTS (
          SELECT 1 FROM salaries s
          WHERE e.employee_id = s.employee_id AND s.salary > 50000
      );
      
      SELECT * FROM orders
      WHERE NOT order_date BETWEEN '2022-01-01' AND '2022-12-31';
      
  7. Combining NOT with other comparison operators:

    • Description: Combine NOT with other comparison operators (e.g., >, <, =) to create negated conditions.
    • Example Code:
      SELECT * FROM products
      WHERE NOT unit_price > 100;
      
  8. Using NOT with subqueries in SQL:

    • Description: NOT can be used with subqueries to exclude rows based on the non-existence or non-matching values in a subquery.
    • Example Code:
      SELECT * FROM employees
      WHERE NOT EXISTS (
          SELECT 1 FROM terminated_employees
          WHERE employees.employee_id = terminated_employees.employee_id
      );
      
  9. Alternatives to NOT for condition negation in SQL:

    • Description: Depending on the scenario, alternatives to NOT include using negative conditions, negating with other operators, or restructuring the query logic.
    • Example Code:
      -- Using negative conditions
      SELECT * FROM products
      WHERE category <> 'Electronics';
      
      -- Restructuring query logic
      SELECT * FROM orders
      WHERE order_status = 'Pending' OR order_status = 'Processing';