Sql Sorting And Filtering With Set Operations Complete Guide

 Last Update:2025-06-23T00:00:00     .NET School AI Teacher - SELECT ANY TEXT TO EXPLANATION.    8 mins read      Difficulty-Level: beginner

Understanding the Core Concepts of SQL Sorting and Filtering with Set Operations

SQL Sorting and Filtering with Set Operations

Introduction

Key Components

  1. Sorting: Arranges the rows of a result set based on specified columns in ascending or descending order.
  2. Filtering: Selects specific rows from a result set based on conditions provided in the WHERE clause.
  3. Set Operations: Manipulate multiple result sets:
    • UNION: Combines multiple SELECT statements into a single result set, removing duplicate rows.
    • UNION ALL: Combines multiple SELECT statements without removing duplicate rows.
    • INTERSECT: Returns only the rows that are common between two SELECT statements.
    • EXCEPT: Returns rows from the first SELECT statement that are not present in the second SELECT statement (also known as MINUS in some SQL dialects like Oracle).

SQL Sorting

Syntax:

SELECT column_list
FROM table_name
ORDER BY column_name [ASC | DESC], column_name [ASC | DESC], ...;
  • column_list: Specifies the columns to be retrieved.
  • table_name: Specifies the table(s) from which to retrieve the data.
  • ORDER BY: Sorts the result set based on one or more columns.
  • ASC: Sorts the data in ascending order (default).
  • DESC: Sorts the data in descending order.

Example:

SELECT name, salary
FROM employees
ORDER BY salary DESC, name ASC;

This query retrieves the name and salary columns from the employees table, sorting primarily by salary in descending order and then by name in ascending order if salaries are equal.

SQL Filtering

Syntax:

SELECT column_list
FROM table_name
WHERE condition;
  • column_list: Specifies the columns to be retrieved.
  • table_name: Specifies the table from which to retrieve the data.
  • WHERE: Filters rows based on a specified condition.

Example:

SELECT name, salary
FROM employees
WHERE department_id = 5 AND salary > 50000;

This query retrieves the name and salary of employees who belong to department 5 and earn more than $50,000.

Set Operations: Details and Examples

  1. UNION

    Combines the results of two or more SELECT statements, removing any duplicate rows. Both result sets must have the same number of columns and compatible data types.

    Syntax:

    SELECT column_list
    FROM table_name1
    UNION
    SELECT column_list
    FROM table_name2;
    

    Example:

    SELECT name FROM employees WHERE department_id = 5
    UNION
    SELECT name FROM employees WHERE department_id = 6;
    

    This query returns a list of employee names from departments 5 and 6, with duplicates removed.

  2. UNION ALL

    Similar to UNION, but it does not remove duplicate rows. UNION ALL generally performs better than UNION because it skips the distinct operation.

    Syntax:

    SELECT column_list
    FROM table_name1
    UNION ALL
    SELECT column_list
    FROM table_name2;
    

    Example:

    SELECT name FROM employees WHERE department_id = 5
    UNION ALL
    SELECT name FROM employees WHERE department_id = 6;
    

    This query combines the names of employees from departments 5 and 6 without removing duplicates.

  3. INTERSECT

    Returns rows that are common to both result sets. The same rules apply regarding the compatibility of columns and data types.

    Syntax:

    SELECT column_list
    FROM table_name1
    INTERSECT
    SELECT column_list
    FROM table_name2;
    

    Example:

    SELECT name FROM employees WHERE department_id = 5
    INTERSECT
    SELECT name FROM employees WHERE title = 'Manager';
    

    This query returns the names of employees who are in department 5 and also hold the position of a Manager.

  4. EXCEPT

    Returns rows from the first result set that do not exist in the second result set. Similar compatibility rules apply as with other set operations.

    Syntax:

    SELECT column_list
    FROM table_name1
    EXCEPT
    SELECT column_list
    FROM table_name2;
    

    Example:

    SELECT name FROM employees WHERE department_id = 5
    EXCEPT
    SELECT name FROM employees WHERE title = 'Manager';
    

    This query lists employees in department 5 excluding those who are Managers.

Important Information

  • Column Compatibility: Ensure that the columns being used in the SELECT statements are compatible in terms of type and number.

  • Null Values Handling: UNION treats nulls as equal values, meaning duplicate rows containing nulls will be removed. EXCEPT and INTERSECT also handle nulls similarly.

  • Performance Considerations: Using UNION ALL over UNION can improve performance since it avoids the overhead of eliminating duplicates.

  • Aliasing: In complex queries involving set operations, aliasing can help increase readability.

  • Sorting After Union/Intersect/Except: To sort the combined result set, use the ORDER BY clause at the end of the set operation:

    SELECT name FROM employees WHERE department_id = 5
    UNION
    SELECT name FROM employees WHERE department_id = 6
    ORDER BY name;
    

    This example demonstrates sorting after combining results from departments 5 and 6.

