Using Logical Operators (AND, OR, NOT)


  Logical operators in SQL allow you to combine conditions and filter results more precisely. They are useful for creating complex queries.


Syntax


The basic syntax for using logical operators in SQL is:
SELECT * FROM [table] WHERE [condition1] [AND/OR] [condition2];
You can also use NOT to negate a condition.


Purpose


Logical operators help combine multiple conditions in an SQL query. For example:


  • Select products with a price greater than 50 and in stock.
  • Find users who are younger than 30 or older than 60.
  • Filter records where a value is not null.


Exercises



Interactive Test 1: Drag and Drop

Drag in the corresponding order.


Drag the options:

SELECT
*
FROM
precio > 50
AND
stock > 0

Completa el código:

______
______
______
______
______
______

Interactive Test 2: Fill in the Blanks

Fill the Blanks.


SELECT * FROM products WHERE price  stock ;

Practical Exercise:

Statement:

  Write an SQL query to select all products with a price greater than 50 and that are in stock.

Example Data

idnameage
1John25
2Anna30
3Louis22
4Martha35

* Write your SQL query and execute to see results.

Results:

No results available.



Which of the following is a logical operator in SQL?