Data Sorting and Filtering Techniques with SQL

by | SQL

In today’s data-driven world, SQL (Structured Query Language) has become an indispensable tool for handling, storing, and retrieving data from databases. One of the fundamental tasks in data analysis is to sort and filter the data according to specific criteria, so you can get the answers you need quickly and efficiently.

Sorting and filtering data in SQL allows you to manage large volumes of data and retrieve relevant information. You can organize your data, spot patterns, and derive insights. SQL’s SELECT and ORDER BY statements are the building blocks of data analysis, allowing you to sort and filter your data to meet your specific needs.

This detailed tutorial will show you how to sort and filter data using SQL’s SELECT and ORDER BY statements. You’ll learn the syntax for sorting data in ascending and descending order and how to use filtering criteria to retrieve the data you need.

Let’s get started!

Table of Contents

Introduction to SQL and Databases

Introduction

Structured Query Language (SQL) is a standardized language used to manage and manipulate databases. It is essential for data retrieval, insertion, updating, deleting data in a database, and managing database structures.

Setting Up an SQL Database

Before you can start using SQL, you need to set up a database. This example will use the widely-supported SQLite database for simplicity, but the SQL syntax provided will be applicable to most SQL database systems with minor adjustments.

Creating a Database and Table

  1. Open your database client or command line interface.


  2. Create a new database.

    CREATE DATABASE employee_db;

  3. Use the newly created database.

    USE employee_db;

  4. Create a table within the database to store employee data.

    CREATE TABLE employees (
    employee_id INT PRIMARY KEY,
    first_name VARCHAR(50),
    last_name VARCHAR(50),
    department VARCHAR(50),
    salary DECIMAL(10, 2)
    );

Inserting Data into the Table

Insert some sample data into the employees table.

INSERT INTO employees (employee_id, first_name, last_name, department, salary)
VALUES
    (1, 'John', 'Doe', 'Engineering', 70000.00),
    (2, 'Jane', 'Smith', 'Marketing', 65000.00),
    (3, 'Albert', 'Einstein', 'Research', 120000.00),
    (4, 'Marie', 'Curie', 'Research', 110000.00),
    (5, 'Charles', 'Darwin', 'Biology', 90000.00);

Sorting Data

Sorting data helps in organizing the output in a meaningful order. Use the ORDER BY clause to sort the data.

Example: Sorting by Last Name

SELECT * FROM employees
ORDER BY last_name;

Example: Sorting by Salary in Descending Order

SELECT * FROM employees
ORDER BY salary DESC;

Filtering Data

Filtering data allows you to retrieve specific records that match certain criteria. Use the WHERE clause to filter data.

Example: Filtering by Department

SELECT * FROM employees
WHERE department = 'Research';

Example: Filtering by Salary Range

SELECT * FROM employees
WHERE salary > 80000 AND salary < 130000;

Combining Sorting and Filtering

You can combine sorting and filtering to get ordered results that match specific criteria.

Example: Filtering by Department and Sorting by Salary

SELECT * FROM employees
WHERE department = 'Research'
ORDER BY salary DESC;

Example: Filtering by Department and Salary Range then Sorting by Last Name

SELECT * FROM employees
WHERE department = 'Research' AND salary > 80000
ORDER BY last_name;

Conclusion

SQL is a powerful tool for managing and querying databases. This guide covered the basics of creating a database and table, inserting data, and the core functionalities of sorting and filtering data. These fundamentals will enable you to manage and organize your data efficiently in real-life scenarios.

Sorting and Filtering Data Using SQL

Sorting Data

Sorting allows you to order your retrieved data based on specified columns.

Basic Syntax:

SELECT column1, column2
FROM table_name
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC];

Example:
Sorting employees by their last names in ascending order:

SELECT first_name, last_name
FROM employees
ORDER BY last_name ASC;

Sorting employees by their last names in descending order:

SELECT first_name, last_name
FROM employees
ORDER BY last_name DESC;

Sorting by multiple columns (last name first, then by first name):

SELECT first_name, last_name
FROM employees
ORDER BY last_name ASC, first_name ASC;

Filtering Data

Filtering allows you to fetch only the required data based on specific conditions.