Practical Usage

These operations are particularly useful when dealing with data from different tables or when needing to merge data sets while maintaining consistency. For example:

  • Merging Data Across Multiple Sources: Combine customer data from different sales teams using UNION.
  • Identifying Commonalities: Use INTERSECT to find employees who both meet certain criteria (e.g., high performers and managers).
  • Finding Differences: Employ EXCEPT to discover records in one dataset that are missing from another, aiding in data auditing and reconciliation processes.

Online Code run

🔔 Note: Select your programming language to check or run code at

💻 Run Code Compiler

Step-by-Step Guide: How to Implement SQL Sorting and Filtering with Set Operations

SQL Sorting and Filtering

Sorting with ORDER BY: The ORDER BY clause is used to sort the result-set in ascending (ASC) or descending (DESC) order.

Example 1: Sort employees by their first name in ascending order.

SELECT first_name, last_name
FROM employees
ORDER BY first_name ASC;

Step-by-step:

  1. SELECT first_name, last_name: Select the columns first_name and last_name from the table.
  2. FROM employees: Specify the table to use, which is employees.
  3. ORDER BY first_name ASC: Sort the results by first_name in ascending order.

Filtering with WHERE: The WHERE clause is used to filter records based on a specified condition.

Example 2: Select employees who earn more than $50,000.

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

Step-by-step:

  1. SELECT first_name, last_name, salary: Select the columns first_name, last_name, and salary from the table.
  2. FROM employees: Specify the table to use, which is employees.
  3. WHERE salary > 50000: Filter the results to include only rows where the salary is greater than 50,000.

SQL Set Operations

Set operations include UNION, INTERSECT, EXCEPT (or MINUS in some SQL dialects), and they are used to combine rows from two or more queries.

UNION: The UNION operator combines the results of two or more SELECT statements. The UNION operator removes duplicate rows.

SELECT first_name, last_name
FROM employees
WHERE department_id = 10

UNION

SELECT first_name, last_name
FROM employees
WHERE department_id = 20;

Step-by-step:

  1. SELECT first_name, last_name FROM employees WHERE department_id = 10: Select the first_name and last_name of employees in department ID 10.
  2. UNION: Combine the results of the previous query with the following.
  3. SELECT first_name, last_name FROM employees WHERE department_id = 20: Select the first_name and last_name of employees in department ID 20.
  4. The final result will include unique names from both departments and exclude duplicates.

INTERSECT: The INTERSECT operator returns only the rows that are common to both result sets.

SELECT first_name, last_name
FROM employees
WHERE department_id = 10

INTERSECT

SELECT first_name, last_name
FROM employees
WHERE manager_id = 101;

Step-by-step:

  1. SELECT first_name, last_name FROM employees WHERE department_id = 10: Select the first_name and last_name of employees in department ID 10.
  2. INTERSECT: Include only the rows that are present in both result sets.
  3. SELECT first_name, last_name FROM employees WHERE manager_id = 101: Select the first_name and last_name of employees with manager ID 101.
  4. The final result will include only those rows that meet both conditions.

EXCEPT (or MINUS in some SQL dialects): The EXCEPT operator (or MINUS in Oracle) returns all rows from the first query that are not present in the second query.

SELECT first_name, last_name
FROM employees
WHERE department_id = 10

EXCEPT

SELECT first_name, last_name
FROM employees
WHERE department_id = 20;

Step-by-step:

  1. SELECT first_name, last_name FROM employees WHERE department_id = 10: Select the first_name and last_name of employees in department ID 10.
  2. EXCEPT: Exclude the rows that are present in the following query.
  3. SELECT first_name, last_name FROM employees WHERE department_id = 20: Select the first_name and last_name of employees in department ID 20.
  4. The final result will include those rows from department 10 that are not present in department 20.

Combining Sorting, Filtering, and Set Operations

Example 4: Find unique department IDs of employees who earn more than $50,000 and then sort these department IDs in ascending order.

SELECT department_id
FROM employees
WHERE salary > 50000

ORDER BY department_id;

