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 |
|---|---|---|---|---|---|---|---|
| 101 | Riya | F | XII | A | Jaipur | 95 | 1 |
| 102 | Aman | M | XII | B | Delhi | 91 | 2 |
| 103 | Priya | F | XII | A | Jaipur | 88 | 1 |
| 104 | Rahul | M | XII | C | Mumbai | 76 | 3 |
| 105 | Neha | F | XII | B | Delhi | 84 | 2 |
| 106 | Arjun | M | XII | A | Jaipur | 92 | 1 |
| 107 | Simran | F | XII | C | Mumbai | 81 | 3 |
| 108 | Karan | M | XII | B | Delhi | 69 | 2 |
SQL Operators
SQL operators are classified into three major categories in the CBSE syllabus:
- Mathematical Operators
- Relational Operators
- 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 |
|---|---|---|
| Riya | 95 | 100 |
| Aman | 91 | 96 |
| Priya | 88 | 93 |
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 |
|---|---|---|
| 101 | Riya | Jaipur |
| 103 | Priya | Jaipur |
| 106 | Arjun | Jaipur |
Example 2 : Greater Than (>)
SELECT Name,Marks
FROM Student
WHERE Marks>90;
Output
| Name | Marks |
|---|---|
| Riya | 95 |
| Aman | 91 |
| Arjun | 92 |
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 |
|---|---|---|
| Riya | Jaipur | 95 |
| Arjun | Jaipur | 92 |
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.