AI-Assisted SQL with GitHub Copilot
Use GitHub Copilot and Copilot in Fabric to write, debug, and optimise T-SQL β understand the security implications, configure instruction files, and get the most from AI-assisted development.
Your AI pair programmer for SQL
Imagine having an expert SQL developer sitting next to you.
You describe what you want in plain English β βcreate a stored procedure that finds duplicate customersβ β and they write the T-SQL for you. You can ask them to explain code, fix bugs, optimise slow queries, or suggest indexes. That is what GitHub Copilot does for SQL development.
But like any assistant, you need to review their work. Copilot can suggest code that looks right but has security flaws, uses deprecated features, or does not follow your companyβs standards. This module teaches you how to use AI tools effectively AND safely.
GitHub Copilot for SQL development
GitHub Copilot provides AI assistance directly in your IDE. For SQL developers, it can:
- Generate T-SQL from natural language descriptions
- Complete code as you type (inline suggestions)
- Explain existing queries line by line
- Debug errors by analysing error messages with context
- Optimise queries by suggesting index strategies or rewrites
Enabling GitHub Copilot
- GitHub account β requires a Copilot subscription (Individual, Business, or Enterprise)
- IDE extension β install the GitHub Copilot extension in VS Code or Visual Studio
- Organisation policy β for Copilot Business/Enterprise, an admin must enable it at the org level
- Sign in β authenticate with your GitHub account in the IDE
Chat vs inline suggestions
| Feature | Inline Suggestions | Copilot Chat |
|---|---|---|
| How it works | Suggests code as you type | Conversational Q&A in a side panel |
| Best for | Writing new code, auto-completing patterns | Explaining code, debugging, asking questions |
| Context | Current file and open tabs | Current file + explicitly attached context |
| Interaction | Tab to accept, Esc to dismiss | Type questions, get explanations |
Copilot in Microsoft Fabric
Fabric integrates Copilot directly into the SQL experience:
- SQL notebooks β write natural language, get T-SQL
- SQL editor β inline suggestions while writing queries
- Data warehouse β AI-assisted query building against Fabric data
Enabling Copilot in Fabric requires:
- Fabric capacity (F64 or higher, or trial)
- Tenant-level admin setting enabled
- User must have appropriate Fabric permissions
Security impact of AI-assisted tools
This is a critical exam topic. AI tools introduce security considerations that developers must understand.
| Risk | What Happens | Mitigation |
|---|---|---|
| Code sent to external service | Your T-SQL and schema context are sent to the AI model endpoint | Review data handling policies; use Copilot Business/Enterprise for IP protection |
| Sensitive data in prompts | Connection strings, passwords, or PII in code context may be sent | Never hardcode secrets; use environment variables and Key Vault references |
| Generated code may be insecure | SQL injection vulnerabilities, missing parameterisation, overly broad permissions | Always review generated code; never deploy AI suggestions without testing |
| Compliance and data residency | Data may cross geographic boundaries during processing | Check Copilot data handling vs your compliance requirements (GDPR, SOC 2) |
| Intellectual property | Generated code may resemble public code or copyrighted patterns | Copilot Business/Enterprise includes IP indemnity; configure content filters |
Exam tip: The security question pattern
The exam often asks: βA developer is using GitHub Copilot to write queries against a production database. What is the MOST significant security concern?β
The answer is usually about sensitive data in context β schema names, table structures, and especially any hardcoded credentials that appear in the code context sent to the AI service. The mitigation is always: use parameterised queries, never hardcode secrets, and review organisational policies on AI tool data handling.
GitHub Copilot instruction files
Instruction files tell Copilot about your projectβs conventions, standards, and constraints. They ensure consistent, compliant code suggestions.
.github/copilot-instructions.md
This file lives in your repository and provides project-wide context:
# Database Coding Standards
## Naming conventions
- Tables: PascalCase singular (Customer, not customers)
- Columns: PascalCase (FirstName, not first_name)
- Stored procedures: usp_ prefix (usp_GetCustomer)
- Views: vw_ prefix (vw_ActiveOrders)
- Indexes: IX_{Table}_{Columns}
## Security requirements
- Always use parameterised queries β never concatenate user input
- Use EXECUTE AS for stored procedures accessing sensitive data
- Apply principle of least privilege in all permission grants
## Performance standards
- Include SET NOCOUNT ON in all stored procedures
- Use SET XACT_ABORT ON for transaction safety
- Prefer inline TVFs over scalar functions
- Always include error handling (TRY...CATCH)
Model and tool options in chat
In Copilot Chat, you can configure:
- Model selection β choose between available models (GPT-4o, Claude, etc.)
- MCP tools β connect to database-aware tools for schema-aware suggestions
- Context attachments β explicitly include files, schemas, or documentation
Scenario: Dev's Copilot setup at PixelForge
Dev Kapoor at PixelForge Studios creates a .github/copilot-instructions.md that includes:
- Naming conventions (all tables use
pf_prefix) - Security rules (all user-facing queries must use parameterisation)
- Performance guidelines (no scalar functions in SELECT lists)
- Compliance notes (GDPR β never log PII in error messages)
Now when any developer on the team uses Copilot, the suggestions automatically follow PixelForgeβs standards. Dev also connects the SQL MCP server so Copilot understands the actual database schema when suggesting queries.
Ingrid at Nordic Shield Insurance is evaluating GitHub Copilot for the database team. The company handles sensitive policyholder data subject to GDPR. Which concern should Ingrid raise FIRST?
Dev at PixelForge wants Copilot to always generate stored procedures with SET NOCOUNT ON, SET XACT_ABORT ON, and TRY...CATCH error handling. What is the best way to enforce this?
π¬ Video coming soon
Next up: MCP: Connecting AI to Your Database β connect AI agents to your SQL Server and Fabric databases using Model Context Protocol.