Parsing SQL SELECT query using JSQLParser

By | 18th July 2019

Update: A new version of this article is available here

In the other article, we saw how to parse an insert query and modify the objects in the query with the help of the JSQL Parser. In this article, we will see how to use the JSQL Parser for parsing a simple select query with the basic condition and multiple select queries.

Imagine a scenario where you are creating the SQL query dynamically in your application for your users. Suppose, if you have to add or remove additional columns based on the case then you may have to hardcode that in your application.

Instead, you can use the JSQL Parser to parse the Select query and add or remove columns based on the need.

Maven dependency

You need to create a new maven project and add the following dependency in your pom.xml file to access this parsing library


Java code to parse SELECT query

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.util.SelectUtils;

import java.util.List;

public class SelectSqlParser {

    public static void main(String[] args) {

        System.out.println("Program to parse SELECT sql statement");
        String selectSQL = "Select id, name, location from Database.UserTable " +
                "where created_dt >= current_date- 180";

        try {
            Statement select = (Statement) CCJSqlParserUtil.parse(selectSQL);
            //Simple Select query parsing
            System.out.println("Simple single select with where condition\n");

            System.out.println("List of  columns in select query");
            List<SelectItem> selectCols = ((PlainSelect) ((Select) select).getSelectBody()).getSelectItems();

            for (SelectItem selectItem : selectCols)

            System.out.println("Where condition: " + ((PlainSelect) ((Select) select).getSelectBody()).getWhere().toString());

            SelectUtils.addExpression((Select) select, new Column("newColumnName"));
            System.out.println("\nModified select with additional column");

            ((Table) ((PlainSelect) ((Select) select).getSelectBody()).getFromItem()).setName("NewSourceTable");
            ((Table) ((PlainSelect) ((Select) select).getSelectBody()).getFromItem()).setSchemaName("NewSourceTable");

            System.out.println("\nModified select with new table and database");

            selectSQL = "Select,, w.location from Database.WebLogs w " +
                    "union Select,, m.location from Database.MobileLogs m ";

            Statement newSQL = (Statement) CCJSqlParserUtil.parse(selectSQL);
            List<SelectBody> selectList = ((SetOperationList) ((Select) newSQL).getSelectBody()).getSelects();
            System.out.println("\nListing all selects from the query");
            for (SelectBody selectBody : selectList)

        } catch (JSQLParserException e) {

Expected output

Program to parse SELECT sql statement
Simple single select with where condition

List of  columns in select query
Where condition: created_dt >= current_date - 180

Modified select with additional column
SELECT id, name, location, newColumnName FROM Database.UserTable WHERE created_dt >= current_date - 180

Modified select with new table and database
SELECT id, name, location, newColumnName FROM NewSourceTable.NewSourceTable WHERE created_dt >= current_date - 180

Listing all selects from the query
SELECT,, w.location FROM Database.WebLogs w
SELECT,, m.location FROM Database.MobileLogs m

Also check “Parsing INSERT sql using jsqlparser“,”Parsing CREATE sql using jsqlparser