# Create an Index for a SQLite Database

SQL language cannot be written directly in a script, therefore, to create an index, a Query needs to be created and then called in the OnStartup script. For example, if there is a Table that stores results: inspection time, pass/fail and the expected name of the product type. Generally, the most recent results are the primary concern, along with the ability to filter by the product type name and whether or not it passed or failed.

1. Add a new Query for the Table, called "CreateIndex".

   Note: For more information about adding a query, see the Querying a Database topic.

2. Add the following to the Query Text field:

   CREATE INDEX IF NOT EXISTS IDX_ReplayResults ON ReplayResults(InspectionTime DESC, ProductType, ResultCode)

   This SQL statement indicates:

   - If the index called "IDX_ReplayResults" (ReplayResults is the name of the table) does not exist, then create an index called "IDX_ReplayResults".

   - The index should include:

     - The InspectionTime column (a DateTime data type), in descending order, so that when queried for the most recent, it will be displayed first.
     - The ProductType and ResultCode will also be included.