This repository contains 50 SQL practice queries based on the classic Northwind database. These exercises are designed to help you become proficient in writing and extracting business queries from a database.
- A SQL Database Management System (e.g., MySQL, PostgreSQL, SQL Server, SQLite)
- The Northwind Database installed and ready for querying
| File | Description |
|---|---|
northwind_schema.sql |
Complete database schema with all table definitions |
northwind_data_part1.sql |
Status tables, privileges, shippers, suppliers, employees, customers (~206 records) |
northwind_data_part2.sql |
Products and Orders (~300 records) |
northwind_data_part3.sql |
Order details (500 records) |
northwind_data_part4.sql |
Invoices, purchase orders, purchase order details, inventory transactions (~500 records) |
northwind_data_part5.sql |
Additional inventory transactions (300 records) |
- Create the database and import the schema:
mysql -u your_username -p < northwind_schema.sql- Import the data files in order:
mysql -u your_username -p northwind < northwind_data_part1.sql
mysql -u your_username -p northwind < northwind_data_part2.sql
mysql -u your_username -p northwind < northwind_data_part3.sql
mysql -u your_username -p northwind < northwind_data_part4.sql
mysql -u your_username -p northwind < northwind_data_part5.sqlOr from within MySQL:
USE northwind;
SOURCE northwind_schema.sql;
SOURCE northwind_data_part1.sql;
SOURCE northwind_data_part2.sql;
SOURCE northwind_data_part3.sql;
SOURCE northwind_data_part4.sql;
SOURCE northwind_data_part5.sql;The Northwind database includes the following tables:
customers- Customer informationemployees- Employee recordsemployee_privileges- Employee access privilegesprivileges- Available privilegesproducts- Product catalogsuppliers- Supplier informationshippers- Shipping companiesorders- Customer ordersorder_details- Line items for each orderorders_status- Order status typesorders_tax_status- Tax status typesorder_details_status- Order detail status typesinvoices- Invoice recordspurchase_orders- Purchase orders to supplierspurchase_order_details- Line items for purchase orderspurchase_order_status- Purchase order status typesinventory_transactions- Inventory movement trackinginventory_transaction_types- Transaction type definitions
- Create a report that shows the
CategoryNameandDescriptionfrom thecategoriestable sorted byCategoryName. - Create a report that shows the
ContactName,CompanyName,ContactTitleandPhonenumber from thecustomerstable sorted byPhone. - Create a report that shows the capitalized
FirstNameand capitalizedLastNamerenamed asFirstNameandLastnamerespectively andHireDatefrom theemployeestable sorted from the newest to the oldest employee. - Create a report that shows the top 10
OrderID,OrderDate,ShippedDate,CustomerID,Freightfrom theorderstable sorted byFreightin descending order. - Create a report that shows all the
CustomerIDin lowercase letter and renamed asIDfrom thecustomerstable. - Create a report that shows the
CompanyName,Fax,Phone,Country,HomePagefrom thesupplierstable sorted by theCountryin descending order then byCompanyNamein ascending order. - Create a report that shows
CompanyName,ContactNameof all customers from 'Buenos Aires' only. - Create a report showing
ProductName,UnitPrice,QuantityPerUnitof products that are out of stock. - Create a report showing all the
ContactName,Address,Cityof all customers not from Germany, Mexico, Spain. - Create a report showing
OrderDate,ShippedDate,CustomerID,Freightof all orders placed on 21 May 1996. - Create a report showing
FirstName,LastName,Countryfrom theemployeesnot from United States. - Create a report that shows the
EmployeeID,OrderID,CustomerID,RequiredDate,ShippedDatefrom all orders shipped later than the required date. - Create a report that shows the
City,CompanyName,ContactNameof customers from cities starting with A or B. - Create a report showing all the even numbers of
OrderIDfrom theorderstable. - Create a report that shows all the orders where the freight cost more than $500.
- Create a report that shows the
ProductName,UnitsInStock,UnitsOnOrder,ReorderLevelof all products that are up for reorder. - Create a report that shows the
CompanyName,ContactNamenumber of all customer that have no fax number. - Create a report that shows the
FirstName,LastNameof all employees that do not report to anybody. - Create a report showing all the odd numbers of
OrderIDfrom theorderstable. - Create a report that shows the
CompanyName,ContactName,Faxof all customers that do not have Fax number and sorted byContactName. - Create a report that shows the
City,CompanyName,ContactNameof customers from cities that has letter L in the name sorted byContactName. - Create a report that shows the
FirstName,LastName,BirthDateof employees born in the 1950s. - Create a report that shows the
FirstName,LastName, the year ofBirthDateas birth year from theemployeestable. - Create a report showing
OrderID, total number of Order ID asNumberofOrdersfrom theorderdetailstable grouped byOrderIDand sorted byNumberofOrdersin descending order.HINT: You will need to use a
GROUP BYstatement. - Create a report that shows the
SupplierID,ProductName,CompanyNamefrom all product Supplied by Exotic Liquids, Specialty Biscuits, Ltd., Escargots Nouveaux sorted by theSupplierID. - Create a report that shows the
ShipPostalCode,OrderID,OrderDate,RequiredDate,ShippedDate,ShipAddressof all orders withShipPostalCodebeginning with "98124". - Create a report that shows the
ContactName,ContactTitle,CompanyNameof customers that the has no "Sales" in theirContactTitle. - Create a report that shows the
LastName,FirstName,Cityof employees in cities other than "Seattle". - Create a report that shows the
CompanyName,ContactTitle,City,Countryof all customers in any city in Mexico or other cities in Spain other than Madrid. - Create a select statement that outputs the following:

