SQL Server sample databases for practice
Microsoft SQL Server is the database many companies run for business systems and reporting, and the free Developer and Express editions run on Windows, Linux and Docker. Its dialect is T-SQL. Here are 13 practice databases as SQL Server scripts, each with six questions whose answers were produced by SQL Server 2022 16.0.4325.4.
Sales orders
Superstore-style retail orders with sales, cost, discount and profit.
SQL ServerOnline store
Four related tables (customers, products, orders, order items) for JOIN practice.
SQL ServerEmployees
An HR table: departments, titles, hire dates, salaries and managers.
SQL ServerCustomers
A customer list with segments, order counts, lifetime spend and activity.
SQL ServerInventory
Stock levels, reorder points and stock value across four warehouses.
SQL ServerStudent grades
Test scores, averages, letter grades and attendance for grades 9-12.
SQL ServerWebsite traffic
Daily sessions, users, bounce rate and conversions by channel.
SQL ServerMarketing campaigns
Budget, spend, impressions, clicks, CPA and ROAS per campaign.
SQL ServerCall center
Calls with queue, reason, wait and handle time, resolution and CSAT.
SQL ServerRestaurant orders
Order lines with menu items, tickets, order type and payment.
SQL ServerInvoices
Invoices with due dates, payment dates, tax, status and days late.
SQL ServerBank transactions
One fictional account: debits, credits, categories and a running balance.
SQL ServerMessy customers
A customer list with duplicates, blanks, mixed dates and text in number columns.
SQL Server SQL in these exercises
The questions are the same on every engine; what changes is the syntax. These are the SQL Server-specific parts you will meet:
- CONVERT(char(7), date, 126) keeps the first 7 characters of the ISO date (yyyy-mm). It is faster than FORMAT() and short enough to sort a window frame on.
- YEAR(date) returns the year as a number.
- DATEDIFF(day, earlier, later) returns the number of days between two dates.
- SELECT TOP n ... ORDER BY returns the first n rows; SQL Server has no LIMIT.
- Yes/no columns are BIT: 1 is true and 0 is false.
- CONCAT(a, b, ...) joins strings and treats NULL as an empty string.
- SUBSTRING(text, start, length) cuts out part of a string.
- Integer divided by integer truncates in SQL Server, so the numerator is cast to DECIMAL first.
- AVG of an INT column returns an INT in SQL Server, so the column is cast to DECIMAL first.
- RANK() and ROW_NUMBER() OVER (PARTITION BY ...) work in SQL Server 2012 and later.
- LEN counts characters and ignores trailing spaces.
- The usual default collation is case-insensitive, so COLLATE Latin1_General_CS_AS is added to count raw emails case-sensitively.
Other engines
SQLite · PostgreSQL · MySQL · DuckDB
The data is fictional and released under CC0. AdventureWorks, Northwind and Sakila are well-known sample databases from their vendors; we do not copy them here.