Fast Paced SQL Tutorial for Beginners

Jun 16, 2024


Welcome to this tutorial on mastering basic SQL queries using the Wide World Importers database, a free demo database provided by Microsoft. By the end of this video, you will have a solid understanding of how to perform various SQL operations, including selecting data, filtering results, joining tables, and performing calculations.

Wide World Importers DB: download

Getting Started with SQL Server Management Studio

We begin our journey in SQL Server Management Studio, connected to the local host where the Wide World Importers database is attached. This demo database contains numerous tables, and we’ll focus on the sales.orders table to start with.

Basic SELECT Queries

To retrieve all columns from the sales.orders table, we use:

This query returns every column in the table, including orderID, customerID, salespersonpersonID, and orderdate.

If you want to retrieve only specific columns, you can specify them:

Ordering Results

By default, results may appear in a seemingly ordered fashion due to the order of data entry. To explicitly order results, use the ORDER BY clause:

This query orders the results first by customerID and then by orderdate.

Joining Tables

To find out the names of salespersons, we need to join the sales.orders table with the application.people table:

Here, we use table aliases o and p to simplify our query. This join allows us to match the salespersonpersonID in sales.orders with personID in application.people, retrieving the full name of the salesperson.

Calculating Revenue

To calculate the revenue generated from sales, we need to consider the order lines table, which contains quantity and unitprice:

This query multiplies quantity by unitprice to compute the revenue for each order line.

Filtering Data

To filter results, add a WHERE clause. For example, to show orders from January 1, 2014:

Grouping and Aggregating Data

To aggregate data by orderID and calculate total revenue for each order, use GROUP BY:

This query groups data by orderID and calculates the sum of quantities and revenue for each order.

Conclusion

By following this tutorial, you've learned how to perform essential SQL operations, from selecting and filtering data to joining tables and performing complex calculations. The Wide World Importers database provides a rich set of data to practice these skills, helping you to become proficient in querying databases effectively.

Are you struggling to remember the SQL syntax?

Get FREE SQL Query Cheat Sheet