Northwind Database Sql Queries

  1. Northwind Database Script Sql
  2. Northwind Database Sql Queries Examples
  3. Northwind Database Sql Queries List

SQL is a standard language for storing, manipulating and retrieving data in databases. Our SQL tutorial will teach you how to use SQL in: MySQL, SQL Server, MS Access, Oracle, Sybase, Informix, Postgres, and other database systems.

  • Northwind Database Assignment Query 9: Using JOIN and Aggregate Function.
  • That is what the subquery does. 'a' refers to the main query that I outlined above. 'b' refers to the new subquery that we created. For each record in 'a' the query will fetch the product name with highest sales from 'b' The joins are used only for linking the data of sales person. So the data of 'a' will be linked to the data of 'b'.
  • Northwind Database Querying with Linq 1. Give the name, address, city, and region of employees. Give the name, address, city, and region of employees living in USA 3. Give the name, address, city, and region of employees older than 50 years old 4. Give the name, address, city, and region of employees that have placed orders to be delivered.
  • Northwind and pubs sample databases for Microsoft SQL Server Before you can use the Northwind database, you have to run the downloaded instnwnd.sql script file to recreate the database on an instance of SQL Server by using SQL Server Management Studio or a similar tool. Follow the instructions in the Readme file in the repository.

This is part 2 of the tutorial series - converting the popular Microsoft Access Northwind database queries toMySQL queries. These queries are originated from Access Northwind Traders application. Some of them are relatively complex aggregated queries with sub-queries.

6. Order Details Extended

This query calculates sales price for each order after discount is applied.

Here is the query result. 2,155 records returned.

7. Sales by Category

For each category, we get the list of products sold and the total sales amount.Note that, in the second query, the inner query for table c is to get sales for each product on each order. It then joins with outer query on Product_ID. In the outer query, products are grouped for each category.

Northwind

Here is the query result. 77 records returned.

Database

8. Ten Most Expensive Products

Northwind Database Sql Queries

The two queries below return the same result. It demonstrates how MySQL limits the number of records returned.

The first query uses correlated sub-query to get the top 10 most expensive products.

The second query retrieves data from an ordered sub-query table and then the keyword LIMIT is used outside the sub-query to restrict the number of rows returned.

Here is the query result. 10 records returned.

9. Products by Category

This is a simple query just because it's in Access Northwind so we converted it here in MySQL.

Here is the query result. 69 records returned.

10. Customers and Suppliers by City

This query shows how to use UNION to merge Customers and Suppliers into one result set by identifying them as having different relationships to Northwind Traders - Customers and Suppliers.

Here is the query result. 120 records returned.


Other tutorials in this category

1. MySQL Northwind Queries - Part 1
2. MySQL Northwind Queries - Part 3
3. How to Work with Two Unrelated Values
4. How to Fill Gaps in Sales Data
5. How to Calculate Totals, Subtotals and Grand Total
6. How to Work with NULL Values
7. How to fill down empty cells with values from a previous non-empty row
8. Use RANK function to update a previous record within a group or partition
9. Two ways to add a unique number or ID to each row
10. 3 ways to get Top N rows from MySQL
11. How to generate Cumulative Sum (running total) by MySQL - Part 1

In many examples, you use the Northwind sample database. So you need to install it in your SQL Server instance. You can download the Northwind database scripts from its GitHub repository at https://github.com/Microsoft/sql-server-samples/tree/master/samples/databases/northwind-pubs. Specifically, the instnwnd.sql file contains scripts necessary for creating the Northwind database.

Read more : Get started with ASP.NET Core MVC 3.1

Using SQL Server Management Studio

Once you have the database script, you can run it using SQL Server Management Studio .

Step 1 : In SQL Server Management Studio, connect to an instance of the SQL Server Database Engine.

Northwind Database Script Sql

Step 2 : Open the script in a new query window

Step 3 : Run the script

Step 4 : Click the “! Execute” button

Sql

Step 5 : After a successful execution of the Query, you should find your newly populated database.

Step 6 :Adding a Countries table

The Employees table contains a Country column that stores an employee’s country. Therefore, on the data entry pages, you need to accept a country from the end user. Instead of accepting the user input in a textbox, it would be nice to display a list of countries to choose from. This requires another table, Countries, that contains a list of countries. The default installation of Northwind doesn’t contain such a table, and hence you need to add one.

Northwind Database Sql Queries Examples

The Countries table contains just two columns: CountryID and Name. The CountryID is an integer identity column, whereas Name is a varchar column with length of 80 characters.

Northwind Database Sql Queries List

Make sure to add a few countries in the Countries table so that you can use it in Employee Manager.