SQL OR operator in DB2 for i SQL

SQL OR operator in DB2 for i SQL
SQL OR operator in DB2 for i SQL, SQL OR, SQL Tutorial, SQL, DB2 for i SQL, IBMi DB2

The OR operator is used with SQL WHERE clause. The OR operator is used to filter data based on one or more condition.

  • The OR operator displays a record if any of the conditions(at least one condition) are TRUE in the SQL WHERE clause used in the SQL Query.
  • Syntax using OR operator

    SELECT column1, column2, ...
    FROM table_name
    WHERE condition1 OR condition2 ...

    Example using SQL AND operator

    Find out all the customers of BIHAR state and also from HARYANA from the Cusotmer Table

    SELECT * FROM Customer WHERE CUSTSTATE = 'BIHAR' OR CUSTSTATE = 'HARYANA'
    CUSTID   CUSTNAME              CUSTCITY              CUSTSTATE             CUSTCOUNTRY 
         1   Amit                  PATNA                 BIHAR                 INDIA       
         2   Anil                  PATNA                 BIHAR                 INDIA       
         3   Dhanraj               HISAR                 HARYANA               INDIA       

    Combining AND/OR operator

    We can use both AND and OR operator in the single WHERE clause.

    Find out all the customers of Country INDIA and City either CHENNAI or HISAR from the Cusotmer Table

    SELECT * FROM Customer WHERE CUSTCOUNTRY ='INDIA' and ( CUSTCITY = 
    'CHENNAI' or CUSTCITY = 'HISAR')                                   
    CUSTID   CUSTNAME              CUSTCITY              CUSTSTATE             CUSTCOUNTRY
         3   Dhanraj               HISAR                 HARYANA               INDIA      
         8   Annamalai             CHENNAI               TAMIL NADU            INDIA      
         9   Kannan                CHENNAI               TAMIL NADU            INDIA      

    Post a Comment

    © AS400 and SQL Tricks. All rights reserved. Developed by Jago Desain