{"id":22344111,"url":"https://github.com/github-insight-anz-lab/copilot-lab-sql","last_synced_at":"2025-03-26T10:12:31.627Z","repository":{"id":264746651,"uuid":"855557914","full_name":"GitHub-Insight-ANZ-Lab/copilot-lab-sql","owner":"GitHub-Insight-ANZ-Lab","description":"A hands-on lab to introduce people to working with GitHub Copilot and SQL","archived":false,"fork":false,"pushed_at":"2024-11-26T04:34:41.000Z","size":43,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-01-31T11:29:22.261Z","etag":null,"topics":["copilot-tutorial","database","github-copilot","sql"],"latest_commit_sha":null,"homepage":"https://github-insight-anz-lab.github.io/github-copilot-labs-list/","language":"PLpgSQL","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/GitHub-Insight-ANZ-Lab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-09-11T04:17:08.000Z","updated_at":"2024-11-26T04:34:44.000Z","dependencies_parsed_at":"2024-11-26T07:05:27.118Z","dependency_job_id":null,"html_url":"https://github.com/GitHub-Insight-ANZ-Lab/copilot-lab-sql","commit_stats":null,"previous_names":["github-insight-anz-lab/copilot-lab-sql"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GitHub-Insight-ANZ-Lab%2Fcopilot-lab-sql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GitHub-Insight-ANZ-Lab%2Fcopilot-lab-sql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GitHub-Insight-ANZ-Lab%2Fcopilot-lab-sql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GitHub-Insight-ANZ-Lab%2Fcopilot-lab-sql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/GitHub-Insight-ANZ-Lab","download_url":"https://codeload.github.com/GitHub-Insight-ANZ-Lab/copilot-lab-sql/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245632425,"owners_count":20647193,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["copilot-tutorial","database","github-copilot","sql"],"created_at":"2024-12-04T09:08:20.421Z","updated_at":"2025-03-26T10:12:31.621Z","avatar_url":"https://github.com/GitHub-Insight-ANZ-Lab.png","language":"PLpgSQL","readme":"# GitHub Copilot lab for SQL\n\nA hands-on lab to introduce people to working with GitHub Copilot and Structured Query Language (SQL). You do not need to be an experienced SQL developer to complete this lab!\n\n**Proposed duration**: 40-60 minutes (excluding prerequisites setup).\n\n## Prerequisites\n\n- [Visual Studio Code](https://code.visualstudio.com/)**\n- [GitHub Copilot](https://copilot.github.com/) installed and licensed\n- [Markdown Preview Mermaid Support](https://marketplace.visualstudio.com/items?itemName=bierner.markdown-mermaid) extension for Visual Studio Code\n- [SQLTools](https://marketplace.visualstudio.com/items?itemName=mtxr.sqltools)\n- [SQLTools PostgreSQL driver](https://marketplace.visualstudio.com/items?itemName=mtxr.sqltools-driver-pg) (or driver for your selected database server)\n- PostgreSQL database server (or another database server of your choice) with an empty database (or permissions to create a new database).\n\n** You can also consider using [Azure Data Studio](https://learn.microsoft.com/azure-data-studio/download-azure-data-studio) which also supports GitHub Copilot and PostgresSQL via extensions.\n\n### Database server\n\nWhile SQL is a [standardised language](https://en.wikipedia.org/wiki/SQL#Interoperability_and_standardization) that can be used with many different database servers, this lab will use [PostgreSQL](https://www.postgresql.org/) as the database server.\n\nYou can use another database server if you prefer, but you may need to adjust some of the generated SQL commands to work with your selection.\n\nIf you cannot run a local PostgreSQL instance, consider using a cloud-based service like [Azure Database for PostgreSQL](https://learn.microsoft.com/azure/postgresql/flexible-server/how-to-deploy-on-azure-free-account) which offers a free tier (for 12 months) which will suffice for this lab.\n\nYou can complete the labs without access to a server, but you will not be able to run the SQL commands to see the results and validate that the code works as expected.\n\n\u003e [!IMPORTANT]\n\u003e GitHub Copilot Individual and Business works by referencing files held locally on your computer or in a Codespace. At time of writing GitHub Copilot does not support connecting to a database server to obtain additional context such as your database engine vendor and version or any deployed artefacts (schemas, tables, etc). Keep this in mind when working with SQL and GitHub Copilot.\n\n---\n\n### Pre-lab challenge\n\nAll database servers have a default database. For Postgres it's called `postgres`, for MySQL it's called `mysql`, and for SQL Server it's called `master`. Oracle has a default database called `system`.\n\nIt's not recommended to use the default database for your application data, so the for our first \"pre-challenge\" is to create a new database on your database server.\n\nFirst you will need to connect to your database server and select the default database.\n\nNext, open a new SQL file in your editor and use Copilot to generate the SQL command to create a new database and then also grant your user full privileges on the new database.\n\nOnce the database is created you may need to update your connection settings to use the new database and reconnect.\n\nStuck? Check out the [pre-challenge lab help](help/pre-challenge.md) for a hint.\n\n---\n\n## Lab 1: Create related tables\n\nIn the sample-data folder you will find three CSV files:\n\n- [address.csv](sample-data/address.csv) - This file contains a list of addresses with the street, city, state, and postal code.\n- [customer.csv](sample-data/customer.csv) - This file contains a list of customers with their name, email address, and the date they became a customer.\n- [order.csv](sample-data/order.csv) - This file contains a list of orders with the order number, the customer who placed the order, the date the order was placed, and the total amount of the order.\n\nUsing these three files and GitHub Copilot, create the tables required to store this data in your database.\n\nMake sure to create foreign key constraints where appropriate.\n\n\u003e [!TIP]\n\u003e Try using Copilot Chat and file referencing.\n\nOnce you have created the SQL make sure to save the file. Save the script in `lab1.sql`.\n\nEnsure that you have documentation in the SQL file that explains the purpose of each table and the relationships between the tables.\n\nWhile you can bulk insert the data from the CSV files, you don't need to do this for this lab. Use GitHub Copilot to create `INSERT` statements to insert the data from each CSV.\n\n\u003e [!TIP]\n\u003e Did you run into `-- More rows...` or a similar response from Copilot and didn't get the SQL for all the rows? You are likely experiencing the token limit for the large language model that sits behind Copilot. Try clearing your Chat history using `/clear` and trying again.\n\nStuck? Check out the [lab 1 help](help/lab1.md) for hints.\n\n## Lab 2: Building Database Logic\n\nNow we have a database defined, let's create some logic to interact with the data.\n\nYou may be familiar with database views, stored procedures, and triggers. These are all ways to interact with the data in the database.\n\n### Create a view: CustomerOrders\n\nCreate a view called `CustomerOrders` that shows that shows customer orders and which doesn't include the customer's ID. Save the script in `lab2-view.sql`.\n\nOnce you've created the view, ask Copilot to update the definition so it includes the Customer ID field and that orders are grouped by customer.\n\n\u003e [!TIP]\n\u003e If you run into issues, make sure to use GitHub Copilot chat to help identify how to fix them. You should be able to create and update the view without needing to write the SQL from scratch.\n\n### Create a stored procedure or function: GetTotalOrdersForCustomer\n\nBefore you implement a solution for this brief, ask GitHub Copilot Chat what the differences are between a stored procedure and a function. Save the script in `lab2-sproc.sql`.\n\nOnce you understand the differences, create a solution for these scenarios:\n\n- Provide a way to get the total number of orders for a customer.\n- Provide a way to get the total value of orders for a customer.\n\nOnce you've created the stored procedure or function `GetTotalOrdersForCustomer`, see if you can optimise the resulting SQL. What changes does Copilot suggest? Are they valid?\n\nStuck? Check out the [lab 2 help](help/lab2.md) for hints.\n\n## Lab 3: Debugging SQL\n\nIn this lab, you will be given a a series of SQL-related problems and your task is to use GitHub Copilot to identify the issues and fix them.\n\n### Fixing a query\n\nThe SQL file is called [lab3-query.sql](sample-data/lab3-query.sql) and is located in the `sample-data` folder. The query is designed to run against a database that has had the tables created in Lab 1. The query is shown below.\n\n```sql\nSELECT C.Firstname, A.AddressLine1, A.AddressLine2, A.City, A.State, A.ZipCode\nFROM Customer AS C\nJOIN Address AS A ON CustomerID = A.CustomerID\nWHERE A.IsShippingAddress = TRUE AND A.IsBillingAddress = FALSE;\nORDER BY C.Firstname\nGROUP BY A.City \n```\n\nFirst up, try opening the SQL file and executing the query. If it doesn't run as expected, use GitHub Copilot to help identify the issue and fix it. There may be more than one problem with the query. Make sure that any solution you provide includes both an `ORDER BY` and a `GROUP BY` clause. Save the updated script in `lab3-fixedquery.sql`.\n\n\u003e [!TIP]\n\u003e Depending on the database server you are using you might need to copy/paste any errors you receive into the chat to get help from Copilot. If you are using the command-line you could try out the `@terminal` agent.\n\n### Modifying the Customer table\n\nIn order to complete this next lab step, you will need to modify the `Customer` table to include a `EmailAddress` field. Use GitHub Copilot to generate the SQL to add this field to the table and to set the default to `NULL`. This field should hold case insensitive text. If your database server does not support case insensitive text, use a `VARCHAR(254)` field instead. Save the script in `lab3-newfield.sql`.\n\nMake sure to update your `lab1.sql` file (or copy from `help\\lab1-solution.sql`) with the updated `Customer` table definition so you can use it in future exercises. Save the updated file as `lab3-schema.sql`.\n\n### Fixing a stored procedure\n\nThe below stored procedure runs fine but has some issues that have caused odd behaviour in the application using it. You can find a copy of this stored procedure in the [lab3-sproc.sql](sample-data/lab3-sproc.sql) file located in the `sample-data` folder. The issues highlighted are:\n\n- Any data can be inserted into the `EmailAddress` field without validation.\n- If a non-existent customer ID is passed, the provided data is lost and no error is returned.\n\n```sql\nCREATE OR REPLACE FUNCTION update_existing_customer_email(customer_id UUID, new_email CITEXT)\nRETURNS VOID AS $$\nBEGIN\n    -- Update the customer's email address\n    UPDATE Customer\n    SET EmailAddress = new_email\n    WHERE CustomerID = customer_id;\nEND;\n$$ LANGUAGE plpgsql;\n```\n\nYou can test out this stored procedure by running the below SQL:\n\n```sql\n-- No error is returned\nSELECT update_customer_email('7f1cf3dd-2281-41ec-8847-bab8411dbc6d', 'myemailaddress');\n-- The data provided does not appear in the table\nSELECT * from Customer;\n```\n\nUsing GitHub Copilot, address the two highlighted issues and then validate the stored procedure works as expected by attempting to do below. Save the updated script in `lab3-fixedsproc.sql`.\n\n- Update the email address for a customer that doesn't exist (provide a GUID not listed in the table).\n- Update the email address for a customer with an invalid email address (e.g. `myemailaddress`).\n\nHere's an extra challenge - once you've fixed the stored procedure, see if you can optimise the SQL generated by Copilot. What changes does Copilot suggest? Are they valid?\n\nStuck? Check out the [lab 3 help](help/lab3.md) for hints.\n\n## Lab 4: Understanding SQL\n\n### Generating an Entity Relationship (ER) diagram\n\nIn this lab, you will use GitHub Copilot to generate an ER diagram for the tables you created in Lab 1. The ER diagram should be created as a [Mermaid diagram](https://mermaid.js.org/syntax/entityRelationshipDiagram.html) that can be embedded in a markdown file and should include the table names and field data types (but not the field properties such as size or constraints).\n\nA sample is shown below (you will need the appropriate Mermaid extension for Visual Studio Code to view this locally).\n\n```mermaid\nerDiagram\n    PERSON {\n        int id PK \"Unique identifier\"\n        string name \"Person's name\"\n        string email \"Person's email\"\n    }\n    ADDRESS {\n        int id PK \"Unique identifier\"\n        string street \"Street name\"\n        string city \"City name\"\n        int person_id FK \"References PERSON.id\"\n    }\n    PERSON ||--o{ ADDRESS : \"has\"\n```\n\n### Describing complex SQL\n\nYou will find a series of SQL queries in the [lab4-query.sql](sample-data/lab4-query.sql) file located in the `sample-data` folder.\n\nUse GitHub Copilot to explain what each query does and how it works. You can compare the description from GitHub Copilot against the results in the lab help file. Also ask GitHub Copilot how the resulting data from each query might be used so you can determine the intent of the query, not just how it functions.\n\nCheck out the [lab 4 help](help/lab4.md) for hints and solutions.\n\n## That's a wrap\n\nThanks for completing the GitHub Copilot lab for SQL. We hope you enjoyed working with GitHub Copilot and SQL and that you learned something new. Please check out our list of labs and resources for more hands-on learning experiences on our [GitHub repository](https://github.com/Insight-Services-APAC/github-copilot-labs-list).\n\n[Back to top 👆](#github-copilot-lab-for-sql)","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgithub-insight-anz-lab%2Fcopilot-lab-sql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgithub-insight-anz-lab%2Fcopilot-lab-sql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgithub-insight-anz-lab%2Fcopilot-lab-sql/lists"}