|
| 1 | +--- |
| 2 | +id: sql-operators |
| 3 | +title: SQL Operators |
| 4 | +sidebar_label: SQL Operators |
| 5 | +sidebar_position: 8 |
| 6 | +tags: [sql, operators, database, relational-databases] |
| 7 | +description: In this super beginner-friendly guide, you’ll learn about SQL operators—the tools that help you do math, compare things, and make decisions in your queries! |
| 8 | +keywords: [sql, operators, sql tutorial, sql basics, database management, relational databases, sql operators tutorial, sql for beginners, sql in 2025] |
| 9 | +--- |
| 10 | + |
| 11 | +## 📙 Welcome to SQL Operators! |
| 12 | + |
| 13 | +Hey there, SQL beginner! Operators are like magic symbols in SQL that let you add numbers, compare values, or combine conditions. Think of them as the plus (+) or equals (=) signs you use in math, but for databases. They’re super useful in queries, especially in WHERE clauses to filter data. We’ll use a simple `students` table (with columns like `id`, `name`, `age`, `marks`, and `city`) to explain everything with easy examples. Let’s dive in step by step! |
| 14 | + |
| 15 | +### 📘 What Are Operators? |
| 16 | + |
| 17 | +Operators are symbols or keywords that perform operations on data. They help you: |
| 18 | +- Do calculations (arithmetic operators). |
| 19 | +- Compare values (comparison operators). |
| 20 | +- Combine conditions (logical operators). |
| 21 | +- Check for patterns or ranges (special operators like LIKE or BETWEEN). |
| 22 | + |
| 23 | +You’ll often use them in SELECT statements to filter or calculate results. |
| 24 | + |
| 25 | +> **Pro Tip**: Operators make your queries smarter—practice them to find exactly the data you need! |
| 26 | +
|
| 27 | +### 📘 Arithmetic Operators (Math in SQL!) |
| 28 | + |
| 29 | +These operators let you add, subtract, multiply, divide, or find remainders. They’re great for calculating things like total marks. |
| 30 | + |
| 31 | +The main ones: |
| 32 | +- **+** (Addition): Adds numbers. |
| 33 | +- **-** (Subtraction): Subtracts numbers. |
| 34 | +- *** (Multiplication): Multiplies numbers. |
| 35 | +- **/** (Division): Divides numbers. |
| 36 | +- **%** (Modulus): Gives the remainder. |
| 37 | + |
| 38 | +**Example**: |
| 39 | + :::info |
| 40 | +<Tabs> |
| 41 | + <TabItem value="SQL Code" label="SQL Code"> |
| 42 | +```sql title="Using Arithmetic Operators" |
| 43 | +-- Assume students table has marks column |
| 44 | +SELECT id, name, marks + 10 AS bonus_marks -- Add 10 to marks |
| 45 | +FROM students |
| 46 | +WHERE marks > 80; |
| 47 | +``` |
| 48 | + </TabItem> |
| 49 | + |
| 50 | + <TabItem value="Output" label="Output"> |
| 51 | +| id | name | bonus_marks | |
| 52 | +|----|-------|-------------| |
| 53 | +| 1 | Alice | 95 | |
| 54 | +| 2 | Bob | 102 | |
| 55 | + </TabItem> |
| 56 | +</Tabs> |
| 57 | +::: |
| 58 | + |
| 59 | +> **What NOT to Do**: Don’t divide by zero (e.g., marks / 0)—it’ll cause an error. Always check for zero first! |
| 60 | +
|
| 61 | +### 🔄 Comparison Operators (Checking If Things Match!) |
| 62 | + |
| 63 | +These help you compare values, like finding students older than 18. They’re used in WHERE to filter rows. |
| 64 | + |
| 65 | +The main ones: |
| 66 | +- **=** (Equal): Checks if equal. |
| 67 | +- **!=** or **<>** (Not Equal): Checks if not equal. |
| 68 | +- **>** (Greater Than): Bigger than. |
| 69 | +- **<** (Less Than): Smaller than. |
| 70 | +- **>=** (Greater Than or Equal): Bigger or same. |
| 71 | +- **<=** (Less Than or Equal): Smaller or same. |
| 72 | + |
| 73 | +**Example**: |
| 74 | + :::info |
| 75 | +<Tabs> |
| 76 | + <TabItem value="SQL Code" label="SQL Code"> |
| 77 | +```sql title="Using Comparison Operators" |
| 78 | +SELECT id, name, age |
| 79 | +FROM students |
| 80 | +WHERE age >= 18 AND marks > 80; -- Adults with high marks |
| 81 | +``` |
| 82 | + </TabItem> |
| 83 | + |
| 84 | + <TabItem value="Output" label="Output"> |
| 85 | +| id | name | age | |
| 86 | +|----|-------|-----| |
| 87 | +| 1 | Alice | 20 | |
| 88 | +| 2 | Bob | 22 | |
| 89 | + </TabItem> |
| 90 | +</Tabs> |
| 91 | +::: |
| 92 | + |
| 93 | +> **What NOT to Do**: Don’t use = for strings without quotes (e.g., WHERE city = Delhi)—always quote text like 'Delhi' to avoid errors! |
| 94 | +
|
| 95 | +### 📘 Logical Operators (Combining Conditions!) |
| 96 | + |
| 97 | +These let you mix multiple conditions, like finding students who are young OR high-scorers. |
| 98 | + |
| 99 | +The main ones: |
| 100 | +- **AND**: Both conditions must be true. |
| 101 | +- **OR**: At least one condition is true. |
| 102 | +- **NOT**: Reverses a condition (true becomes false). |
| 103 | + |
| 104 | +**Example**: |
| 105 | + :::info |
| 106 | +<Tabs> |
| 107 | + <TabItem value="SQL Code" label="SQL Code"> |
| 108 | +```sql title="Using Logical Operators" |
| 109 | +SELECT id, name, marks |
| 110 | +FROM students |
| 111 | +WHERE (marks > 90 OR age < 18) AND NOT city = 'Delhi'; -- High marks or young, not from Delhi |
| 112 | +``` |
| 113 | + </TabItem> |
| 114 | + |
| 115 | + <TabItem value="Output" label="Output"> |
| 116 | +| id | name | marks | |
| 117 | +|----|-------|-------| |
| 118 | +| 3 | Carol | 95 | |
| 119 | +| 4 | Dave | 45 | |
| 120 | + </TabItem> |
| 121 | +</Tabs> |
| 122 | +::: |
| 123 | + |
| 124 | +> **What NOT to Do**: Don’t overuse AND/OR without parentheses (e.g., A AND B OR C)—it can confuse the order. Use (A AND B) OR C to be clear! |
| 125 | +
|
| 126 | +### 🔄 Special Operators (Fancy Filters!) |
| 127 | + |
| 128 | +These are extra helpful for patterns or ranges: |
| 129 | +- **BETWEEN**: Checks a range (e.g., marks BETWEEN 70 AND 90). |
| 130 | +- **IN**: Checks against a list (e.g., city IN ('Delhi', 'Mumbai')). |
| 131 | +- **LIKE**: Pattern matching (e.g., name LIKE 'A%' for names starting with A). |
| 132 | +- **IS NULL / IS NOT NULL**: Checks for empty values. |
| 133 | + |
| 134 | +**Example**: |
| 135 | + :::info |
| 136 | +<Tabs> |
| 137 | + <TabItem value="SQL Code" label="SQL Code"> |
| 138 | +```sql title="Using Special Operators" |
| 139 | +SELECT id, name, city |
| 140 | +FROM students |
| 141 | +WHERE marks BETWEEN 80 AND 90 AND name LIKE 'A%'; -- Marks 80-90, name starts with A |
| 142 | +``` |
| 143 | + </TabItem> |
| 144 | + |
| 145 | + <TabItem value="Output" label="Output"> |
| 146 | +| id | name | city | |
| 147 | +|----|-------|-------| |
| 148 | +| 1 | Alice | Mumbai| |
| 149 | + </TabItem> |
| 150 | +</Tabs> |
| 151 | +::: |
| 152 | + |
| 153 | +> **What NOT to Do**: Don’t use LIKE without % or _ wildcards (e.g., name LIKE 'Alice')—it’s the same as = and less efficient for patterns! |
| 154 | +
|
| 155 | +## ✅ What You’ve Learned |
| 156 | + |
| 157 | +You’re a SQL operator pro now! You’ve explored: |
| 158 | +- **Arithmetic Operators**: +, -, *, /, % for calculations. |
| 159 | +- **Comparison Operators**: =, !=, >, etc., for checks. |
| 160 | +- **Logical Operators**: AND, OR, NOT for combining. |
| 161 | +- **Special Operators**: BETWEEN, IN, LIKE, IS NULL for advanced filters. |
| 162 | + |
| 163 | +Practice by writing queries with these operators on a sample table. Avoid the "What NOT to Do" tips to keep your queries error-free and fast! |
| 164 | + |
| 165 | +--- |
0 commit comments