Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Azure icon
Labs

Using SQL to Retrieve Data

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, you 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.

Azure icon
Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 1h 0m
Published
Clock icon Sep 20, 2019

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Log In to the Azure Portal

    1. Log in to the Azure Portal using the provided credentials.
  2. Challenge

    Create a SQL Database

    1. Click on the three-line menu button at the top left of the page, and click Create a resource.
    2. Click on the Databases category on the left, and click 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. For the Authentication method, choose Use SQL authentication.
    7. Assign a Server name.
    8. Assign an Admin login.
    9. Assign a Password, following the requirements.
    10. Choose the same location as you lab provided resource group.
    11. Click OK.
    12. Leave Elastic Pool at No.
    13. Click Configure database.
    14. Click the area for Basic.
    15. Click Locally-redundant backup storage
    16. Click Apply.
    17. Click Next: Additional Settings.
    18. Under Use existing data click Sample.
    19. Click Review + create.
    20. 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:

    1. Click Go to resource.
    2. Click Set server firewall at the top.
    3. Click Add client IP.
    4. Add the public IP address (it may autofill).
    5. Click Save.
  3. Challenge

    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.

    1. If we want to avoid potential connection issues and use a simple editor, click Query editor which is currently in preview.
    2. If you'd rather use a local client, follow these steps (for the purposes of this example, we are going to assume Visual Studio Code is being used.)
      1. Download and install Visual Studio Code.(https://code.visualstudio.com/)
      2. Install the MSSQL extension.
        1. Click on the Extensions icon on the far left-hand side.
        2. Search for MSSQL, click on it, and then click Install.
    3. Search for MSSQL, click on it, and then click Install.
    4. Open a new window, and change the type by clicking on Plain Text at the bottom right and choosing SQL.
    5. In the same area, click Disconnected.
    6. In the top pane that pops up choose Create Connection Profile.
    7. Copy/paste the Server name from the database Overview page and hit Enter.
    8. Type or copy/paste the Database name and hit Enter.
    9. Choose SQL Login and hit Enter.
    10. Enter the Admin login that was specified earlier and hit Enter.
    11. Enter the Password that was specified earlier and hit Enter.
    12. Choose whether or not to save the password and hit Enter.
    13. 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.

  4. Challenge

    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
    
  5. Challenge

    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 t.name as TableName, s.name as SchemaName
    FROM sys.tables t
    INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
    
  6. Challenge

    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'
    
  7. Challenge

    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
    

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans