SQL

[Basic Grammar] SQL NOT Operator

grace21110 2023. 10. 13. 18:24
반응형

The NOT operator is used to give the opposite result (negative result).

 

Example 

Select only the guests that are NOT from South Korea: 

SELECT * FROM Students 
WHERE NOT Country = 'South Korea';
  • Syntax 
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;

 

  • NOT LIKE 

Example 

Select brands that does not start with the letter 'B':

SELECT * FROM Brands
WHERE BrandName NOT LIKE 'B%';

 

  • NOT BETWEEN 

Example 

Select the guests with a guestID not between 1000 and 2000:

SELECT * FROM Guests
WHERE GuestID NOT BETWEEN 1000 and 2000;

 

  • NOT IN 

Example

Select customers that are not from Japan or China: 

SELECT * FROM Customers 
WHERE City NOT IN ('Japan', 'China');

 

  • NOT Greater Than 

Example 

Select customers with a CustomerID not greater than 50: 

SELECT * FROM Customers
WHERE NOT CustomerID > 50;

 

 

  • NOT LESS THAN

Example

Select guests with a GuestID not less than 50:

SELECT * FROM Guests
WHERE NOT GuestId < 50;

 

Exercises

1. Select all customers whose City is NOT "London."

SELECT * FROM Customers
WHERE City NOT 'London';

2. Retrieve all product brands that do not start with the letter 'B.'

SELECT BrandName FROM Products 
WHERE BrandName NOT LIKE 'B%';