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
for data professionals
A WHERE clause with AND:
- SELECT column-names
- FROM table-name
- WHERE condition1 AND condition2
A WHERE clause with OR:
- UPDATE table-name
- SET column-name = value
- WHERE condition1 OR condition2
A WHERE clause with NOT:
- DELETE table-name
- WHERE NOT condition
SQL WHERE with AND, OR, and NOT Examples
Problem: Get customer named Thomas Hardy
- SELECT Id, FirstName, LastName, City, Country
- FROM Customer
- WHERE FirstName = 'Thomas' AND LastName = 'Hardy'
Problem: List all customers from Spain or France
- SELECT Id, FirstName, LastName, City, Country
- FROM Customer
- WHERE Country = 'Spain' OR Country = 'France'
Problem: List all customers that are not from the USA
- SELECT Id, FirstName, LastName, City, Country
- FROM Customer
- WHERE NOT Country = 'USA'
Problem: List all orders that not between $50 and $15000
- SELECT Id, OrderDate, CustomerId, TotalAmount
- FROM [Order]
- WHERE NOT (TotalAmount >= 50 AND TotalAmount <= 15000)
- ORDER BY TotalAmount DESC
No comments:
Post a Comment