SQLforGeeks
  • Home
  • SQL
  • SQL Tutorial
  • SQL Syntax
  • Our Services
Contact
  1. SQL
  2. SQL Logical operators

Text copied!

« Previous
Next »

SQL Logical operators

May 21, 2023, 2:14 p.m. under SQL

  • Hi! You should check SQL comparison operators post first.

    In SQL, Operators are special character, symbol or a keyword that is used to perform some specific operations. For example : comparing data

    SQL operators are commonly used with SQL statements to retrieve, filter, and manipulate data.

    SQL logical operators can be used to combine multiple conditions in 'WHERE' clause to retrieve specific data.

    Overview of SQL logical operators :
    • AND 
    • OR 
    • NOT
            

    Let's assume we have a table named "[Employees]".

    SQL Logical operators
    1. AND :

    It is a operator that returns 'TRUE' if both or all the specified conditions are true in 'WHERE' clause.

    Write a SQL statement to retrieve employees data from [Employees] table where Gender is 'Female' and Salary is greater than 5000.

    SELECT * 
    FROM [Employees]
    WHERE Gender = 'Female' AND Salary > 5000;
            
    SQL Logical operators

    By using 'AND' operator, above statement returned zero rows because both/all conditions in 'WHERE' clause were not satisfied, only the first condition was satisfied. The [Employees] table doesn't contain any row where employee's gender is 'Female' and as well as their salary is greater than 5000.

    2. OR :

    It is a operator that returns 'TRUE' if any or at least one condition is true in 'WHERE' clause.

    Write a SQL statement to retrieve employees data from [Employees] table where Gender is 'Female' or Salary is greater than 5000.

    SELECT * 
    FROM [Employees]
    WHERE Gender = 'Female' OR Salary > 5000;
            
    SQL Logical operators

    By using 'OR' operator, above statement returned two rows because the first condition in 'WHERE' clause were satisfied. The [Employees] table contains 2 rows where employee's gender is 'Female' and it doesn't contain any row where salary is greater than 5000.

    3. NOT :

    It is a operator that reverses or negates the conditions in 'WHERE' clause.

    Write a SQL statement to retrieve employees data from [Employees] table where Gender is not 'Male'.

    SELECT * 
    FROM [Employees]
    WHERE NOT Gender = 'Male';
            
    SQL Logical operators

    By using 'NOT' operator, above statement returned two rows because the condition in 'WHERE' clause were reversed. The [Employees] table contains 2 rows where employee's gender is not 'Male'.

    To enhance your understanding of 'SQL operators', be sure to check out our next tutorial.

    Frequently Asked Questions :

    What is logical operators in SQL?

    Logical operators in SQL are used to combine or manipulate conditions in WHERE clauses, such as AND, OR, and NOT.

    What is SQL logic?

    SQL logic refers to the principles and rules governing the manipulation and retrieval of data using SQL commands and statements.

    What is the unique logical operator in SQL?

    The unique logical operator in SQL is XOR (Exclusive OR), which returns true if only one of the operands is true.

    What are logical operators?

    Logical operators in SQL are symbols or keywords used to perform logical operations like AND, OR, NOT, XOR, etc., to filter data based on specified conditions.
    Thank You! You should check UNION operator post next.
    « Previous
    Next »
    RELATED :

    What is SQL

    What is T-SQL

    Difference between DBMS and Data Warehouse

    Download SQL Server

    Install SQL Server

    Download SQL Server Management Studio SSMS

    SQL Server Management Studio

    SQL Database

    Download database

    Restore database

    Backup database

    Attach database

    Detach database

    Create database

    Delete database

    Rename database

    Select database

    Database offline

    Database online

    SQL Commands

    SQL Tables

    Create table

    Truncate table

    Delete table

    Rename table

    Select table

    Alter table

    SQL Data Types

    SQL Comments

    SQL Constraints

    SQL Joins

    SQL inner join

    SQL left join

    SQL right join

    SQL full join

    SQL cross join

    SQL self join

    INSERT INTO SELECT statement

    INSERT INTO statement

    SQL Clauses

    SELECT clause

    FROM clause

    WHERE clause

    GROUP BY clause

    HAVING clause

    ORDER BY clause

    JOIN clause

    UNION clause

    UNION ALL clause

    TOP clause

    DISTINCT clause

    SQL Operators

    SQL Arithmetic operators

    SQL Comparison operators

    UNION operator

    UNION ALL operator

    INTERSECT operator

    EXCEPT operator

    LIKE operator

    NOT LIKE operator

    IN operator

    NOT IN operator

    IS NULL operator

    IS NOT NULL operator

    EXISTS operator

    NOT EXISTS operator

    BETWEEN operator

    NOT BETWEEN operator

    SQL Functions

    SQL Built-In functions

    CHARINDEX function

    DATEADD function

    CONCAT function

    LEN function

    REPLACE function

    SUBSTRING function

    CASE statement

    GETDATE function

    DATEPART function

    DATEDIFF function

    CAST function

    TRY_CAST function

    CONVERT function

    TRY_CONVERT function

    ISNULL function

    NULLIF function

    COALESCE function

    SQL Window functions

    ROW_NUMBER function

    RANK function

    DENSE_RANK function

    IIF function

    CHOOSE function

    SQL Store Procedure

    Store Procedure vs. Function

    SQL Subquery

    SQL Aliases

    Temp table

    SQL Error Handling

    SQL Variables

    SQL Views

    SQL Merge

    SQL CTE

    SQL WITH TIES

    Define Transaction in DBMS

    ACID properties in DBMS

    Types of Triggers in DBMS


    • Have Some Questions?
    logo

    Elevate your data experience with SQL excellence

    Quick Links
    •  Home
    •  SQL Tutorial
    •  SQL Syntax
    •  Our Services
    Our Services
    • Web Development
    • BI Development
    • Data Warehousing
    • Data Integration ETL


    Follow Us

    GST Registered: XXAXXXXXXXZX
    Mumbai, Maharashtra, India support@sqlforgeeks.com

    © 2025 Copyright™ | All Rights Reserved | Privacy Policy