Basic Syntax:

SELECT column1, column2
FROM table_name
WHERE condition;

Example:
Filtering to find employees with a salary greater than 50,000:

SELECT first_name, last_name, salary
FROM employees
WHERE salary > 50000;

Filtering to find employees where the department is ‘HR’:

SELECT first_name, last_name, department
FROM employees
WHERE department = 'HR';

Combining Sorting and Filtering

You can sort and filter data in a single query to fetch more precise results.

Example:
Finding all ‘HR’ department employees with a salary above 50,000 and sorting them by last name:

SELECT first_name, last_name, department, salary
FROM employees
WHERE department = 'HR'
  AND salary > 50000
ORDER BY last_name ASC;

Using LIKE for Pattern Matching

You can use LIKE for partial matches with % (matches any sequence of characters) or _ (matches a single character).

Example:
Finding employees whose first name starts with ‘J’:

SELECT first_name, last_name
FROM employees
WHERE first_name LIKE 'J%';

Finding employees whose first name has ‘oh’ in the middle:

SELECT first_name, last_name
FROM employees
WHERE first_name LIKE '%oh%';

Filtering with IN and BETWEEN

Use IN to filter data that matches any value in a list, and BETWEEN to filter a range of values.

Example:
Finding employees in specific departments:

SELECT first_name, last_name, department
FROM employees
WHERE department IN ('HR', 'Finance', 'IT');

Finding employees with salary between 40,000 and 60,000:

SELECT first_name, last_name, salary
FROM employees
WHERE salary BETWEEN 40000 AND 60000;

Combining Multiple Conditions with AND and OR

Multiple filtering conditions can be combined using logical operators.

Example:
Finding employees in ‘IT’ department or with salary above 70,000:

SELECT first_name, last_name, department, salary
FROM employees
WHERE department = 'IT'
  OR salary > 70000;

Finding employees in ‘IT’ department and with salary above 70,000:

SELECT first_name, last_name, department, salary
FROM employees
WHERE department = 'IT'
  AND salary > 70000;

Conclusion

This chapter covers fundamental concepts of sorting and filtering data using SQL. By mastering these queries, you can efficiently retrieve and manipulate data as per your requirements.

Sorting Data with ORDER BY

Sorting data in SQL is a fundamental operation for organizing query results in a specified order. The ORDER BY clause is used to sort the query results in either ascending or descending order based on one or more columns.

Syntax

SELECT column1, column2, ...
FROM table_name
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...;

Practical Implementation

Consider a database table named Employees with the following columns: EmployeeID, FirstName, LastName, Salary, and Department.

Example 1: Sorting by a Single Column

To sort the employees by LastName in ascending order:

SELECT EmployeeID, FirstName, LastName, Salary, Department
FROM Employees
ORDER BY LastName ASC;

Example 2: Sorting by a Single Column in Descending Order

To sort the employees by Salary in descending order:

SELECT EmployeeID, FirstName, LastName, Salary, Department
FROM Employees
ORDER BY Salary DESC;

Example 3: Sorting by Multiple Columns

To sort the employees by Department in ascending order and then by LastName in ascending order within each department:

SELECT EmployeeID, FirstName, LastName, Salary, Department
FROM Employees
ORDER BY Department ASC, LastName ASC;

Example 4: Sorting by Column Position

It is also possible to sort by the position of the columns in the SELECT statement. For instance, to sort the result first by the fourth column (Salary) in descending order and then by the second column (FirstName) in ascending order:

SELECT EmployeeID, FirstName, LastName, Salary, Department
FROM Employees
ORDER BY 4 DESC, 2 ASC;

Example 5: Combining ORDER BY with Filtering

You can combine ORDER BY with WHERE for more refined results. For instance, to get employees from the IT department and sort by Salary in descending order:

SELECT EmployeeID, FirstName, LastName, Salary, Department
FROM Employees
WHERE Department = 'IT'
ORDER BY Salary DESC;

Example 6: Using ORDER BY with Aliases

Aliases can make your SQL queries more readable. Consider sorting results by an aliased column:

