Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 68 additions & 54 deletions docs/sql/SQL-basics/the-inequality-operator.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ id: the-inequality-operator
title: The Inequality Operator
sidebar_label: The Inequality Operator
sidebar_position: 4
tags: [html, web-development, document-structure]
description: In this tutorial, you will learn about the structure of an HTML document and how to create a basic HTML document.
tags: [sql, database, queries, operators]
description: In this tutorial, you will learn about the inequality operator and comparison operators in SQL to filter data effectively.
---


# 📗 The Inequality Operator
To check if a coloumns value is not equal to another we use the inequality operator.
The inequality operator (`<>` or `!=`) helps you find records that **don't match** a specific value. Think of it as asking "show me everything except this!"

For example, consider a table named `Friends`. Below is how a simple table might look:
Let's start with a simple example. Imagine you have a `students` table and want to find all students who are NOT in their first year:



:::info
<Tabs>
<TabItem value="SQL Table" label="SQL Table">
```sql title="Friends"
```sql title="Students"

| name | year | major |
|-------------|------|---------|
Expand All @@ -30,7 +30,7 @@ For example, consider a table named `Friends`. Below is how a simple table might

<TabItem value="SQL Code" label="SQL Code">

```sql title="Creating SQL Tables & db. "
```sql title="Finding students not in year 1"
SELECT *
FROM students
WHERE year <> 1;
Expand All @@ -39,7 +39,7 @@ WHERE year <> 1;

</TabItem>

<TabItem value="how-git-works" label="Output ">
<TabItem value="Output" label="Output">
| name | year | major |
| -------- | ---- | ------- |
| Lin Wong | 3 | Biology |
Expand All @@ -50,19 +50,19 @@ WHERE year <> 1;



### 📘 Practise Example
### 📘 Let's Practice Together

> To query data from a table, use the FROM clause followed by the table's name.
> Here's another way to look at it: the FROM clause tells SQL which table to look in, and WHERE tells it what to filter.


For example, consider a table named `Friends`. Below is how a simple table might look:
For example, consider a table named `students`. Let's see the same example again to reinforce the concept:



:::info
<Tabs>
<TabItem value="SQL Table" label="SQL Table">
```sql title="Friends"
```sql title="Students"
| name | year | major |
|-------------|------|---------|
| Ava Smith | 1 | Biology |
Expand All @@ -73,15 +73,15 @@ For example, consider a table named `Friends`. Below is how a simple table might

<TabItem value="SQL Code" label="SQL Code">

```sql title="Creating SQL Tables. "
```sql title="Selecting non-first-year students"
SELECT *
FROM students
WHERE year <> 1;
```

</TabItem>

<TabItem value="how-git-works" label="Output">
<TabItem value="Output" label="Output">
| name | year | major |
|----------|------|---------|
| Lin Wong | 3 | Biology |
Expand All @@ -93,25 +93,33 @@ WHERE year <> 1;



:::tip
The Inequality sign is useful when we want to get all items that dont satisfy a criterion.
it checks the value are not equal.
It can apply to all types of numbers.
:::tip 💡 Quick Tip
The inequality operator is your "everything except" tool! It's perfect when you want to exclude specific values from your results.

By following these best practices,
**What it works with:**
- Numbers: `age <> 25` (everyone except 25-year-olds)
- Text: `major <> 'Biology'` (all majors except Biology)
- Dates: `signup_date <> '2024-01-01'` (all dates except New Year's Day)

Pro tip: Use this when you know what you DON'T want in your results!
:::

### 🔄 Comparison Operator.
### 🔄 More Powerful Comparisons

**The Problem:** Sometimes you need more than just "equal" or "not equal."

**Real-world scenario:** You're analyzing air quality data and want to find cities with low pollution levels (under 100 on the pollution index).

> Sometimes, comparison with = and are not felxible enoguh like when filtering by price of a grocery itesm
> We can use conditions to select items having a property les s than a threshold value like chocolate items with a price less than 2$
> We can also use the numeric properties , it dont need to put in the quotes.
> the = sign check if the two values are equal.
We want to code a condition to select records from the pollution table where the pollution_index is less than 100.
> **Good to know:**
> - Numbers don't need quotes: `pollution_index < 100` ✅
> - Text needs quotes: `city = 'Tokyo'` ✅
> - The `<` symbol means "less than" - just like in math!

Here's how to find cities with clean air (pollution index under 100):
:::info
<Tabs>
<TabItem value="SQL Table" label="SQL Table">
```sql title="friends"
```sql title="Pollution"
| city | pollution_index |
|----------|-----------------|
| Delhi | 168 |
Expand All @@ -123,15 +131,15 @@ We want to code a condition to select records from the pollution table where the

<TabItem value="SQL Code" label="SQL Code">

```sql title="Creating SQL Tables. "
```sql title="Finding cities with low pollution"
SELECT *
FROM pollution
WHERE pollution_index < 100;
```

