Pagination Queries in Oracle: Analytics, Row Limiting Clause

Pagination Queries in Oracle OFFSET FETCH

Pagination is the process of dividing query results (typically on a user interface) into discrete pages, where each page contains a smaller/more manageable number of rows. In classic offset pagination, each page shows a fixed count of rows (say N): if N is 20, then the first page shows 1-20 rows, a “Next” navigation moves control to the second page with 21-40 rows, and so on.

This article demonstrates how to write queries to achieve this style of pagination in Oracle.

Read more

REGEX to Split a Comma-Separated String into Rows

REGEX to Split String to Rows

A typical scenario that involves splitting a comma-separated string into rows:

  • in a UI-driven query, the user would enter a list of IDs into an input box, and
  • the application should retrieve details for the input list of IDs

Search Input boxThe count of IDs in the list can vary — the user may enter a single value or multiple, and the underlying SQL should fetch the details for all of them.

This sounds simple enough on the face of it: bind a parameter to a comma-separated list of values such as ‘A1,A2,A4’ and then look for corresponding rows using SQL for those IDs.

There’s a catch though.

Read more

JSON Conditionals: JSON_EXISTS, JSON_TEXTCONTAINS

JSON Conditional Logic
In the last two articles, we saw the means and reasons for storing JSON data in Oracle and ways of retrieving JSON data from Oracle. In this article, we will explore ways of implementing true/false tests on JSON data using conditionals: JSON_EXISTS, JSON_TEXTCONTAINS.

JSON Conditional Logic JSON_EXISTS JSON_TEXTCONTAINS

JSON conditionals check for the existence of specified paths/values within JSON documents. They are typically applied as row filters in the SQL WHERE clause.

Read more

SQL: Compare Two Tables for Differences in Data

SQL Find Differences between Two Tables

An approach for comparing two tables for differences in data, using a pure SQL solution:

In a recent project, a shadow table had to be compared periodically with its main source table to identify the differences between the two tables.

The nature of differences fell into one of these buckets:

  1. Insert Required [INS]: If the source had rows not present in the shadow table
  2. Update Required [UPD]: If one or more columns in a row (identified by a primary key) had changed values
  3. Delete Required [DEL]: If the shadow table had rows not present in the source table

Comparing two tables for differences (INS/UPD/DEL) needed a different solution from a simple MERGE SQL statement. Here an upsert was not to be executed, only the nature of differences to be identified.

Read more

DATE Format SQL for Dates Stored as Strings

Convert-VARCHAR2-to-DATE.jpg

It is well-acknowledged that attributes in the database should use the correct datatypes (numbers should go into NUMBER columns, dates into DATE columns, etc). Storing date values in VARCHAR2 columns is an open invitation for bugs and issues due to date format such as the one I’m about to describe. Besides, using string-type columns to represent dates rules out your ability to build upon features like range interval partitioning. Yes – ideally you should never have to convert VARCHAR2 to date.

It is equally well-acknowledged that we do not live in an ideal world. In some situations, we do not control the database design; we have to live with what exists and provide a solution to the problem.

This article addresses one such problem — how to compare two dates when they are stored as strings with different DATE formats.

Read more