SELECT EmployeeID, FirstName, LastName, Salary AS AnnualSalary, Department
FROM Employees
ORDER BY AnnualSalary DESC;

This organizes the retrieved data by AnnualSalary in descending order while referencing the Salary column.

By using the ORDER BY clause in these ways, you can effectively sort your data in SQL queries to suit various real-world needs.

Filtering Data with WHERE Clause

Practical Implementation

The WHERE clause in SQL is used to filter records based on specific conditions. This clause is used in the SELECT, UPDATE, DELETE, etc., statements to specify which rows should be affected by the query.

Syntax

SELECT column1, column2, ...
FROM table_name
WHERE condition;

Examples

1. Filtering with Equality

To find all employees in the employees table where the department is Sales:

SELECT first_name, last_name
FROM employees
WHERE department = 'Sales';

2. Filtering with Range

To find all products in the products table with a price greater than 100:

SELECT product_name, price
FROM products
WHERE price > 100;

3. Filtering with Multiple Conditions (AND)

To find customers in the customers table from New York with an active status:

SELECT first_name, last_name, city
FROM customers
WHERE city = 'New York' AND status = 'Active';

4. Filtering with Multiple Conditions (OR)

To find all orders in the orders table that are either Pending or Shipped:

SELECT order_id, status
FROM orders
WHERE status = 'Pending' OR status = 'Shipped';

5. Filtering with Pattern Matching

To find all products in the products table where the product name starts with ‘A’:

SELECT product_name
FROM products
WHERE product_name LIKE 'A%';

6. Filtering with NULL Values

To find all entries in the employees table where the phone number is missing:

SELECT first_name, last_name
FROM employees
WHERE phone_number IS NULL;

7. Using IN to Match Multiple Values

To find all employees in the employees table where the role is either Manager or Developer:

SELECT first_name, last_name, role
FROM employees
WHERE role IN ('Manager', 'Developer');

8. Using BETWEEN for Range Filtering

To find all products in the products table where the price is between 50 and 150:

SELECT product_name, price
FROM products
WHERE price BETWEEN 50 AND 150;

Each of these examples demonstrates how the WHERE clause can be used to filter data efficiently to meet specific criteria. Compose your SQL queries according to your data structure and needs.

Combining Filters using AND, OR, and NOT Operators in SQL

Filtering data effectively is crucial in SQL for extracting meaningful insights from your database. SQL provides logical operators such as AND, OR, and NOT to combine multiple filter conditions in the WHERE clause.

Using AND Operator

The AND operator combines multiple conditions and returns true only if all the conditions are true.

SELECT *
FROM Employees
WHERE Department = 'Sales' AND YearsOfService > 5;

Explanation: This query selects all employees from the Sales department who have more than 5 years of service.

Using OR Operator

The OR operator combines multiple conditions and returns true if at least one of the conditions is true.

SELECT *
FROM Employees
WHERE Department = 'Sales' OR Department = 'Marketing';

Explanation: This query selects all employees who are either in the Sales or Marketing department.

Combining AND and OR Operators

You can combine AND and OR operators to form complex conditions. Use parentheses to group conditions and control the order of evaluation.

SELECT *
FROM Employees
WHERE (Department = 'Sales' OR Department = 'Marketing') AND YearsOfService > 5;

Explanation: This query selects employees who are in either the Sales or Marketing department AND have more than 5 years of service.

Using NOT Operator

The NOT operator negates the condition that follows it.

SELECT *
FROM Employees
WHERE NOT Department = 'Sales';

Explanation: This query selects all employees who are not in the Sales department.

Combining AND, OR, and NOT Operators

For even more complex filtering, combine all three logical operators.

SELECT *
FROM Employees
WHERE (Department = 'Sales' OR Department = 'Marketing') AND NOT (YearsOfService <= 3);

Explanation: This query selects employees who are in the Sales or Marketing department AND do not have 3 or fewer years of service.

Practical Example on a Real Dataset

Assuming you have a table Products with columns: Category, Price, and StockQuantity.

Retrieve Products

  1. In Electronics or Toys category
  2. That are priced over $100
  3. And are in stock (quantity > 0)
