Task 6 – SQL AND, OR and NOT Operators

Actuary Forums Forums Learnings Softwares SQL Course Task 6 – SQL AND, OR and NOT Operators

  • This topic is empty.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • #22717
    Mayank Goyal
    Keymaster

      The SQL AND, OR and NOT OperatorsThe WHERE clause can be combined with AND, OR, and NOT operators.

       The AND and OR operators are used to filter records based on more than one condition:

       

    • The AND operator displays a record if all the conditions separated by AND is TRUE.

    • The OR operator displays a record if any of the conditions separated by OR is TRUE.
    • The NOT operator displays a record if the condition(s) is NOT TRUE.AND Syntax

      Code:

      SELECT [i]column1[/i],[i] column2, …[/i]
      FROM [i]table_name[/i]
      WHERE [i]condition1[/i] AND [i]condition2[/i] AND [i]condition3 …[/i];


      OR Syntax

      Code:

      SELECT [i]column1[/i],[i] column2, …[/i]
      FROM [i]table_name[/i]
      WHERE [i]condition1[/i] OR [i]condition2[/i] OR [i]condition3 …[/i];


      NOT Syntax

      Code:

      SELECT [i]column1[/i],[i] column2, …[/i]
      FROM [i]table_name[/i]
      WHERE NOT [i]condition[/i];


      AND ExampleThe following SQL statement selects all fields from “Customers” where country is “Germany” AND city is “Berlin”:

       Example

      Code:

      SELECT * FROM Customers
      WHERE Country=’Germany’ AND City=’Berlin’;


      Try it Yourself »


      [hr]
      OR ExampleThe following SQL statement selects all fields from “Customers” where city is “Berlin” OR “München”:Example

      Code:

      SELECT * FROM Customers
      WHERE City=’Berlin’ OR City=’München’;


      Try it Yourself »


      [hr]
      NOT ExampleThe following SQL statement selects all fields from “Customers” where country is NOT “Germany”:

       Example

      Code:

      SELECT * FROM Customers
      WHERE NOT Country=’Germany’;


      Try it Yourself »


      [hr]
      Combining AND, OR and NOTYou can also combine the AND, OR and NOT operators.

      The following SQL statement selects all fields from “Customers” where country is “Germany” AND city must be “Berlin” OR “München” (use parenthesis to form complex expressions):Example

      Code:

      SELECT * FROM Customers
      WHERE Country=’Germany’ AND (City=’Berlin’ OR City=’München’);


      Try it Yourself »

       The following SQL statement selects all fields from “Customers” where country is NOT “Germany” and NOT “USA”:Example

      Code:

      SELECT * FROM Customers
      WHERE NOT Country=’Germany’ AND NOT Country=’USA’;


      Try it Yourself »

    Viewing 1 post (of 1 total)
    • You must be logged in to reply to this topic.

    Actuary Forums Forums Learnings Softwares SQL Course Task 6 – SQL AND, OR and NOT Operators