Oracle

Chapter 7 - DQL (Data Query Language)

'>' and '<' Operator (greater than, less than)

The greater than (>) and less than (<) operators in Oracle's Data Query Language (DQL) are used to compare values in SQL queries. These operators are essential when filtering records based on conditions that involve comparisons, such as finding books published after a certain year or memberships that expire before a certain date. They help define the range of data you want to retrieve, making them vital for data querying.

Using > and < in SQL Queries

  1. Basic Syntax

    • The > operator is used to find values greater than a specified value.
    • The < operator is used to find values smaller than a specified value.

    Example:

    SELECT * FROM books WHERE publication_year > 2015;
    
  2. Comparison in SQL

    • These operators are used to compare column values with a constant or another column.
    • For example, to list books published before the year 2000:
      SELECT title, author FROM books WHERE publication_year < 2000;
      
  3. Combining with Other Conditions

    • You can combine > and < with other conditions like AND or OR.
    • Example: To find books published after 2010 but before 2020:
      SELECT title FROM books WHERE publication_year > 2010 AND publication_year < 2020;
      
  4. Using with Different Data Types

    • The operators work not only with numbers but also with dates.
    • For example, to find memberships that expire before a certain date:
      SELECT member_id, expiry_date FROM membership WHERE expiry_date < '2024-12-31';
      

Best Practices

  1. Optimize Queries with Indexes
    • When comparing large datasets, make sure to have indexes on columns used in > or < comparisons. This will improve query performance, especially when querying tables with a high volume of data.
    • Example:
      CREATE INDEX idx_publication_year ON books (publication_year);
      

By following these practices, you can efficiently use > and < operators in Oracle to query and retrieve specific sets of data based on your conditions.

Tansy SQL Course | Greater than and Less than Operator | Chapter 7 | Lesson 13 - Video Thumbnail

TEST CODE

In Oracle, to fetch salaries exceeding 150,000, excluding the amount of 150,000 itself, you can use a subquery to achieve the same result.

SELECT *
FROM (
    SELECT *
    FROM org_employee
    WHERE salary > 150000
) filtered_employees
ORDER BY salary;

In Oracle, to retrieve salaries equal to or greater than 150,000, including the amount of 150,000 itself, you can use a subquery to achieve the same result.

SELECT *
FROM (
    SELECT *
    FROM org_employee
    WHERE salary >= 150000
) AS filtered_employees
ORDER BY salary;

In Oracle, you can use the `TRUNC` function to achieve the same result by truncating the date to the day level and comparing it with the desired date.

SELECT *
FROM act_order
WHERE TRUNC(order_date) >= to_date('15/12/23', 'DD/MM/YY')
ORDER BY order_date;

Example 1:

Let's explore the procedure of extracting information from a designated table using the '>' SQL operator with numeric data. This query retrieves all rows from the employee table where the salary column is greater than 150k.

Example 1 - Raw data from employee table

Example 1 Raw data from employee table

Example 1 - Query

SELECT *
FROM org_employee
WHERE salary > 150000
ORDER BY salary;

Example 1 - Query data mapping

Example 1 Query data mapping

In the provided image, the green color signifies the data that has been selected or satisfies the conditions specified in our query. Data points in red indicate information that does not meet the criteria set by the query. Note that salary 150,000 is not selected.

Example 1 - Query Output

Example 1 Query Output

Example 2:

Let's explore the procedure for extracting information from a designated table using '>=' operator on integer data. This query retrieves all rows from the employee table where the salary column is greater than 150k, including border line salary of 150k.

Example 2 - Raw data from employee table

Example 2 Raw data from employee table

Example 2 - Query

SELECT *
FROM org_employee
WHERE salary >= 150000
ORDER BY salary;

Example 2 - Query data mapping

Example 2 Query data mapping

In the provided image, the green color signifies the data that has been selected or satisfies the conditions specified in our query. Data points in red indicate information that does not meet the criteria set by the query. Note that salary 150,000 is also selected, when compared to above query where 150,000 was not selected.

Example 2 - Query Output

Example 2 Query Output

Example 3:

Let's explore the procedure for extracting information from a designated table using the '>=' SQL operator on date time data. This query retrieves all rows from the orders table where the order date is greater than or equal to '2023-12-15'

Example 3 - Raw data from order table

Example 3 Raw data from order table

Example 3 - Query

SELECT *
FROM act_order
WHERE order_date >= '2023-12-15'
ORDER BY order_date;

Example 3 - Query data mapping

Example 3 Query data mapping

In the provided image, the green color signifies the data that has been selected or satisfies the conditions specified in our query. Data points in red indicate information that does not meet the criteria set by the query.

Example 3 - Query Output

Example 3 Query Output
Comments(0 comments)

Comments Not Found