Mastering SQL Filtering and Logical Operators with electro4u

06 Jun 2023 Balmiki Mandal 0 SQL

SQL Filtering and Logical Operators

SQL filtering and logical operators allow you to select specific rows of data from a database based on certain criteria. Logical operators allow you to combine multiple conditions into a single expression, which can be used to create more complex filters.

Using AND, OR, and NOT operators

The three most common logical operators are AND, OR, and NOT. The AND operator returns a result of TRUE only if all of the conditions it combines are TRUE. The OR operator returns a result of TRUE if any of the conditions it combines are TRUE. The NOT operator reverses the result of a condition.

Example, the following SQL statement selects all customers from Germany who are over the age of 18:

SQL
SELECT * FROM customers WHERE country = 'Germany' AND age > 18;

This statement uses the AND operator to combine two conditions: country = 'Germany' and age > 18. Only customers who meet both of these conditions will be returned in the result set.

Following SQL statement selects all customers from Germany or the United States:

SQL
SELECT * FROM customers WHERE country = 'Germany' OR country = 'United States';

This statement uses the OR operator to combine two conditions: country = 'Germany' and country = 'United States'. Any customer who meets either of these conditions will be returned in the result set.

Following SQL statement selects all customers who are not from Germany:

SQL
SELECT * FROM customers WHERE country <> 'Germany';

This statement uses the NOT operator to reverse the condition country = 'Germany'. Only customers who do not meet this condition will be returned in the result set.

Combining conditions with parentheses

You can use parentheses to group conditions together and create more complex filters. For example, the following SQL statement selects all customers from Germany who are between the ages of 18 and 65:

SQL
SELECT * FROM customers WHERE country = 'Germany' AND (age >= 18 AND age <= 65);

The parentheses in this statement ensure that the two conditions age >= 18 and age <= 65 are evaluated as a single unit. This is necessary because the AND operator has a higher precedence than the comparison operators.

Using comparison operators (=, <, >, etc.)

Comparison operators are used to compare two values. The most common comparison operators are:

  • =: equal to
  • <>: not equal to
  • <: less than
  • <=: less than or equal to
  • >: greater than
  • >=: greater than or equal to

Example, the following SQL statement selects all customers who have placed an order in the last 30 days:

SQL
SELECT * FROM customers WHERE order_date >= CURRENT_DATE() - INTERVAL 30 DAY;

This statement uses the >= comparison operator to compare the order_date column to the current date minus 30 days. Only customers who have placed an order in the last 30 days will be returned in the result set.

You can also use comparison operators to compare values to strings.

Example, the following SQL statement selects all customers whose last name starts with the letter A:

SQL
SELECT * FROM customers WHERE last_name LIKE 'A%';

This statement uses the LIKE operator to compare the last_name column to the string A%. The % wildcard character at the end of the string means that any value that starts with the letter A will be returned in the result set.

Conclusion

SQL filtering and logical operators allow you to select specific rows of data from a database based on certain criteria. By using AND, OR, and NOT operators, you can combine multiple conditions into a single expression, which can be used to create more complex filters. You can also use parentheses to group conditions together and create even more complex filters.

 

Top Resources:

Enroll Now:

 

[ Course in production] "Start Supercharging Your Productivity!"

Contact Us:

 

  • For any inquiries, please email us at [[email protected]].
  • Follow us on insta  [ electro4u_offical_ ] for updates and tips.

 

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.