Edit

Share via


Quickstart: Use the Smart Query Builder (Preview)

In this quickstart, you learn how the Query Building Assistant helps you craft efficient, accurate, and secure queries using either raw SQL or your preferred ORM. Designed for both code-first and data-first developers, it enables faster generation of production-ready logic aligned with your connected database schema.

Get started

Ensure that you're connected to a database and have an active editor window open with the MSSQL extension. This connection allows the @mssql chat participant to understand the context of your database environment, enabling accurate and context-aware suggestions. Without a database connection, the chat participant won't have the schema or data context to provide meaningful responses.

The following examples use the AdventureWorksLT2022 sample database, which you can download from the Microsoft SQL Server Samples and Community Projects home page.

For best results, adjust table and schema names to match your own environment.

Make sure the chat includes the @mssql prefix. For example, type @mssql followed by your question or prompt. This ensures that the chat participant understands you're asking for SQL-related assistance.

Query building

GitHub Copilot supports intelligent query construction directly within Visual Studio Code. From basic SELECTs to complex joins, filters, and aggregations, it generates SQL or ORM queries that follow best practices and reflect your current schema, so you can focus on your application logic.

Here are common use cases and examples of what you can ask via the chat participant:

Time-based analysis

These prompts help analyze trends over time, such as recent sales activity, top performers by period, or comparisons to historical averages. GitHub Copilot can build queries that calculate values relative to your data's most recent dates, avoiding assumptions based on the current system date.

  • Generate a nested query to fetch orders from SalesLT.SalesOrderHeader where the total is above the average order amount for the last six months, relative to the most recent order date in the database (not relative to the current date).
  • Write a query to find the top three customers by total sales in the SalesLT.SalesOrderHeader table, grouped by year.
  • Find the total revenue for each customer in SalesLT.Customer who has placed orders in the last 30 days, relative to the most recent order date in SalesLT.SalesOrderHeader (not relative to the current date).
  • Create a Sequelize query to fetch Customers (SalesLT.Customers) along with their orders (SalesLT.SalesOrderDetail) and total revenue, sorted by descending revenue during the last year in the database (not relative to the current date).

Complex relationships

Use these prompts to generate queries that span multiple related tables. Whether you're joining customer data with order details or building revenue aggregations, GitHub Copilot helps navigate complex relationships using schema context to produce accurate joins and conditions.

  • Using the actual schema of the SalesLT.SalesOrderHeader table, generate a nested SQL query that retrieves orders where the order total is above the average order total for the last six months. The six-month period should be calculated relative to the most recent order date in the table (not the current date).

  • Using my current database, create a SQLAlchemy query to fetch customers along with their orders and total revenue, sorted by descending revenue.

  • Using Prisma, generate a query that joins SalesLT.Customer, SalesLT.SalesOrderHeader, and SalesLT.SalesOrderDetail and calculates total revenue per customer.

  • In Entity Framework, write a LINQ query that returns the top 10 customers by sales in the past year using the SalesLT schema.

  • Write a TypeORM query that finds products that haven't been sold in the last six months. The six-month period should be calculated relative to the most recent order date in the table (not the current date).

  • Write a Django ORM query that retrieves all customers who have made purchases in the last year, sorted by total spending. The "last year" period should be calculated relative to the most recent order date in the table (not the current date).

Business insights

These prompts are designed to surface actionable insights from your data. From identifying churn-risk customers to finding unsold products, GitHub Copilot can help build logic that supports strategic decisions and reporting, tailored to your connected database.

  • Using my current database, generate a list that shows which customers have placed their first order in the last six months, using the most recent order date in the database as the reference point.

  • Using my current database, generate a list that identifies products that haven't been sold in the last 12 months, using the most recent order date in the database as the reference.

  • Identify customers who have placed more than five orders but none in the last 90 days, using the most recent order date in the database as reference.

  • List the top five products with the highest return rate based on order returns or cancellations, calculated relative to the most recent order date.

  • Generate a trend of monthly revenue over the last 12 months based on OrderDate in SalesLT.SalesOrderHeader, using the most recent order date as the anchor.

  • Using SQLAlchemy and Pandas, create a report that identifies customers with declining order frequency over the last three quarters based on the most recent order date.

Feedback: Smart Query Builder

To help us refine and improve GitHub Copilot for the MSSQL extension, use the following GitHub issue template to submit your feedback: GitHub Copilot Feedback

When submitting feedback, consider including:

  • Scenarios tested – Let us know which areas you focused on, for example, schema creation, query generation, security, localization.

  • What worked well – Describe any experiences that felt smooth, helpful, or exceeded your expectations.

  • Issues or bugs – Include any problems, inconsistencies, or confusing behaviors. Screenshots or screen recordings are especially helpful.

  • Suggestions for improvement – Share ideas for improving usability, expanding coverage, or enhancing the GitHub Copilot's responses.