A CGI script is a program, no more and no less. It can specify any logic a ``conventional'' program can. In our example, the CGI script uses ``CGI'' as a keyword and search for it in a database that catalogs all the merchandise sold by Borders.
As a CGI script receives data from a database, the script needs to process that data, and somehow transform the raw data into HTML elements (such as tables, buttons, bitmaps and etc.). A CGI script sequentially outputs (prints) the HTML elements so that the output eventually becomes an HTML document.