Step-by-step:

  1. SELECT department_id: Select the department_id column.
  2. FROM employees: Specify the employees table.
  3. WHERE salary > 50000: Filter employees who earn more than $50,000.
  4. ORDER BY department_id: Sort the resulting department IDs in ascending order.

Example 5: Find the employees who are either in department 10 or department 20 and then sort them by salary in descending order.

SELECT first_name, last_name, department_id, salary
FROM employees
WHERE department_id = 10

UNION

SELECT first_name, last_name, department_id, salary
FROM employees
WHERE department_id = 20

ORDER BY salary DESC;

Step-by-step:

  1. SELECT first_name, last_name, department_id, salary FROM employees WHERE department_id = 10: Select employees from department 10.
  2. UNION: Combine with the results from the next query, removing duplicates.
  3. SELECT first_name, last_name, department_id, salary FROM employees WHERE department_id = 20: Select employees from department 20.
  4. ORDER BY salary DESC: Sort the combined result by salary in descending order.

Top 10 Interview Questions & Answers on SQL Sorting and Filtering with Set Operations

1. How do you sort data in SQL?

Answer: To sort data in SQL, use the ORDER BY clause. It is used to arrange the result set in ascending (ASC) or descending (DESC) order based on one or more columns.

Example:

SELECT * FROM employees ORDER BY salary DESC;

This sorts the employees table by salary in descending order.

2. Can you sort by multiple columns in SQL?

Answer: Yes, absolutely. You can specify several columns in the ORDER BY clause to sort the data first by the primary column and then by secondary columns.

Example:

SELECT * FROM orders ORDER BY customer_id ASC, order_date DESC;

This orders the orders table by customer_id in ascending order, and if there are ties, it further orders by order_date in descending order.

3. What does the WHERE clause do in SQL?

Answer: The WHERE clause filters records that meet specific conditions and is crucial for refining the dataset.

Example:

SELECT * FROM products WHERE price > 100;

This retrieves all columns from the products table where the price is greater than 100.

4. How can you combine multiple WHERE conditions in SQL?

Answer: Use logical operators such as AND, OR, and NOT to combine multiple conditions in the WHERE clause.

Example:

SELECT * FROM employees WHERE department = 'Sales' AND status = 'Active';

This selects all active employees who work in the sales department.

5. What is an example of using LIKE in SQL filtering?

Answer: LIKE is used for pattern matching in the WHERE clause.

Example:

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

This selects all customers whose names start with the letter 'A'.

6. How do you use DISTINCT to eliminate duplicates in SQL results?

Answer: DISTINCT is used to return only unique values.

Example:

SELECT DISTINCT job_title FROM employees;

This returns a list of unique job titles from the employees table.

7. What is a union in SQL and how is it used?

Answer: A UNION operator combines the result sets of two or more SELECT queries into a single list. All selected columns must be of similar datatypes and in the same order. By default, UNION removes duplicate rows. Use UNION ALL to include duplicates.

Example:

SELECT name FROM employees UNION SELECT name FROM contractors;

This combines the names of employees and contractors into one list without repeating any name.

8. What is the difference between INTERSECT and EXCEPT in SQL?

Answer:

  • INTERSECT returns the rows common to both SELECT statements.
  • EXCEPT (or MINUS in some SQL dialects) returns all rows from the first SELECT statement that are not returned by the second SELECT statement.

Example:

SELECT name FROM employees INTERSECT SELECT name FROM contractors;
-- This will return the names of people who are listed as both employees and contractors

SELECT name FROM employees EXCEPT SELECT name FROM contractors;
-- This will return the names of employees who are not listed as contractors

9. How can you use IN with a subquery in filtering?

Answer: The IN operator allows you to filter based on a list of values provided, including those generated from a subquery.

Example:

SELECT * FROM orders WHERE customer_id IN (SELECT id FROM customers WHERE country = 'USA');

This retrieves orders placed by customers from the USA.

10. What does the HAVING clause do, and how does it differ from the WHERE clause?

Answer: The HAVING clause filters groups after the aggregation has been performed, whereas WHERE filters rows before any grouping is done. HAVING is typically used alongside GROUP BY.

Example:

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

This counts the number of employees in each department and shows only those departments with more than 10 employees.

Summary

  • Sorting: Use the ORDER BY clause for sorting.
  • Filtering: Apply the WHERE clause to filter individual rows and the HAVING clause to filter grouped data.
  • Set Operations: Utilize UNION/UNION ALL for combining result sets, INTERSECT for finding common rows, and EXCEPT for finding rows exclusive to the first query.

You May Like This Related .NET Topic

Login to post a comment.