- Create a report that shows the
ContactNameof all customers that do not have letter A as the second alphabet in theirContactName. - Create a report that shows the average
UnitPricerounded to the next whole number, total price ofUnitsInStockand maximum number of orders from theproductstable. All saved asAveragePrice,TotalStockandMaxOrderrespectively. - Create a report that shows the
SupplierID,CompanyName,CategoryName,ProductNameandUnitPricefrom theproducts,suppliersandcategoriestable. - Create a report that shows the
CustomerID, sum ofFreight, from theorderstable with sum of freight greater $200, grouped byCustomerID.HINT: You will need to use a
GROUP BYand aHAVINGstatement. - Create a report that shows the
OrderID,ContactName,UnitPrice,Quantity,Discountfrom theorder details,ordersandcustomerstable with discount given on every purchase. - Create a report that shows the
EmployeeID, theLastNameandFirstNameas employee, and theLastNameandFirstNameof who they report to as manager from theemployeestable sorted byEmployee ID.HINT: This is a
Self JOIN. - Create a report that shows the average, minimum and maximum
UnitPriceof all products asAveragePrice,MinimumPriceandMaximumPricerespectively. - Create a view named
CustomerInfothat shows theCustomerID,CompanyName,ContactName,ContactTitle,Address,City,Country,Phone,OrderDate,RequiredDate,ShippedDatefrom thecustomersandorderstable.HINT: Create a View.
- Change the name of the view you created from
CustomerInfotoCustomerDetails. - Create a view named
ProductDetailsthat shows theProductID,CompanyName,ProductName,CategoryName,Description,QuantityPerUnit,UnitPrice,UnitsInStock,UnitsOnOrder,ReorderLevel,Discontinuedfrom thesupplier,productsandcategoriestables.HINT: Create a View.
- Drop the
CustomerDetailsview. - Create a report that fetch the first 5 character of
CategoryNamefrom thecategorytables and renamed asShortInfo. - Create a copy of the
shippertable asshippers_duplicate. Then insert a copy of shippers data into the new table.HINT: Create a Table, use the
LIKEStatement andINSERT INTOstatement. - Create a select statement that outputs the following from the
shippers_duplicateTable: (Note: Original resource referenced a visual output)
- Create a report that shows the
CompanyNameandProductNamefrom all product in the Seafood category. - Create a report that shows the
CategoryID,CompanyNameandProductNamefrom all product in theCategoryID5. - Delete the
shippers_duplicatetable. - Create a select statement that outputs the following from the
employeestable.
- Create a report that the
CompanyNameand total number of orders by customer renamed as number of orders since December 31, 1994. Show number of Orders greater than 10. - Create a select statement that outputs the following from the
producttable.
Note: This content was adapted from practice materials curated by Musili Adebayo.