DataBrighten

    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.

    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.