SQL Operators

SQL operators are used to perform operations on data in a SQL database. They are used in the WHERE and HAVING clauses of SQL statements to filter the data that is returned. Here are some common SQL operators and examples of how they can be used:

  • Comparison operators:
    • = : equal to
    • <> or != : not equal to
    • : greater than
    • < : less than
    • = : greater than or equal to
    • <= : less than or equal to
SELECT * FROM products WHERE price > 10;
SELECT * FROM customers WHERE age >= 18;
  • Logical operators:
    • AND : returns true if both conditions are true
    • OR : returns true if at least one condition is true
    • NOT : negates the value of a condition
SELECT * FROM products WHERE price > 10 AND quantity < 50;
SELECT * FROM customers WHERE age >= 18 OR age < 30;
SELECT * FROM products WHERE NOT active = true;
  • BETWEEN … AND …: used to filter data within a range of values.
SELECT * FROM products WHERE price BETWEEN 10 AND 20;
SELECT * FROM orders WHERE order_date BETWEEN '2022-01-01' AND '2022-12-31';
  • IN(…) : used to filter data that matches any value in a list Examples
SELECT * FROM products WHERE category IN ('Electronics', 'India');
SELECT * FROM customers WHERE country IN ('US', 'Canada', 'Mexico');
  • LIKE : used to filter data that matches a specified pattern. Wildcard characters such as % and _ can be used in the pattern.
SELECT * FROM products WHERE name LIKE '%phone%';
SELECT * FROM customers WHERE last_name LIKE 'S%';
  • IS NULL: used to filter data that contains null values Examples:
SELECT * FROM products WHERE image IS NULL;
SELECT * FROM customers WHERE middle_name IS NULL;

These are just a few examples of the many SQL operators that are available. The specific operators and syntax may vary depending on the SQL database management system (DBMS) you are using.