</TabItem>

<TabItem value="how-git-works" label="Output">
<TabItem value="Output" label="Output">
| city | pollution_index |
|----------|-----------------|
| Shanghai | 74 |
Expand All @@ -143,15 +151,17 @@ WHERE pollution_index < 100;

:::

## 🧹 Selecting Unique Values with numeric
## 🧹 Including the Boundary Value

**What if we want cities with "acceptable" pollution levels?** Let's say anything 122 or below is acceptable.

We want to code a condition to select records from the pollution table where the pollution_index is less than or equal to 122.
The `<=` operator means "less than or equal to" - it includes the exact value too!


:::info
<Tabs>
<TabItem value="SQL Table" label="SQL Table">
```sql title="Subscribers"
```sql title="Pollution"
| city | pollution_index |
|----------|-----------------|
| Delhi | 168 |
Expand All @@ -163,15 +173,15 @@ We want to code a condition to select records from the pollution table where the

<TabItem value="SQL Code" label="SQL Code">

```sql title="Creating SQL Tables. "
```sql title="Finding cities with moderate pollution"
SELECT *
FROM pollution
WHERE pollution_index <= 122;
```

</TabItem>

<TabItem value="how-git-works" label="Output">
<TabItem value="Output" label="Output">
| city | pollution_index |
|----------|-----------------|
| Milano | 122 |
Expand All @@ -185,17 +195,19 @@ WHERE pollution_index <= 122;

:::

## 🧹 Filtering coloumns
## 🎯 Getting Only What You Need

**Smart tip:** You don't always need all the data! Sometimes you only want specific columns to make your results cleaner and easier to read.

When using contions we dont have to select all coloumns with *, we can select only a couple like name and year.
> We dont have to select all colouns when filtering.
**Example:** Instead of seeing all student info, maybe you just want names and years.
> **Memory trick:** Think of `SELECT *` as "give me everything" and `SELECT name, year` as "just give me the basics"

---

:::info
<Tabs>
<TabItem value="SQL Table" label="SQL Table">
```sql title="Subscribers"
```sql title="Students"
| name | year | major |
|-------------|------|---------|
| Ava Smith | 1 | Biology |
Expand All @@ -206,19 +218,19 @@ When using contions we dont have to select all coloumns with *, we can select on

<TabItem value="SQL Code" label="SQL Code">

```sql title="Creating SQL Tables. "
SELECT *
```sql title="Just names and years of first-year students"
SELECT name, year
FROM students
WHERE year = 1;
```

</TabItem>

<TabItem value="how-git-works" label="Output">
| name | year | major |
| ----------- | ---- | ------- |
| Ava Smith | 1 | Biology |
| Luis Garcia | 1 | Physics |
<TabItem value="Output" label="Output">
| name | year |
| ----------- | ---- |
| Ava Smith | 1 |
| Luis Garcia | 1 |



Expand All @@ -229,20 +241,22 @@ WHERE year = 1;
:::


## ✅ What You have Learned
## 🎉 What You've Mastered

Great job! You've learned the essential skills for smart data filtering:

This module covers four essential topics in data selection:
**🚫 The "Not Equal" Expert**
You can now use `<>` and `!=` to exclude specific values and find everything except what you don't want.

- **Rows and Columns**
Learn how to access specific rows and columns in a dataset or table, the building blocks of any query.
**📊 The Comparison Pro**
You've mastered `<`, `>`, `<=`, and `>=` for finding records above, below, or within specific ranges.

- **Select Data**
Understand the basic `SELECT` statement to retrieve data from a database.
**🎯 The Precision Selector**
You know how to pick just the columns you need instead of grabbing everything with `*`.

- **Select Multiple Columns**
Retrieve more than one column at a time in your queries to get the information you need all at once.
**🔍 The Smart Filterer**
You can combine `WHERE` clauses with any operator to create laser-focused queries that find exactly what you're looking for.

- **Select Distinct Values**
Use `DISTINCT` to eliminate duplicate records and identify unique entries within your dataset.
**Next up:** Try combining these operators with `AND` and `OR` to create even more powerful searches!

---
---
Loading