Custom LabKey queries are written in LabKey SQL. They can be created via the web UI or provided by a module. If created in the web UI, the query SQL and meta data is stored in the database in table query.QueryDef. In either case, at execution time the SQL is parsed, verified (for syntax, authorization, etc.), and translated into native database SQL on the fly.
More details:
https://www.labkey.org/Documentation/wiki-page.view?name=customSQL
Adam