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

Arrastra en el orden correspondiente.


Arrastra las opciones:

SELECT
*
FROM
precio > 50
AND
stock > 0

Completa el código:

______
______
______
______
______
______

Interactive Test 2: Fill in the Blanks

Rellena los huecos en cada casilla.


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.

Datos de Ejemplo

idnameage
1Juan25
2Ana30
3Luis22
4Marta35

* Escribe tu consulta SQL y ejecuta para ver los resultados.

Resultados:

No hay resultados disponibles.



Which of the following is a logical operator in SQL?