Operators in SQL

 Operators in SQL

SQL operators are special symbols or keywords used to specify conditions in SQL statements. They help filter, compare, and perform calculations on data in your database queries.

Types of operators:

1. Arithmetic Operators: Addition(+), subtraction(-), multiplication(*), division(/) , modulus(%)

Used to perform mathematical calculations.

  • + Addition
    SELECT 2 + 3 AS Result; (Result: 5)
  • - Subtraction
    SELECT 5 - 2 AS Result; (Result: 3)
  • * Multiplication
    SELECT 4 * 2 AS Result; (Result: 8)
  • / Division
    SELECT 10 / 2 AS Result; (Result: 5)
  • % Modulus (Remainder)
    SELECT 10 % 3 AS Result; (Result: 1)
2. Comparison Operator: equla to(=), not equal to (!=), gt(>) , lt(<), gteq(>=) gtlt(<)

Used to compare values in SQL statements.

  • = Equal to
    SELECT * FROM employee WHERE salary = 50000;
  • <> or != Not equal to
    SELECT * FROM employee WHERE name <> 'Rahul';
  • > Greater than
    SELECT * FROM employee WHERE age > 25;
  • < Less than
    SELECT * FROM employee WHERE salary < 30000;
  • >= Greater than or equal to
    SELECT * FROM employee WHERE age >= 30;
  • <= Less than or equal to
    SELECT * FROM employee WHERE age <= 22;
3. Logical Operators: 

Used to combine multiple conditions.

  • AND
    SELECT * FROM employee WHERE age > 25 AND salary < 50000;
  • OR
    SELECT * FROM employee WHERE name = 'Rahul' OR name = 'Vivek';
  • NOT
    SELECT * FROM employee WHERE NOT age = 30;

4. Special Operators

BETWEEN

Checks if a value falls within a specified range (inclusive).
SELECT * FROM employee WHERE salary BETWEEN 25000 AND 50000;

IN

Checks if a value matches any value in a list.
SELECT * FROM employee WHERE name IN ('Rahul', 'Vivek', 'Ram');

LIKE & Wildcard 

Searches for a pattern in a column.
SELECT * FROM employee WHERE name LIKE 'R%'; (names starting with "R")

IS NULL / IS NOT NULL

Checks for null (missing) values.
SELECT * FROM employee WHERE dob IS NULL;
SELECT * FROM employee WHERE dob IS NOT NULL;




















Comments

Popular posts from this blog

Introduction to DBMS

ACID Properties

Understanding First Normal Form (1NF) in Database Design