Skip to main content

Using SQL to Retrieve Data

Hands-On Lab


Photo of Landon Fowler

Landon Fowler

Database Training Architect II





SQL is a powerful language for querying, changing, and deleting data. Almost every discipline in IT will encounter SQL queries at some point in their work. Being familiar with how to use it effectively can help you achieve greater success in your current role and possibly even set you up for a move to another. In this hands-on lab we are going to work with various methods of selecting data. This will help you get an understanding of how to extract useful information from databases.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Using SQL to Retrieve Data


In this hands-on lab, we are going to work with various methods of selecting data.


Log in to the Azure Portal with the provided username and password in the Credentials section of the Hands-On Lab page.

Create a SQL Database

  1. On the Home page, click SQL databases.

  2. Click Create sql database.

  3. Under Resource group, do not click Create new and name it because the lab already provides one for us to select.

  4. Assign a Database name e.g. sql_lab.

  5. Under Server click Create new.

  6. Assign a Server name.

  7. Assign an Admin login.

  8. Assign a Password, following the requirements.

  9. Choose the nearest Location.

  10. Check the box to allow Azure services to connect.

  11. Click OK.

  12. Leave Elastic Pool at No.

  13. Click Configure database.

  14. Click the area for Basic.

  15. Click Apply.

  16. Click Next: Additional Settings.

  17. Under Use existing data click Sample.

  18. Click Review + create.

  19. Double-check everything and click Create.

    After a short time, we will have a fully functional SQL database preloaded with data that's ready to go!

    Note: If we will be connecting from our client machine rather than the Azure Query Editor, take these additional steps:

  20. Click Go to resource.

  21. Click Set server firewall at the top.

  22. Click Add client IP.

  23. Add the public IP address (it may autofill).

  24. Click Save.

Connect Your Client

  1. Go to the Overview page for the SQL database.

    Note: We can return to the page also by clicking SQL databases in the far left pane, and then on the database name. Or we can use the breadcrumb menu at the top.

  2. If we want to avoid potential connection issues and use a simple editor, click Query editor which is currently in preview.

  3. Connect the local client (for the purposes of this example, we are going to assume Visual Studio Code is being used.)

  4. Download and install Visual Studio Code.(

  5. Install the MSSQL extension.

  6. Click on the Extensions icon on the far left-hand side.

  7. Search for MSSQL, click on it, and then click Install.

  8. Open a new window, and change the type by clicking on Plain Text at the bottom right and choosing SQL.

  9. In the same area, click Disconnected.

  10. In the top pane that pops up choose Create Connection Profile.

  11. Copy/paste the Server name from the database Overview page and hit Enter.

  12. Type or copy/paste the Database name and hit Enter.

  13. Choose SQL Login and hit Enter.

  14. Enter the Admin login that was specified earlier and hit Enter.

  15. Enter the Password that was specified earlier and hit Enter.

  16. Choose whether or not to save the password and hit Enter.

  17. Type a Profile Name and hit Enter.

    Note: In the bottom right, we should see a message saying that the profile has been created and indicating our connected status.

    Now let's have some fun!

List the Database Tables

  1. Let's start by finding out what tables are in our database to work with. Use the query below to list those tables from a system view:

    SELECT name FROM sys.tables

Use a Join to Show the Tables' Schema

  1. Without knowledge of which schema a table belongs to we cannot properly query it. Use the query below to associate the table name with its schema name:

    SELECT as TableName, as SchemaName
    FROM sys.tables t
    INNER JOIN sys.schemas s ON t.schema_id = s.schema_id

List Silver Bicycles

  1. Looking at the tables we can see that there is an associated ProductCategory table. Use the query below to pull a list of products that filters via the correct category ID for bicycles and the color Silver:

    SELECT *
    FROM SalesLT.Product p
    LEFT JOIN SalesLT.ProductCategory c ON p.ProductCategoryID = c.ProductCategoryID
    WHERE c.ProductCategoryID = 5
    AND p.Color = 'Silver'

Narrow Down the List to Items Under $1000 and Sort by Least to Most Expensive

  1. We will expand on the last query by adding additional WHERE conditions and an ORDER BY. Use the following:

    SELECT *
    FROM SalesLT.Product p
    LEFT JOIN SalesLT.ProductCategory c ON p.ProductCategoryID = c.ProductCategoryID
    WHERE c.ProductCategoryID = 5
    AND p.Color = 'Silver'
    AND p.ListPrice < 1000
    ORDER BY p.ListPrice ASC


Congratulations — you've completed this hands-on lab!