SQL Tutorial

SQL Introduction SQL Aggregate Functions SQL Aliases SQL And SQL Any All SQL Avg SQL Between SQL Case SQL Comments SQL Count SQL Delete SQL Distinct SQL Exists SQL Groupby SQL Having SQL In SQL Insert_into SQL Is Not Null SQL Join SQL Full Outer Join SQL Inner Join SQL Left Join SQL Right Join SQL Self Join SQL Like SQL Min Max SQL NOT Operator SQL Null SQL Operators SQL OR operator SQL OrderBy SQL Select SQL Select Into SQL Top Limit Fetch SQL Store Procedures SQL Sum SQL Union SQL Update SQL Where SQL Wildcards

SQL Database

SQL Alter Table SQL Auto increment SQL BackupDB SQL Check SQL Constrains SQL Create View SQL CreateDB SQL CreateTable SQL Data types SQL Dates SQL DefaultConstrain SQL DropDB SQL DropTable SQL Foreign Key SQL Hosting SQL Index SQL injections SQL Not NULL SQL PrimaryKey SQL Unique SQL Views

SQL NOT Operator Tutorial

Example Table

We will use the following table named employees for our examples:


    CREATE TABLE employees (
        employee_id INT PRIMARY KEY,
        employee_name VARCHAR(50),
        department VARCHAR(50)
    );

    INSERT INTO employees (employee_id, employee_name, department) VALUES
    (1, 'Alice Johnson', 'HR'),
    (2, 'Bob Smith', 'IT'),
    (3, 'Charlie Brown', 'Finance'),
    (4, 'Diana Prince', 'IT'),
    (5, 'Eve Davis', 'Marketing');
    

Employees Table

employee_id employee_name department
1 Alice Johnson HR
2 Bob Smith IT
3 Charlie Brown Finance
4 Diana Prince IT
5 Eve Davis Marketing

Using SQL NOT Operator

SQL NOT is used to negate a condition in a query.

Example: Find Employees Not in IT Department

To find employees who are not in the IT department, use the following query:


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

Result:

employee_name Alice Johnson
department HR
employee_name Charlie Brown
department Finance
employee_name Eve Davis
department Marketing

Example: Find Employees Not Named 'Bob Smith'

To find employees whose name is not 'Bob Smith', use the following query:


    SELECT employee_name, department
    FROM employees
    WHERE employee_name != 'Bob Smith';
    

Result:

employee_name Alice Johnson
department HR
employee_name Charlie Brown
department Finance
employee_name Diana Prince
department IT
employee_name Eve Davis
department Marketing