Geek Slack

Getting Started with SQL Server
    About Lesson


    SQL NOT Operator


    SQL NOT Operator

    The NOT operator is used to negate a condition in a SQL WHERE clause. It returns rows that do not meet the specified condition.

    Basic Syntax:

    SELECT column1, column2, ...
    FROM table_name
    WHERE NOT condition;

    Example 1: Using NOT with a Simple Condition

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

    This query selects all columns from the “employees” table where the “department” is not ‘Sales’.

    Example 2: Using NOT with Multiple Conditions

    SELECT *
    FROM customers
    WHERE NOT (country = 'USA' AND city = 'New York');

    This query selects all columns from the “customers” table where the “country” is not ‘USA’ and the “city” is not ‘New York’.

    Example 3: Combining NOT with Other Operators

    SELECT *
    FROM products
    WHERE NOT (category = 'Electronics' OR price >= 300);

    This query selects all columns from the “products” table where the “category” is not ‘Electronics’ and the “price” is not greater than or equal to 300.

    The NOT operator is useful for querying data where you need to exclude specific conditions. It allows for precise control over the query results by negating the specified criteria.