Search code examples
sqlsql-server-2014

WHERE and WHERE NOT in SELECT


How would I use a WHERE and WHERE NOT in the same SELECT SQL query?

For example, I could have a table called fruits.

ID | Name       | Colour
-------------------------
1  | Strawberry | Red 
2  | Apple      | Red
3  | Grape      | Red
4  | Banana     | Yellow

From this table, I could execute a SELECT * FROM [fruits] WHERE Colour = 'Red', and it would retrieve the following:

ID | Name       | Colour
-------------------------
1  | Strawberry | Red 
2  | Apple      | Red
3  | Grape      | Red

However, I would like to exclude Apple from the single request above, I could use a WHERE NOT, but this would return all fruits, including Banana.

How would I write an SQL query so that it would result in the following, a specific colour being chosen, but excluding a specific fruit:

ID | Name       | Colour
-------------------------
1  | Strawberry | Red 
3  | Grape      | Red

I am using MsSQL 2014, any help would be appreciated.


Solution

  • You can have multiple clauses in your where:

    select * 
    from [fruits] 
    where Colour = 'Red'
      and Name <> 'Apple'