Class-30

Mastering SQL Filtering: Handling Greater Than, Less Than, and Null Values

SQL provides powerful filtering options that allow us to retrieve specific data efficiently. In this article, we will explore various SQL operators, focusing on:

  • Filtering data based on values greater than or less than a given number
  • Ordering the results
  • Using equality and inequality operators
  • Handling NULL values

Let's dive into practical SQL techniques that can help us refine our queries.

Filtering Amounts Greater Than a Specific Value

The marketing manager wants to see only payments where the amount is greater than $10 or $10.99. To achieve this, we use the > (greater than) operator in an SQL query:

SELECT * FROM payments WHERE amount > 10;

This query retrieves all rows where the amount is greater than $10. If we specifically want amounts greater than $10.99, we modify the condition:

SELECT * FROM payments WHERE amount > 10.99;

Similarly, we can use the < (less than) operator to filter values below a certain threshold:

SELECT * FROM payments WHERE amount < 10.99;

Sorting Results with ORDER BY

By default, SQL does not return results in any specific order. If we want to organize the amounts in ascending order, we use:

SELECT * FROM payments WHERE amount > 10 ORDER BY amount;

To display the amounts in descending order, we modify the query:

SELECT * FROM payments WHERE amount > 10 ORDER BY amount DESC;

Using Greater Than or Equal To (>=) and Less Than or Equal To (<=)

SQL also allows us to include the threshold values by using >= (greater than or equal to) and <= (less than or equal to):

SELECT * FROM payments WHERE amount >= 10.99;
SELECT * FROM payments WHERE amount <= 10.99;

These queries will include amounts that are exactly $10.99 in addition to amounts above or below the threshold.

Using NOT EQUAL TO (!= or <>)

If we want to exclude a specific value, we use the != or <> operator. For example, to exclude payments of exactly $10.99, we write:

SELECT * FROM payments WHERE amount != 10.99;

Alternatively, using < >:

SELECT * FROM payments WHERE amount <> 10.99;

Both queries return all payments except those with an amount of $10.99.

Handling NULL Values

In SQL, NULL represents a missing or undefined value. To filter records where a column has no value, we use IS NULL:

SELECT * FROM customers WHERE last_name IS NULL;

Conversely, to find all records where the last name is not NULL:

SELECT * FROM customers WHERE last_name IS NOT NULL;

This ensures that only rows with a valid last name are retrieved.

Conclusion

By mastering these SQL operators, we can efficiently filter and manipulate data to meet specific requirements. Whether filtering payments greater than a certain amount, ordering results, excluding specific values, or handling NULL values, these techniques are essential for effective database management.

Now it's your turn—try these queries in your SQL editor and enhance your data retrieval skills! 🚀


Was this article helpful?