Computer Science

SQL Operators Class 12 CBSE (083): Mathematical, Relational and Logical Operators

Class 12 · Computer Science

SQL Operators (CBSE Class 12 Computer Science - 083)

SQL operators are special symbols or keywords used to perform calculations, compare values, and combine multiple conditions in SQL queries. Operators are commonly used with the SELECT, UPDATE, and DELETE statements to filter or manipulate data.

Learning Objectives

  • Understand SQL Operators.
  • Learn Mathematical Operators.
  • Learn Relational Operators.
  • Learn Logical Operators.
  • Use operators in SQL queries.

Sample Student Table

The following table will be used throughout this chapter.

StudentID Name Gender Class Section City Marks DeptID
101RiyaFXIIAJaipur951
102AmanMXIIBDelhi912
103PriyaFXIIAJaipur881
104RahulMXIICMumbai763
105NehaFXIIBDelhi842
106ArjunMXIIAJaipur921
107SimranFXIICMumbai813
108KaranMXIIBDelhi692

SQL Operators

SQL operators are classified into three major categories in the CBSE syllabus:

  1. Mathematical Operators
  2. Relational Operators
  3. Logical Operators

1. Mathematical Operators

Mathematical operators perform arithmetic calculations on numeric values.

Operator Description Example
+ Addition Marks + 5
- Subtraction Marks - 5
* Multiplication Marks * 2
/ Division Marks / 2
% Modulus (Remainder) Marks % 2

Example 1 : Addition


SELECT Name,
Marks,
Marks + 5 AS NewMarks
FROM Student;

Output

Name Marks NewMarks
Riya95100
Aman9196
Priya8893

Example 2 : Multiplication


SELECT Name,
Marks,
Marks*2 AS DoubleMarks
FROM Student;

Example 3 : Division


SELECT Name,
Marks,
Marks/2 AS HalfMarks
FROM Student;

2. Relational Operators

Relational operators compare two values and return either TRUE or FALSE.

Operator Description
= Equal To
> Greater Than
< Less Than
>= Greater Than or Equal To
<= Less Than or Equal To
<> or != Not Equal To

Example 1 : Equal To (=)


SELECT *
FROM Student
WHERE City='Jaipur';

Output

StudentID Name City
101RiyaJaipur
103PriyaJaipur
106ArjunJaipur

Example 2 : Greater Than (>)


SELECT Name,Marks
FROM Student
WHERE Marks>90;

Output

Name Marks
Riya95
Aman91
Arjun92

Example 3 : Less Than (<)


SELECT *
FROM Student
WHERE Marks<80;

Example 4 : Greater Than or Equal To


SELECT *
FROM Student
WHERE Marks>=90;

Example 5 : Less Than or Equal To


SELECT *
FROM Student
WHERE Marks<=80;

Example 6 : Not Equal To


SELECT *
FROM Student
WHERE City<>'Delhi';

3. Logical Operators

Logical operators combine multiple conditions in SQL queries.

Operator Description
AND Both conditions must be TRUE.
OR At least one condition must be TRUE.
NOT Reverses the condition.

AND Operator

Returns records only when both conditions are TRUE.


SELECT *
FROM Student
WHERE City='Jaipur'
AND Marks>90;

Output

Name City Marks
RiyaJaipur95
ArjunJaipur92

OR Operator

Returns records when either condition is TRUE.


SELECT *
FROM Student
WHERE City='Delhi'
OR Marks>90;

NOT Operator

Returns records that do not satisfy the given condition.


SELECT *
FROM Student
WHERE NOT City='Delhi';

Combining Relational and Logical Operators


SELECT *
FROM Student
WHERE Marks>=80
AND Marks<=90;

Operator Precedence

When multiple operators are used in the same query, SQL evaluates them according to precedence.

Priority Operator
1 Mathematical Operators
2 Relational Operators
3 NOT
4 AND
5 OR

Comparison of SQL Operators

Operator Type Purpose
Mathematical Performs arithmetic calculations.
Relational Compares values.
Logical Combines multiple conditions.

Common Errors

  • Using = instead of == (SQL uses only =).
  • Forgetting quotation marks around text values.
  • Using AND when OR is required.
  • Incorrect use of NOT with conditions.
  • Ignoring operator precedence.

Exam Tips

  • Mathematical operators perform calculations.
  • Relational operators compare values.
  • Logical operators combine conditions.
  • Remember that AND requires both conditions to be TRUE.
  • OR requires at least one condition to be TRUE.
  • NOT reverses the result of a condition.
  • Practice combining multiple operators in SELECT queries.

Frequently Asked Questions (FAQs)

1. What are SQL operators?

SQL operators are symbols or keywords used to perform calculations, compare values, and combine conditions in SQL statements.

2. Which SQL operators are included in the CBSE syllabus?

Mathematical, Relational, and Logical operators.

3. What is the difference between relational and logical operators?

Relational operators compare values, whereas logical operators combine or modify multiple conditions.

4. Which logical operator returns records only when both conditions are true?

The AND operator.

5. Which operator is used to compare two values for equality?

The = (Equal To) operator.


Summary

  • SQL operators are used to perform calculations and comparisons.
  • Mathematical operators perform arithmetic operations.
  • Relational operators compare values.
  • Logical operators combine multiple conditions.
  • Operators are widely used with the SELECT, UPDATE, and DELETE statements.
  • Understanding operators is essential for writing SQL queries effectively.