Tuesday, September 18, 2018

SQL WHERE AND, OR, NOT Clause

SQL WHERE AND, OR, NOT Clause

  • WHERE conditions can be combined with AND, OR, and NOT.
  • A WHERE clause with AND requires that two conditions are true.
  • A WHERE clause with OR requires that one of two conditions is true.
  • A WHERE clause with NOT negates the specified condition.
The definitive guide
for data professionals 










A WHERE clause with AND:
  1. SELECT column-names
  2. FROM table-name
  3. WHERE condition1 AND condition2

A WHERE clause with OR:
  1. UPDATE table-name
  2. SET column-name = value
  3. WHERE condition1 OR condition2

A WHERE clause with NOT:
  1. DELETE table-name
  2. WHERE NOT condition




SQL WHERE with AND, OR, and NOT Examples



Problem: Get customer named Thomas Hardy

  1. SELECT Id, FirstName, LastName, City, Country
  2. FROM Customer
  3. WHERE FirstName = 'Thomas' AND LastName = 'Hardy'




Problem: List all customers from Spain or France
  1. SELECT Id, FirstName, LastName, City, Country
  2. FROM Customer
  3. WHERE Country = 'Spain' OR Country = 'France'




Problem: List all customers that are not from the USA
  1. SELECT Id, FirstName, LastName, City, Country
  2. FROM Customer
  3. WHERE NOT Country = 'USA'




Problem: List all orders that not between $50 and $15000
  1. SELECT Id, OrderDate, CustomerId, TotalAmount
  2. FROM [Order]
  3. WHERE NOT (TotalAmount >= 50 AND TotalAmount <= 15000)
  4. ORDER BY TotalAmount DESC

No comments:

Post a Comment

SQL Important Queries

  How to delete rows with no where clause The following example deletes  all rows  from the  Person.Person  the table in the AdventureWork...