{"meta":{"title":"Running CodeQL queries","intro":"You can run queries on CodeQL databases and view the results in Visual Studio Code.","product":"Security and code quality","breadcrumbs":[{"href":"/en/code-security","title":"Security and code quality"},{"href":"/en/code-security/how-tos","title":"How-tos"},{"href":"/en/code-security/how-tos/find-and-fix-code-vulnerabilities","title":"Find and fix code vulnerabilities"},{"href":"/en/code-security/how-tos/find-and-fix-code-vulnerabilities/scan-from-vs-code","title":"Scan from VS Code"},{"href":"/en/code-security/how-tos/find-and-fix-code-vulnerabilities/scan-from-vs-code/running-codeql-queries","title":"Run CodeQL queries"}],"documentType":"article"},"body":"# Running CodeQL queries\n\nYou can run queries on CodeQL databases and view the results in Visual Studio Code.\n\n## About running CodeQL queries\n\nThe [`github/codeql`](https://github.com/github/codeql) repository contains a large number of example queries. You can access any existing queries in your workspace through the \"Queries\" view.\n\n### Prerequisites\n\nTo analyze a codebase, you run queries against a CodeQL database extracted from the code, so you'll need to select a database to work with in the extension. You can select a database locally (from a ZIP archive or an unarchived folder), from a public URL, or from a project's URL on GitHub.com. For more information, see [Managing CodeQL databases](/en/code-security/codeql-for-vs-code/getting-started-with-codeql-for-vs-code/managing-codeql-databases).\n\n## Running a single query\n\n1. In the sidebar, open the \"Queries\" view.\n\n2. To run a query against the selected database, hover over the desired query, then click the **Run local query** icon.\n\n![Screenshot of the \"Queries\" view, with the \"Run local query\" button outlined in dark orange.](/assets/images/help/security/codeql-for-vs-code-running-a-query.png)\n\nThe CodeQL extension runs the query on the current database and reports progress in the bottom-right corner of the application. When the results are ready, they're displayed in the CodeQL \"Query Results\" view.\n\nIf there are any problems running a query, a notification is displayed in the bottom right corner of the application. In addition to the error message, the notification includes details of how to fix the problem.\n\n## Running all queries in a directory\n\nYou can run every query in a directory.\n\n1. In the sidebar, open the \"Queries\" view.\n\n2. Hover over the desired directory of queries, then click the **Run local queries** icon.\n\n## Running a selection of queries\n\nYou can run multiple queries with a single command.\n\n1. Go to the File Explorer.\n\n2. Select multiple files or folders that contain queries.\n\n3. Right-click and select **CodeQL: Run Queries in Selected Files**.\n\n## Running a query without any setup\n\nWhen working on a new query, you can open a \"Quick Query\" tab to easily execute your code and view the results, without having to save a `.ql` file in your workspace. Select **CodeQL: Quick Query** from the VS Code Command Palette, then to run the query use **CodeQL: Run Query on Selected Database**.\n\nYou can see all quick queries that you've run in the current session in the \"Query History\" view. Click an entry to see the exact text of the quick query that produced the results. For more information, see [Viewing your query history](#viewing-your-query-history).\n\nOnce you're happy with your quick query, you should save it in a CodeQL pack so you can access it later. For more information, see [Customizing analysis with CodeQL packs](/en/code-security/codeql-cli/codeql-cli-reference/about-codeql-packs).\n\n## Running a specific part of a query or library\n\nThis can be helpful if you're debugging a query or library and you want to locate the part that is wrong.\n\nInstead of using **CodeQL: Run Query on Selected Database** to run the whole query (the [select clause](https://codeql.github.com/docs/ql-language-reference/queries/#select-clauses) and any [query predicates](https://codeql.github.com/docs/ql-language-reference/queries/#query-predicates)), you can use **CodeQL: Quick Evaluation** to run a specific part of a `.ql` or `.qll` file.\n\n**CodeQL: Quick Evaluation** evaluates a code snippet that you have selected, instead of the whole query, and displays results of that selection in the \"Results\" view.\n\nPossible targets for quick evaluation include:\n\n* Selecting the name of a CodeQL entity (such as a [class](https://codeql.github.com/docs/ql-language-reference/types/#classes) or [predicate](https://codeql.github.com/docs/ql-language-reference/predicates/#predicates)) to evaluate that entity.\n\n* Selecting a [formula](https://codeql.github.com/docs/ql-language-reference/formulas/#formulas) or [expression](https://codeql.github.com/docs/ql-language-reference/expressions/#expressions) with free variables to evaluate that formula or expression.\n\nFor example, in the following snippet, you could select the predicate name `foo` or the formula `s = \"bar\"` for quick evaluation:\n\n```bash\npredicate foo(string s) { s = \"bar\" }\n```\n\n## Running a query on multiple databases\n\nThis can be helpful if you want to test your query on multiple codebases, or find a vulnerability in multiple projects.\n\n1. Open a query (`.ql`) file.\n\n2. Right-click and select **CodeQL: Run Query on Multiple Databases**.\n\n3. From the dropdown menu, select the databases that you want to run the query on.\n\n## Viewing your query history\n\nTo see the queries that you have run in the current session, open the \"Query History\" view.\n\nThe \"Query History\" view contains information including the date and time when the query was run, the name of the query, the database on which it was run, and how long it took to run the query:\n\n* To customize the information that is displayed, right-click an entry and select **Rename**.\n\n* Optionally, filter the view by language using the language selector. For more information, see [Filtering databases and queries by language](/en/code-security/codeql-for-vs-code/getting-started-with-codeql-for-vs-code/managing-codeql-databases#filtering-databases-and-queries-by-language).\n\n* Click an entry to display the corresponding results, and double-click to display the query itself in the editor (or right-click and select **View Query**).\n\n* To display the exact text that produced the results for a particular entry, right-click it and select **View Query Text**. This can differ from **View Query**, as the query file may have been modified since you last ran it.\n\n* To remove queries from the view, select all the queries you want to remove, then right-click and select **Delete**.\n\n## Understanding your query results\n\n1. Click a query in the \"Query History\" view to display its results in the \"Results\" view.\n\n   > \\[!NOTE]\n   > Depending on the query, you can also choose different views such as CSV, [CodeQL CLI SARIF output](/en/code-security/codeql-cli/codeql-cli-reference/sarif-output), or [DIL format](https://codeql.github.com/docs/codeql-overview/codeql-glossary/#dil). For example, to view the DIL format, right-click a result and select **View DIL**. The available output views are determined by the format and the metadata of the query. For more information, see [CodeQL queries](https://codeql.github.com/docs/writing-codeql-queries/codeql-queries/#codeql-queries).\n\n2. Use the dropdown menu in the \"Results\" view to choose which results to display, and in what form to display them, such as a formatted alert message or a table of raw results.\n\n3. To sort the results by the entries in a particular column, click the column header.\n\nIf a result links to a source code element, you can click it to display it in the source.\n\nTo use standard code navigation features in the source code, you can right-click an element and use the commands **Go to Definition** or **Go to References**. This runs a CodeQL query over the active file, which may take a few seconds. This query needs to run once for every file, so any additional references from the same file will be fast.\n\n> \\[!NOTE]\n> If you're using an older database, code navigation commands such as **Go to Definition** and **Go to References** may not work. To use code navigation, try unzipping the database and running `codeql database cleanup <database>` on the unzipped database using the CodeQL CLI. Then, re-add the database to Visual Studio Code. For more information, see [database cleanup](/en/code-security/codeql-cli/codeql-cli-manual/database-cleanup).\n\n### Comparing query results\n\nWhen you're writing or debugging a query, it's useful to see how your changes affect the results. You can compare two sets of results to see exactly what has changed. To compare results, the two queries must be run on the same database.\n\n1. Right-click a query in the \"Query History\" view and select **Compare Results**.\n\n2. A Quick Pick menu shows all valid queries to compare with. Select a query.\n\n3. The \"Compare\" view shows the differences in the results of the two queries.\n\n## Troubleshooting\n\nTo see the logs from running a particular query, right-click the query in the \"Query History\" view and select **Show Query Log**. If the log file is too large for the extension to open in VS Code, the file will be displayed in your file explorer so you can open it with an external program.\n\nFor details about compiling and running queries, as well as information about database upgrades, check the CodeQL Query Server log. For more information, see [Accessing logs for CodeQL in Visual Studio Code](/en/code-security/codeql-for-vs-code/troubleshooting-codeql-for-vs-code/accessing-logs).\n\nBy default, the extension deletes logs after each workspace session. To override this behavior, you can specify a custom directory for query server logs. For more information, see [Customizing settings](/en/code-security/codeql-for-vs-code/using-the-advanced-functionality-of-the-codeql-for-vs-code-extension/customizing-settings).\n\nYou can use the **CodeQL: Restart Query Server** command to restart the query server. This restarts the server without affecting your CodeQL session history. You are most likely to need to restart the query server if you make external changes to files that the extension is using. For example, regenerating a CodeQL database that’s open in VS Code. In addition to problems in the log, you might also see: errors in code highlighting, incorrect results totals, or duplicate notifications that a query is running.\n\n## Next steps\n\nYou can optionally use the extension to create your own custom queries. For more information, see [Creating a custom query](/en/code-security/codeql-for-vs-code/using-the-advanced-functionality-of-the-codeql-for-vs-code-extension/creating-a-custom-query).\n\nFor information on running analysis at scale across many CodeQL databases, see [Running CodeQL queries at scale with multi-repository variant analysis](/en/code-security/codeql-for-vs-code/getting-started-with-codeql-for-vs-code/running-codeql-queries-at-scale-with-multi-repository-variant-analysis)."}