Coginiti menu Coginiti menu

SQL And, Or

Using the SQL AND + OR Logical Operators

AND Operator

The AND operator combines two conditions and returns TRUE if both conditions are TRUE. Otherwise, it returns FALSE.

SELECT *   
FROM products   
WHERE prices > 50 AND category = 'dresses';

In this example, the query retrieves all columns and rows from the products table where the prices are greater than 50, and the category is ‘dresses’. Only the products whose cost exceeds 50 and belong to the ‘dresses’ category will be in the result set.

In SQL, you can combine multiple operators to form complex conditions in a WHERE clause. Here are a few examples of combining operators in SQL:

SELECT column1, column2, ... 
FROM table_name 
WHERE (condition1 AND condition2) OR condition3;
SELECT column1, column2, ... 
FROM table_name 
WHERE column_name BETWEEN value1 AND value2 AND column_name2 = value;
SELECT column1, column2, ... 
FROM table_name 
WHERE NOT (condition1 OR condition2) AND condition3;
OR Operator

The SQL OR operator is used in a SELECT, INSERT, UPDATE, or DELETE statement to combine multiple conditions in a WHERE clause. It allows you to retrieve data that meets one or more conditions.

The OR operator combines two conditions and returns TRUE if at least one condition is TRUE.

SELECT *   
FROM products   
WHERE prices = 50 OR category = 'dresses';

In this example, the query retrieves all columns and rows from the products table where prices are equal to 50 or the category is dresses. As a result, you will see a mix of all the products that cost 50 and dresses at any price.

If you are looking for something in your database, you can combine the operators with the information you know and find what you need.

SELECT *   
FROM tv_show  
WHERE show_name LIKE 'The%' OR year_launch > 2000 OR rating < 8;