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
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
| id | name | age |
|---|---|---|
| 1 | Juan | 25 |
| 2 | Ana | 30 |
| 3 | Luis | 22 |
| 4 | Marta | 35 |
* Escribe tu consulta SQL y ejecuta para ver los resultados.
Resultados:
No hay resultados disponibles.