top of page

A Comprehensive Guide to SQL Filtering and Querying



Introduction to Filtering


Filtering is an essential technique for managing data and extracting useful information. In business applications, it enables us to sift through large datasets and narrow down to the specific pieces of information needed to answer essential questions. Let's break down this concept and understand how to filter numbers in SQL.


Explanation of Selecting and Counting Data

  • Explanation: Selecting and counting data is the foundation of filtering. Selecting data involves retrieving specific columns from a table, while counting data helps us understand the quantity of data points satisfying particular criteria.

  • Example: Imagine a database containing a table of movies. We can select all movie titles released in 2020 and count how many there are.

SELECT title FROM movies WHERE release_year = 2020;
SELECT COUNT(*) FROM movies WHERE release_year = 2020;

  • Output: The first query will list all movie titles released in 2020. The second query will return the count of those movies.


Importance of Filtering for Business-Related Questions

  • Explanation: Filtering aids in business decision-making by providing precise data. It helps in analyzing trends, customer behaviors, and more.

  • Example Analogy: Consider filtering as a fine mesh that only lets through the desired elements. Similarly, in a sales database, filtering helps isolate records for a specific product or time frame, helping in a focused analysis.


WHERE Clause


Introduction to the WHERE Clause

  • Explanation: The WHERE clause in SQL filters records based on one or more conditions. It allows us to narrow down the results of a query to match specific criteria.

  • Code Snippet:

SELECT * FROM employees WHERE department = 'Sales';

  • Output: This code will return all the records from the 'employees' table where the department is 'Sales'.


Applying WHERE to Filter by Color

  • Explanation: We can apply the WHERE clause to filter data by specific attributes like color.

  • Example: Let's say we want to select all products that are red from a 'products' table.

SELECT * FROM products WHERE color = 'Red';

  • Output: This will display all the products in the 'products' table that are red.


Comparison Operators with WHERE


Filtering Using Comparison Operators: Greater Than, Less Than, etc.

  • Explanation: Comparison operators allow us to filter data based on numerical relationships.

  • Examples: Filtering films based on release year.

SELECT title FROM movies WHERE release_year > 2005 AND release_year < 2015;

  • Output: This will list movies released between 2005 and 2015.


Summary of Comparison Operators

  • Greater than (>), less than (<), equal to (=), greater/less than or equal to (>=, <=), not equal to (<> or !=).


Using WHERE with Strings

  • Explanation: You can also use the WHERE clause to filter data based on string comparisons.

  • Code Snippet:

SELECT * FROM books WHERE author_name = 'J.K. Rowling';

  • Output: This will list all the books authored by J.K. Rowling.


Order of Execution

  • Understanding the Order of SQL Commands: The order of execution in an SQL query is essential. It follows the order: SELECT, FROM, WHERE, LIMIT.


This introduction has provided a foundation in basic filtering techniques, including numeric and text-based comparisons. In the next section, we will explore filtering using multiple criteria.


Multiple Criteria Filtering


Introduction to Multiple Criteria Filtering

  • Explanation: Utilizing multiple criteria in filtering allows us to refine queries further. It permits more specific targeting of records that meet several conditions simultaneously.

  • Example Analogy: Think of multiple criteria filtering as setting up a series of gates. A record must pass through all the gates to be included in the final result, meeting each criterion.


Using OR Operator

  • Definition and Usage: The OR operator allows you to filter records based on more than one condition. If any of the conditions are true, the record is included in the result.

  • Example: Filtering movies by multiple release years.

SELECT title FROM movies WHERE release_year = 2000 OR release_year = 2010;

  • Output: Lists all the movies released in either 2000 or 2010.


Using AND Operator

  • Explanation: The AND operator filters records based on multiple conditions. All the conditions must be true for the record to be included.

  • Code Snippet:

SELECT * FROM products WHERE category = 'Electronics' AND price > 100;

  • Output: Lists all products in the 'Electronics' category with a price greater than 100.


Combining AND and OR

  • Example: Filtering films by release year and certification.

SELECT * FROM movies WHERE (release_year > 2000 AND release_year < 2010) OR certification = 'PG';

  • Output: Lists movies released between 2000 and 2010, or with a 'PG' certification.


