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 GROUP BY 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 ORDER BY SQL Select SQL Select Into SQL Top Limit Fetch SQL Stored Procedures SQL Sum SQL Union SQL Update SQL Where SQL Wildcards

SQL Database

SQL Alter Table SQL Auto increment SQL Backup Database SQL Check SQL Constraints SQL Create View SQL Create Database SQL Create Table SQL Data types SQL Dates SQL Default Constraint SQL Drop Database SQL Drop Table SQL Foreign Key SQL Hosting SQL Index SQL injections SQL Not NULL SQL PrimaryKey SQL Unique SQL Views

SQL Operators

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),
        salary DECIMAL(10, 2)
    );

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

Employees Table

employee_id employee_name department salary
1 Alice Johnson HR 60000.00
2 Bob Smith IT 75000.00
3 Charlie Brown Finance 50000.00
4 Diana Prince IT 80000.00
5 Eve Davis Marketing 55000.00

Using SQL Operators

SQL operators are used to perform operations on data in a database. Here are some common SQL operators:

Example: Using the Equality Operator (=)

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


    SELECT employee_name, department
    FROM employees
    WHERE department = 'IT';
    

Result:

employee_name Bob Smith
department IT
employee_name Diana Prince
department IT

Example: Using the Greater Than Operator (>)

To find employees with a salary greater than 60000, use the following query:


    SELECT employee_name, salary
    FROM employees
    WHERE salary > 60000;
    

Result:

employee_name Bob Smith
salary 75000.00
employee_name Diana Prince
salary 80000.00

Example: Using the Less Than Operator (<)

To find employees with a salary less than 60000, use the following query:


    SELECT employee_name, salary
    FROM employees
    WHERE salary < 60000;
    

Result:

employee_name Charlie Brown
salary 50000.00
employee_name Eve Davis
salary 55000.00