SELECT *
FROM Products
WHERE (Category = 'Electronics' OR Category = 'Toys') AND Price > 100 AND StockQuantity > 0;

This example demonstrates effectively using the AND, OR, and NOT operators to filter data, ensuring you retrieve exactly what you need from your SQL database.

Using SQL Aggregate Functions for Filtering

Purpose

Using aggregate functions to filter data helps in analyzing and extracting meaningful insights from large datasets. Aggregate functions such as COUNT, SUM, AVG, MIN, and MAX can be combined with the HAVING clause to filter grouped data.

Aggregate Functions in SQL

  • COUNT(): Counts the number of rows.
  • SUM(): Adds up values.
  • AVG(): Calculates the average value.
  • MIN(): Finds the minimum value.
  • MAX(): Finds the maximum value.

Practical Examples

1. Filter Groups with COUNT()

SELECT department, COUNT(*)
FROM employees
GROUP BY department
HAVING COUNT(*) > 10;

Explanation: This query groups employees by department and filters departments having more than 10 employees.

2. Filter Groups with SUM()

SELECT product_id, SUM(quantity_sold) as total_quantity
FROM sales
GROUP BY product_id
HAVING SUM(quantity_sold) > 1000;

Explanation: This query groups sales records by product ID and filters products with total quantity sold greater than 1000.

3. Filter Groups with AVG()

SELECT store_id, AVG(sale_amount) as average_sale
FROM transactions
GROUP BY store_id
HAVING AVG(sale_amount) < 500;

Explanation: This query groups transactions by store ID and filters stores with an average sale amount less than 500.

4. Filter Groups with MIN() and MAX()

SELECT category, MIN(price) as min_price, MAX(price) as max_price
FROM products
GROUP BY category
HAVING MIN(price) < 10 AND MAX(price) > 100;

Explanation: This query groups products by category and filters categories where the minimum price is less than 10 and the maximum price is greater than 100.

Combining Conditions with HAVING

You can use multiple conditions in the HAVING clause, similar to the WHERE clause.

SELECT region, AVG(sales) as avg_sales
FROM quarterly_sales
GROUP BY region
HAVING AVG(sales) > 2000 AND COUNT(*) > 5;

Explanation: This filters regions where the average sales are greater than 2000 and the number of sales records is more than 5.


These examples demonstrate how to use SQL aggregate functions with the HAVING clause to filter grouped data effectively. By applying these concepts, large datasets can be analyzed to draw deeper insights and support data-driven decisions.

Advanced Filtering with Subqueries and Joins

In this section, we’ll explore how to effectively use subqueries and joins to filter data in more advanced SQL scenarios. This allows us to handle complex data retrieval tasks with greater precision and flexibility.

1. Using Subqueries for Advanced Filtering

Example: Filter Based on Aggregate Results

Let’s start by using a subquery to filter employees who earn more than the average salary in their department.

SELECT employee_id, employee_name, salary, department_id
FROM employees e
WHERE salary > (
    SELECT AVG(salary)
    FROM employees
    WHERE department_id = e.department_id
);

Explanation:

  • The subquery calculates the average salary for each department.
  • The main query selects employees whose salary is greater than the average salary for their respective department.

2. Filtering with EXISTS Subquery

Example: Filter Customers Who Have Made a Purchase

This example filters customers who have made at least one purchase.

SELECT customer_id, customer_name
FROM customers c
WHERE EXISTS (
    SELECT 1
    FROM orders o
    WHERE o.customer_id = c.customer_id
);

Explanation:

  • The subquery checks for the existence of related rows in the orders table.
  • The main query retrieves customers if the subquery finds at least one matching order.

3. Using JOINs to Filter

Example: Retrieve Orders with Customer Information

This example retrieves orders along with their respective customer information using an INNER JOIN.

SELECT o.order_id, o.order_date, c.customer_name
FROM orders o
INNER JOIN customers c ON o.customer_id = c.customer_id
WHERE o.order_date > '2023-01-01';

Explanation:

  • Performs an INNER JOIN between orders and customers tables on the customer_id column.
  • Filters orders to include only those made after January 1, 2023, and pulls relevant customer details.

4. Combining Subqueries and Joins

