DEV Community

Cover image for Filtering Data in SQL: Using WHERE, AND, OR, and NOT
Bellamer
Bellamer

Posted on

Filtering Data in SQL: Using WHERE, AND, OR, and NOT

Filtering Data with WHERE

The WHERE clause filters rows that meet specific conditions.

Example:

SELECT * FROM users WHERE Age > 25;
Enter fullscreen mode Exit fullscreen mode

Combining Filters with AND, OR, and NOT

  • AND: Filters rows that meet all conditions.
SELECT * FROM users WHERE Age > 25 AND City = 'New York';
Enter fullscreen mode Exit fullscreen mode
  • OR: Filters rows that meet at least one condition.
SELECT * FROM users WHERE Age > 25 OR City = 'New York';
Enter fullscreen mode Exit fullscreen mode
  • NOT: Excludes rows that meet the condition.
SELECT * FROM users WHERE NOT Age < 25;
Enter fullscreen mode Exit fullscreen mode

Challenge: Write Complex Filters

Retrieve all orders placed in 2023 by users from New York or San Francisco.

Top comments (0)