Friday, August 9, 2024

SQL Operators

 Using operators

Arithmetic operators: 
  • + (Addition)
  • - (subtraction)
  • * (Multiplication)
  • / (Division)
  • % (Modulus) - it returns reminder.
Example:

SELECT 30 + 20;

Comparison Operators:
  • = (equal to)
  • != (Not equal to) or <>
  • > (Greater than)
  • < (Less than)
  • >= (Greater than or equal to)
  • <= (Less than or equal to)
Example:
SELECT * FROM Products
WHERE Price <> 18;

SELECT * FROM Products
WHERE Price >= 30;

Logical Operators:
  • AND (To check for both conditions to be true)
SELECT * FROM Customers
WHERE City = "London" AND Country = "UK";
  • OR (To check for one of the conditions to be true)
SELECT * FROM Customers
WHERE City = "London" OR Country = "UK";
  • NOT (To negate the given condition)
SELECT * FROM Customers
WHERE NOT Country = 'Spain';

SELECT * FROM Customers
WHERE CustomerName NOT LIKE 'A%';
  • IN (To specify multiple possible values for a column
SELECT * FROM Customers
WHERE Country IN ('Germany''France''UK');
  • BETWEEN (Between a certain range)
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;
  • ALL (TRUE if all of the subquery values meet the condition)
SELECT ProductName
FROM Products
WHERE ProductID = ALL
  (SELECT ProductID
  FROM OrderDetails
  WHERE Quantity = 10);
  • LIKE (Search for a pattern)
SELECT * FROM Customers
WHERE CustomerName LIKE 'a%';
  • ANY (TRUE if any of the subquery values meet the condition)
SELECT * FROM Products
WHERE Price > ANY (SELECT Price FROM Products WHERE Price > 50);
  • EXIST
SELECT SupplierName
FROM Suppliers
WHERE EXISTS (SELECT ProductName FROM Products WHERE Products.SupplierID = Suppliers.supplierID AND Price < 20);

  • SOME
SELECT * FROM Products
WHERE Price > SOME (SELECT Price FROM Products WHERE Price > 20);

Bitwise Operators:
  • & (Bitwise AND)
  • | (Bitwise OR)
  • ^ ( Bitwise exclusive or)

No comments:

Post a Comment

Featured Post

11g to 12c OSB projects migration points

1. Export 11g OSB code and import in 12c Jdeveloper. Steps to import OSB project in Jdeveloper:   File⇾Import⇾Service Bus Resources⇾ Se...