Example: Products with Above Average Sales

This query finds products that have sales above the average for all products.

SELECT p.product_id, p.product_name, SUM(s.amount) AS total_sales
FROM products p
INNER JOIN sales s ON p.product_id = s.product_id
GROUP BY p.product_id, p.product_name
HAVING SUM(s.amount) > (
    SELECT AVG(total_sales)
    FROM (
        SELECT SUM(amount) AS total_sales
        FROM sales
        GROUP BY product_id
    ) AS subquery
);

Explanation:

  • First, it joins products and sales tables.
  • The GROUP BY clause is used to aggregate the sales amount for each product.
  • The HAVING clause with a subquery filters products to include only those with above average total sales.

Each example provided allows us to address complex filtering requirements by leveraging the power of subqueries and joins in SQL.

Performance Optimization: Indexes and Query Tuning

Index Creation in SQL

Indexes significantly improve the speed of data retrieval. Below is an example demonstrating how to create an index on a table.

Creating an Index

-- Syntax: CREATE INDEX index_name ON table_name (column1, column2, ...);
CREATE INDEX idx_users_lastname ON users(lastname);

-- Example usage with a composite index
CREATE INDEX idx_users_lastname_firstname ON users(lastname, firstname);

Query Tuning: Example and Explanation

Example Query Before Tuning

Suppose you have a query that retrieves user details based on the lastname.

SELECT * FROM users WHERE lastname = 'Smith';

Optimized Query with an Index

First, create an index:

CREATE INDEX idx_users_lastname ON users(lastname);

Now run your optimized query:

SELECT * FROM users WHERE lastname = 'Smith';

The index idx_users_lastname will ensure faster retrieval of records.

Analyzing Query Performance

SQL databases provide tools to analyze query performance. For example, using EXPLAIN or EXPLAIN ANALYZE:

EXPLAIN ANALYZE
SELECT * FROM users WHERE lastname = 'Smith';

This will produce a query execution plan showing the impact of using the index.

Indexing Considerations

While indexes improve read operations, they can slow down write operations. Therefore, ensure that you index columns that are frequently used in search conditions.

Use Case: Removing Unnecessary Indexes

Sometimes, you might have redundant indexes which need to be removed.

-- Syntax: DROP INDEX index_name ON table_name;
DROP INDEX idx_users_firstname ON users;

Combining Indexes and Query Tuning

Scenario: Filtering and Sorting

Suppose you have a complex query that filters and sorts data.

Before tuning:

SELECT * FROM orders WHERE customer_id = 12345 ORDER BY order_date DESC;

Create an index on customer_id and order_date:

CREATE INDEX idx_orders_customer_date ON orders(customer_id, order_date);

Run the optimized query:

SELECT * FROM orders WHERE customer_id = 12345 ORDER BY order_date DESC;

Using Covering Indexes

A covering index includes all fields required for a query, avoiding the need to access the table:

Create a covering index:

CREATE INDEX idx_orders_covering ON orders(customer_id, order_date, total_amount);

Run the query with a covering index:

SELECT order_date, total_amount FROM orders WHERE customer_id = 12345 ORDER BY order_date DESC;

Conclusion

To optimize SQL query performance:

  1. Create indexes on frequently searched columns or columns involved in JOIN and ORDER BY clauses.
  2. Analyze query performance using tools like EXPLAIN or EXPLAIN ANALYZE.
  3. Remove redundant indexes to maintain write performance.
  4. Consider covering indexes for complex queries to minimize table lookups.

Use these strategies to efficiently sort and filter large datasets, ensuring scalable and performant applications.

Final Thoughts

Final Thoughts

Sorting and filtering data in SQL are the cornerstones of data analysis. They enable you to organize, understand, and extract insights from your data.

When you sort and filter your data, you make it more manageable and easier to work with. This, in turn, allows you to make better decisions based on the information you have at hand.

Mastering sorting and filtering in SQL is a valuable skill that can benefit you in a wide range of data-related fields, including database management, business intelligence, and data science. So, keep practicing and applying these techniques to your own projects!

To learn more about how to sort data, check out Enterprise DNA’s detailed learning paths and courses.

Related Posts