Loading
SQL Comparison Operator-interview

Q1. What is the purpose of comparison operators in SQL?

Comparison operators are used to compare one expression with another and return TRUE, FALSE, or UNKNOWN (if NULLs are involved). They are most often used in filtering conditions (WHERE and HAVING) to select specific data sets.






Q2. Explain the difference between != and <> operators in SQL.

Both operators represent “not equal to” in SQL. The != operator is more commonly used in MySQL, PostgreSQL, and SQL Server, while <> is the ANSI SQL standard and works across all database systems.







Q3. How do comparison operators handle NULL values?

Any comparison with a NULL value returns UNKNOWN, not TRUE or FALSE. To explicitly check NULLs, you must use IS NULL or IS NOT NULL instead of comparison operators.


SELECT * FROM Employees WHERE bonus IS NULL;





Q4. Can comparison operators be used in subqueries?

Yes. They are often used with subqueries to compare a single value against the result of another query.


SELECT name, salary FROM Employees WHERE salary > (SELECT AVG(salary) FROM Employees);






Q5. How do you combine comparison operators with logical operators in SQL?

Comparison operators can be combined using AND, OR, and NOT to form complex conditions.

SELECT * FROM Students 
WHERE marks > 80 AND attendance >= 75;
This retrieves students who scored above 80 and have attendance greater than or equal to 75%.