Using BETWEEN Keyword

  • Explanation: The BETWEEN keyword allows you to filter values within a specified range.

  • Code Snippet:

SELECT name FROM employees WHERE salary BETWEEN 50000 AND 100000;

  • Output: Lists all employees with salaries between 50,000 and 100,000.


Combining BETWEEN, AND, OR

  • Explanation: You can enhance your queries by combining multiple operators.

  • Example:

SELECT * FROM products WHERE (price BETWEEN 10 AND 50) AND (category = 'Books' OR category = 'Games');

  • Output: Lists all products priced between 10 and 50 in the 'Books' or 'Games' categories.


Filtering Text


Introduction to Text Filtering

  • Explanation: Text filtering deals with selecting records based on text attributes. We can match patterns or specific strings.

  • Example: Let's take a look at a real SQL command.

SELECT * FROM customers WHERE city LIKE 'New%';

  • Output: Lists all customers from cities that start with 'New'.


Using LIKE Operator

  • Explanation: The LIKE operator, coupled with wildcards, enables pattern matching.

  • Code Snippet:

SELECT * FROM products WHERE name LIKE 'A%';

  • Output: Lists all products with names starting with 'A'.


Using NOT LIKE Operator

  • Explanation: The NOT LIKE operator finds records that don’t match a pattern.

  • Code Snippet:

SELECT * FROM students WHERE name NOT LIKE 'J%';

  • Output: Lists all students whose names don't start with 'J'.


Wildcard Positioning

  • Explanation: Various positioning of wildcards creates different matches.

  • Example:

SELECT * FROM employees WHERE email LIKE '%@example.com';

  • Output: Lists all employees with an email address ending with '@example.com'.


Using IN Operator

  • Explanation: The IN operator specifies multiple values in a WHERE clause.

  • Code Snippet:

SELECT * FROM orders WHERE status IN ('Shipped', 'Processing');

  • Output: Lists all orders with statuses 'Shipped' or 'Processing'.


This part of the tutorial has explored advanced filtering techniques using multiple criteria and text matching. Such methods allow for precise control over query results, aiding in targeted data analysis. Let me know if you would like me to proceed to the next part, where we'll discuss handling NULL values.


Handling NULL Values


Introduction to NULL Values

  • Explanation: NULL values represent missing or unknown data. They're not the same as zero or a blank space; they signify a lack of information.

  • Example Analogy: Think of NULL values as blank cells in a spreadsheet. They are placeholders for information that might exist but is not currently available.


Using IS NULL and IS NOT NULL

  • Explanation: The IS NULL and IS NOT NULL operators are used to identify and filter NULL values.

  • Code Snippet for IS NULL:

SELECT * FROM employees WHERE phone_number IS NULL;

  • Output: Lists all employees with missing phone numbers.

  • Code Snippet for IS NOT NULL:

SELECT * FROM students WHERE graduation_date IS NOT NULL;

  • Output: Lists all students who have a graduation date.


Comparison between COUNT() vs IS NOT NULL

  • Explanation: Both COUNT() and IS NOT NULL can be used to deal with missing values, but they serve different purposes.

  • Using COUNT(): COUNT() returns the number of non-NULL values in a specific column.

SELECT COUNT(email) FROM customers;

  • Output: Returns the number of customers with an email address.

  • Using IS NOT NULL:

SELECT * FROM customers WHERE email IS NOT NULL;

  • Output: Lists all customers who have an email address (without counting them).

  • Comparison: While COUNT() gives you the number of non-NULL entries, IS NOT NULL helps you filter the records that contain non-NULL values.


Conclusion


Handling and filtering data in SQL is an essential skill for data analysts and database administrators. In this tutorial, we explored techniques ranging from basic filtering using the WHERE clause to more advanced topics like handling NULL values.


We began by understanding the different comparison operators, then moved to multiple criteria filtering, combining different logical operators, and handling textual patterns. Finally, we discussed NULL values, understanding how to identify and handle these in SQL queries.

Whether you're creating complex queries or simply filtering data for reporting, the techniques and examples provided here can be valuable tools in your data analysis toolkit. Happy querying!

bottom of page