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.
Successfully complete this lab by achieving the following learning objectives:
- Log In to the Azure Portal
- Log in to the Azure Portal using the provided credentials.
- Create a SQL Database
Click on the three-line menu button at the top left of the page, and click Create a resource.
Click on the Databases category on the left, and click SQL Database.
Under Resource group, do not click Create new and name it because the lab already provides one for us to select.
Assign a Database name e.g. sql_lab.
Under Server click Create new.
Assign a Server name.
Assign an Admin login.
Assign a Password, following the requirements.
Under Location, choose (US) West US.
Leave Elastic Pool at No.
Click Configure database.
Click the area for Basic.
Click Next: Additional Settings.
Under Use existing data click Sample.
Click Review + create.
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:
Click Go to resource.
Click Set server firewall at the top.
Click Add client IP.
Add the public IP address (it may autofill).
- Connect Your Client
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.
If we want to avoid potential connection issues and use a simple editor, click Query editor which is currently in preview.
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.)
- Download and install Visual Studio Code.(https://code.visualstudio.com/)
- Install the MSSQL extension.
- Click on the Extensions icon on the far left-hand side.
- Search for MSSQL, click on it, and then click Install.
Search for MSSQL, click on it, and then click Install.
Open a new window, and change the type by clicking on Plain Text at the bottom right and choosing SQL.
In the same area, click Disconnected.
In the top pane that pops up choose Create Connection Profile.
Copy/paste the Server name from the database Overview page and hit Enter.
Type or copy/paste the Database name and hit
Choose SQL Login and hit Enter.
Enter the Admin login that was specified earlier and hit Enter.
Enter the Password that was specified earlier and hit Enter.
Choose whether or not to save the password and hit Enter.
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.
- List the Database Tables
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
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
- List Silver Bicycles
Looking at the tables we can see that there is an associated
ProductCategorytable. Use the query below to pull a list of products that filters via the correct category ID for
bicyclesand the color
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
We will expand on the last query by adding additional
WHEREconditions 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