This is a premium alert message you can set from Layout! Get Now!

Using Comparison Operators and Logical Operators in SQL

Using Comparison Operators and Logical Operators in SQL

When working with data in SQL, it’s important to be able to filter, compare, and combine conditions effectively. This chapter focuses on comparison operators and logical operators —two fundamental tools that give you powerful control over your queries.

🎯 Why Learn This?

These operators are the foundation of filtering and conditionally retrieving data. Mastering them means you can:

  • Fetch only the records you need.
  • Combine multiple conditions.
  • Create smart, dynamic queries for real-world applications.

We'llnot cover sorting ( ORDER BY ) or limiting results ( LIMIT ) here—those are in upcoming chapters.

πŸ”„ Our Example Table: students

Same table, consistent examples:

CREATE TABLE students ( 
id INT , 
name VARCHAR ( 50 ), 
age INT , 
course VARCHAR ( 30 ),
marks DECIMAL ( 5 , 2 ) );  

Sample data:

id name age course marks
1 Rahul Sharma 21 SQL 85.50
2 Anjali Mehta 22 SQL 91.75
3 Vikas Gupta 23 SQL 88.00
4 Riya Jain 20 Python 92.50
5 Aman Verma 24 SQL 76.00

πŸ”Ž Comparison Operators

These operators compare values in your WHERE clause.

Operator Meaning Example
= Equal to marks = 91.75
<>or != Not equal to course != 'SQL'
> Greater than age >22
< Less than marks< 80
>= Greater than or equal to marks >= 85
<= Less than or equal to age<= 21

πŸ”Ή Example Query

SELECT * FROM students WHERE marks >= 85 ;  

Returns students with marks 85 or higher .

πŸ”— Logical Operators

These operators combine multiple conditions .

1. AND

Returns true only if all conditions are true .

SELECT * FROM students WHERE course = 'SQL' AND age < 23 ;  

Only those in the SQL course and younger than 23.

2. OR

Returns true if at least one condition is true .

SELECT * FROM students WHERE age < 21 OR marks > 90 ;  

This returns students who are either younger than 21 OR scored more than 90 .

3. NOT

Negates the condition.

SELECT * FROM students WHERE NOT course = 'SQL' ;  

Returns students who are not in the SQL course .

4. Grouping Conditions with ()

Use parentheses to group logical operators and control the logic flow .

 SELECT * FROM students WHERE (course = 'SQL' AND marks > 85 ) OR age < 21 ;  
  • This query will return:
  • Students in SQL with marks >85 , OR
  • Any student younger than 21

πŸ§ͺ Practical Examples

  1. Find students who are older than 22 and not in the SQL course:
SELECT * FROM students WHERE age > 22 AND course != 'SQL' ;  
  1. Get students who are in the SQL course or have marks above 90:
SELECT * FROM students WHERE course = 'SQL' OR marks > 90 ;  
  1. List students who are either under 21 or scored less than 80:
SELECT * FROM students WHERE age < 21 OR marks < 80 ;  

⚠️ Common Mistakes to Avoid

Mistake Correction
Mixing AND / OR without () Use brackets to avoid confusion
Using = instead of IS for NULL Use IS NULL or IS NOT NULL
Writing "SQL" instead of 'SQL' for text Use single quotes for string values
Typing AND OR without clear structure Always group complex conditions clearly

πŸ“Œ Summary

  • Comparison operators help compare values (like = , > ,<>, etc.).
  • Logical operators help build complex queries using AND , OR , and NOT .
  • Useparentheses ()to control logical flow in multi-condition queries.
Tags

Post a Comment

0 Comments
Post a Comment
To Top