Free SQL exercises

This page lists 198 SQL exercises. You can use the menu on the left to show just exercises for a specific topic.

This page lists 198 SQL exercises. You can use the menu above to show just exercises for a specific topic.

Software: SQL
Version: Any version of SQL Server
Topic: Transactions
Level: Relatively easy
Course: Advanced SQL
Exercise: Create a transaction, add Shaun the Sheep as a Doctor Who, then commit or roll this back.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Table-valued functions
Level: Relatively easy
Course: Advanced SQL
Exercise: Create a table-valued function to list events for a given year.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Dynamic SQL
Level: Relatively easy
Course: Advanced SQL
Exercise: Create a stored procedure which can change the name of the table in the FROM clause.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Stored procedures
Level: Relatively easy
Course: Advanced SQL
Exercise: Write a basic procedure to list countries in Asia, then make small changes to it.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Temporary tables and table variables
Level: Relatively easy
Course: Advanced SQL
Exercise: Write SQL to put Dr Who rows in a temporary table, and add companions and enemies into this.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Temporary tables and table variables
Level: Relatively easy
Course: Advanced SQL
Exercise: Declare a table variable, and copy the Dr Who companions, enemies and doctors into it.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Derived tables and CTEs
Level: Relatively easy
Courses: Introduction to SQLAdvanced SQL
Exercise: Use a CTE to group a query by a CASE expression without including it twice.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Scalar functions
Level: Relatively easy
Course: Advanced SQL
Exercise: Use a scalar function to include each Dr Who's incarnation dates in a query.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Parameters and return values
Level: Relatively easy
Course: Advanced SQL
Exercise: Get a stored procedure to list the Dr Who episodes for a given enemy.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Scalar functions
Level: Relatively easy
Course: Advanced SQL
Exercise: Count the number of letters for an event using a scalar function.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Stored procedures
Level: Relatively easy
Course: Advanced SQL
Exercise: Create a stored procedure to list Dr Who episodes featuring Matt Smith.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Stored procedures
Level: Relatively easy
Course: Advanced SQL
Exercise: Filter the select statement, only show events occuring in August.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Basic joins
Level: Relatively easy
Course: Introduction to SQL
Exercise: Use an inner join to link two tables together in a query.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Variables
Level: Relatively easy
Course: Advanced SQL
Exercise: Declare 3 variables to hold different data types.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Views
Level: Relatively easy
Course: Introduction to SQL
Exercise: Use the view designer to create a view, and change it in SQL.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Relatively easy
Course:
Exercise: Apply a simple error trap to delete a temporary table if it exists before creating it.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Relatively easy
Course:
Exercise: Create a scalar function to return the weekday for a given date.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Relatively easy
Course:
Exercise: Create an in-line table-valued function to return all of the courses which occur between 2 dates which you pass into the function as parameters.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Setting criteria using WHERE
Level: Relatively easy
Course: Introduction to SQL
Exercise: Create a query to list all of the love and relationships events.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Relatively easy
Course:
Exercise: Create a simple query to list historical events, with the most recent first.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Simple Queries
Level: Relatively easy
Course: Introduction to SQL
Exercise: Create a query to list out all of the events in the database, with the most recent first.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Subqueries
Level: Relatively easy
Course: Introduction to SQL
Exercise: Use a subquery to show events which happened since the last one for a particular country occurred.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Relatively easy
Course:
Exercise: Create a query listing countries and continents, using an inner join with table aliases.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Relatively easy
Course:
Exercise: Create an SQL query showing the count of events for each country, showing the use of GROUP BY.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Relatively easy
Course:
Exercise: Create a procedure which takes one parameter (the name of a category) and selects all websites in that category.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Relatively easy
Course:
Exercise: Use a temporary table to show all the organisations which are either in a particular sector or which employ more than a certain number of people.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Variables
Level: Relatively easy
Course: Advanced SQL
Exercise: Using variables create a summary output window of aggregated data.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Relatively easy
Course:
Exercise: Script a view in the query window to show all events occurring in 2000, then modify this in the view designer.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Derived tables and CTEs
Level: Relatively easy
Courses: Introduction to SQLAdvanced SQL
Exercise: Use a Common Table Expression to write a complex query in two distinct parts.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Relatively easy
Course:
Exercise: Create a function to show when a course ends, given its start date and how long it lasts, and use this function in a query.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Calculations using dates
Level: Relatively easy
Course: Introduction to SQL
Exercise: Combine the YEAR, CONVERT and FORMAT functions to show events in your year of birth.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Relatively easy
Course:
Exercise: Create a query to show for each event in history the length of the description entered.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Simple Queries
Level: Relatively easy
Course: Introduction to SQL
Exercise: Create a query using TOP N to show the last 3 categories in a table.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Aggregation and grouping
Level: Relatively easy
Course: Introduction to SQL
Exercise: Use grouping to show how many episodes each Doctor Who author wrote.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Aggregation and grouping
Level: Relatively easy
Course: Introduction to SQL
Exercise: Use GROUP BY and COUNT to report on the number of events for each category.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Aggregation and grouping
Level: Relatively easy
Course: Introduction to SQL
Exercise: Use COUNT, MAX and MIN to show statistics about the rows in the events table.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Relatively easy
Course:
Exercise: Format each date in a table, then show how long ago it was (in whole years) using DateDiff.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Relatively easy
Course:
Exercise: Create a query showing all of the continents with no matching countries, using a left outer join.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Relatively easy
Course:
Exercise: Create a query listing all of the historical events which have happened in Germany in the 1940s.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Relatively easy
Course:
Exercise: Create a cursor to loop over a table of cursors, writing out the details for each.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Relatively easy
Course:
Exercise: Create a simple view joining 2 tables together, then script a simple change to it.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Relatively easy
Course:
Exercise: Create a stored procedure to show the top 10 websites in the UK, and check it runs OK.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Relatively easy
Course:
Exercise: Create a stored procedure which returns all of the events between 2 dates.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Variables
Level: Relatively easy
Course: Advanced SQL
Exercise: Create a simple stored procedure with a single variable to work out your age, and then print it out.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Relatively easy
Course:
Exercise: Create a subquery showing all events which have happened since the last one involving the European Union.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Relatively easy
Course:
Exercise: Create a table variable, and insert into the table C# courses and courses given by a particular trainer - then list the results.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Temporary tables and table variables
Level: Relatively easy
Course: Advanced SQL
Exercise: Try both methods of creating Temporary tables, using INTO and CREATE.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Relatively easy
Course:
Exercise: Create a query to show the country and the continent id (or 0, if this is null) using ISNULL or COALESCE.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Derived tables and CTEs
Level: Relatively easy
Courses: Introduction to SQLAdvanced SQL
Exercise: Create an expression in a common table expression, and group by this in a query using the CTE.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Table-valued functions
Level: Relatively easy
Course: Advanced SQL
Exercise: List episodes made by any given doctor by using a table function in SQL.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Relatively easy
Course:
Exercise: Create a CTE (common table expression) listing all important people, then use this to show which courses they have attended.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Calculations
Level: Relatively easy
Course: Introduction to SQL
Exercise: List for each event the number of characters in its name.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Basic joins
Level: Relatively easy
Course: Introduction to SQL
Exercise: Create a query using the designer, joining 2 tables, then tidy it up and comment its SQL.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Variables
Level: Relatively easy
Course: Advanced SQL
Exercise: Use a variable holding a row id to get at the details for a row.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Subqueries
Level: Relatively easy
Course: Introduction to SQL
Exercise: Use subqueries to filter with aggregates.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Relatively easy
Course:
Exercise: List the names of the Doctors in alphabetical order using SELECT.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Looping
Level: Average difficulty
Course: Advanced SQL
Exercise: Create a loop to perform a count for each year of a given range.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Testing conditions
Level: Average difficulty
Course: Advanced SQL
Exercise: Use IF to change the SELECT statement that a stored proc runs.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Calculations
Level: Average difficulty
Course: Introduction to SQL
Exercise: Cast numbers as text to allow you to concatenate them together with strings.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Average difficulty
Course:
Exercise: Crreate two variables, and assign to them aggregate values from SELECT statements to compare people by status.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Average difficulty
Course:
Exercise: Create a function which calculates and returns the difference in lengths between two bits of text.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Table-valued functions
Level: Average difficulty
Course: Advanced SQL
Exercise: Return a table of episodes for a series number and author, using a table-valued function.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Creating tables
Level: Average difficulty
Course: Advanced SQL
Exercise: Write a SQL query to create a table of companies, and add two rows to it.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Pivots
Level: Average difficulty
Course: Advanced SQL
Exercise: Show the number of episodes by year and series number as pivoted data.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Parameters and return values
Level: Average difficulty
Course: Advanced SQL
Exercise: Create a stored procedure with NULLs as the default values.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Stored procedures
Level: Average difficulty
Course: Advanced SQL
Exercise: Create a stored procedure to list Dr Who episodes written by Steven Moffat.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Basic joins
Level: Average difficulty
Course: Introduction to SQL
Exercise: Join two tables together in SQL, using alias table names.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Scalar functions
Level: Average difficulty
Course: Advanced SQL
Exercise: Use an SQL function within a query to find out the part number of any Dr Who episode.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Triggers
Level: Average difficulty
Course: Advanced SQL
Exercise: Use a trigger to log when anyone adds, deletes or renames a country.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Views
Level: Average difficulty
Course: Introduction to SQL
Exercise: Write a view to combine tables, then use this as a basis for a grouping query.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Views
Level: Average difficulty
Course: Introduction to SQL
Exercise: Script a view in a query, then use the view designer to edit it.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Creating tables
Level: Average difficulty
Course: Advanced SQL
Exercise: Create a table of genres for books using CREATE TABLE, then insert a few rows using INSERT INTO.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Basic joins
Level: Average difficulty
Course: Introduction to SQL
Exercise: Link the continent, country and event tables with inner joins, and then filter by fields from 2 tables.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Average difficulty
Course:
Exercise: Create a function to return a neatly formatted date.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Scalar functions
Level: Average difficulty
Course: Advanced SQL
Exercise: Create a function to take in any month number and return the month name.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Average difficulty
Course:
Exercise: Use a temporary table to hold directors and actors born in 1969 - flower children.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Creating tables
Level: Average difficulty
Course: Advanced SQL
Exercise: Has mistake must fix spelling of millenium!!!!.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Average difficulty
Course:
Exercise: Create a new database of dates in history, consisting of 2 tables, setting the primary key for each and creating a database diagram linking them.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Parameters and return values
Level: Average difficulty
Course: Advanced SQL
Exercise: Filter in a stored procedure using a parameter.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Average difficulty
Course:
Exercise: Create a view listing for each country the number of events (with various criteria), then turn this view into well-formatted SQL.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Transactions
Level: Average difficulty
Course: Advanced SQL
Exercise: Create a transaction which prevents the same event being added multiple times.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Average difficulty
Course:
Exercise: Use a CTE - common table expression - to show all actors appearing in films directed by Spielberg.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Derived tables and CTEs
Level: Average difficulty
Courses: Introduction to SQLAdvanced SQL
Exercise: Use linked CTEs to show continents having many countries but few events.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Parameters and return values
Level: Average difficulty
Course: Advanced SQL
Exercise: Create stored procedures with default values for the parameters.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Average difficulty
Course:
Exercise: Create a procedure to delete all over 18 sites from a database within a transaction, then roll this back and prove this has undone the deletion.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Dynamic SQL
Level: Average difficulty
Course: Advanced SQL
Exercise: Create a query which selects whatever columns, sort order and table you pass in as parameters, using dynamic SQL.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Average difficulty
Course:
Exercise: Create a query to output a results set of historical events to an Excel workbook.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Average difficulty
Course:
Exercise: Write a function to count the number of values in a comma-delimited string (and hence the number of trainers assigned to a course).

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Average difficulty
Course:
Exercise: Create a function which - given a resource name - locates its id and returns a table of all courses having this resource id in a comma-delimited id list.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Average difficulty
Course:
Exercise: Create a function to divide events into the pre-punk, punk and post-punk eras, using a CASE statement.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Creating tables
Level: Average difficulty
Course: Advanced SQL
Exercise: Store useful information about continents in a new permanent table.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Temporary tables and table variables
Level: Average difficulty
Course: Advanced SQL
Exercise: Use a table variable to hold useful information from several sources together.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Basic joins
Level: Average difficulty
Course: Introduction to SQL
Exercise: Create an inner join in a query, then change it to an outer join to show categories having no events.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Temporary tables and table variables
Level: Average difficulty
Course: Advanced SQL
Exercise: Use a table variable to join one set of data to another table.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Basic joins
Level: Average difficulty
Course: Introduction to SQL
Exercise: Use inner joins to link four tables to show Dr Who enemies by author.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Average difficulty
Course:
Exercise: Create a stored procedure to show all delegates from a training database where the company name and category name equal parameters (which can be null).

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Average difficulty
Course:
Exercise: Create a query linking 3 tables together (historic events, countries and continents) using inner joins and aliases.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Setting criteria using WHERE
Level: Average difficulty
Course: Introduction to SQL
Exercise: Use a WHERE clause to show all of the events between two given dates.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Average difficulty
Course:
Exercise: List out great events in history, using a CASE statement to show one of three regional areas for each.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Average difficulty
Course:
Exercise: Create a query to show all of the events which involved Concorde in the title which took place in France.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Setting criteria using WHERE
Level: Average difficulty
Course: Introduction to SQL
Exercise: Us a WHERE clause to show events which contain two given text strings.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Calculations using dates
Level: Average difficulty
Course: Introduction to SQL
Exercise: Use the DATEDIFF and the ABS functions to list the events in order of closeness to when you were born.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Average difficulty
Course:
Exercise: Create a query to list out the first half of countries (using TOP 50 PERCENT).

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Temporary tables and table variables
Level: Average difficulty
Course: Advanced SQL
Exercise: Add rows one by one within a loop into a temporary table that you've created.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Average difficulty
Course:
Exercise: Create a stored procedure to move a given person to a given organisation, logging this fact, but with error trapping to undo the transaction if it fails.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Parameters and return values
Level: Average difficulty
Course: Advanced SQL
Exercise: Use an output parameter to return a list variable of the most eventful continents.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Transactions
Level: Average difficulty
Course: Advanced SQL
Exercise: Update the number of Dr Who enemies within a transaction, and commit or roll this back.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Temporary tables and table variables
Level: Average difficulty
Course: Advanced SQL
Exercise: Use a temporary table or table variable to combine the best Doctor Who episodes into a single table.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Subqueries
Level: Average difficulty
Course: Introduction to SQL
Exercise: Using sub queries filter the select statement.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Average difficulty
Course:
Exercise: Show the closest events to your birthday, using DATEDIFF, CONVERT and ABS functions.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Aggregation and grouping
Level: Average difficulty
Course: Introduction to SQL
Exercise: Use SELECT, FROM, WHERE, GROUP BY, HAVING and ORDER BY to list non-European busy countries.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Subqueries
Level: Average difficulty
Course: Introduction to SQL
Exercise: Create a correlated subquery to list out all countries having more than 8 events.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Average difficulty
Course:
Exercise: Create a function to show all directors/actors whose names contain a given string.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Calculations using dates
Level: Average difficulty
Course: Introduction to SQL
Exercise: Use the DATENAME and DATEPART functions to show events taking place on Friday 13th of any month/year.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Average difficulty
Course:
Exercise: Create an SQL query to show the number of events taking place for each country, using a WHERE and a HAVING clause to filter data.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Calculations
Level: Average difficulty
Course: Introduction to SQL
Exercise: Use IsNull, Coalesce and/or CASE WHEN to replace nulls with values.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Simple Queries
Level: Average difficulty
Course: Introduction to SQL
Exercise: Create a query to list the first 5 events in chronological order.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Aggregation and grouping
Level: Average difficulty
Course: Introduction to SQL
Exercise: Group by 2 fields and use HAVING clause to show popular combinations.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Average difficulty
Course:
Exercise: Create a stored procedure which will add any event to a database, using parameters.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Average difficulty
Course:
Exercise: Various examples of SQL using subqueries, including using ANY and IN.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Table-valued functions
Level: Average difficulty
Course: Advanced SQL
Exercise: Create an inline tabled valued function taking two parameters.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Average difficulty
Course:
Exercise: Create a blank temporary table, fill it with people, then use UPDATE statements to set the value of various null columns.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Transactions
Level: Average difficulty
Course: Advanced SQL
Exercise: Create a transaction to update all countries that aren't your own, then roll it back.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Calculations
Level: Average difficulty
Course: Introduction to SQL
Exercise: Write a CASE WHEN expression to assign countries to different groups.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Derived tables and CTEs
Level: Average difficulty
Courses: Introduction to SQLAdvanced SQL
Exercise: Show enemies appearing in chosen episodes, using a Common Table Expression (CTE).

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Derived tables and CTEs
Level: Average difficulty
Courses: Introduction to SQLAdvanced SQL
Exercise: Create a CTE to show - amazingly easily - events containing the words THIS and THAT, in this order.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Parameters and return values
Level: Average difficulty
Course: Advanced SQL
Exercise: Create a stored procedure to list Dr Who episodes for a series number, using a default parameter value.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Variables
Level: Average difficulty
Course: Advanced SQL
Exercise: Read a list of the enemies of Doctor Who into a string variable.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Average difficulty
Course:
Exercise: Print out list of websites with position for each, using a cursor.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Dynamic SQL
Level: Average difficulty
Course: Advanced SQL
Exercise: Write a procedure using EXEC which varies the sort column according to the parameter value passed.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: More exotic joins
Level: Average difficulty
Course:
Exercise: Write an SQL outer join to show unmatched records in another table.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Parameters and return values
Level: Average difficulty
Course: Advanced SQL
Exercise: Create a procedure to list out the companions for a given doctor.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Views
Level: Average difficulty
Course: Introduction to SQL
Exercise: Create a view in the view designer, tidy up its SQL and use it to select data.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Average difficulty
Course:
Exercise: Create a stored procedure to store the row count of a query in a variable, then redisplay it.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Derived tables and CTEs
Level: Average difficulty
Courses: Introduction to SQLAdvanced SQL
Exercise: Use a basic CTE to hold filtered data before joining onto another table.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Derived tables and CTEs
Level: Average difficulty
Courses: Introduction to SQLAdvanced SQL
Exercise: Use a derived table to hold a select statement. Then join another table on.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Transactions
Level: Average difficulty
Course: Advanced SQL
Exercise: Use the Insert, Update and Delete commands in a single transaction.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Parameters and return values
Level: Average difficulty
Course: Advanced SQL
Exercise: Use return values to bring back an INT return value from a stored procedure.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Variables
Level: Average difficulty
Course: Advanced SQL
Exercise: Filtering a stored procedure using variables.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Average difficulty
Course:
Exercise: Create a stored procedure which takes a parameter specifying how many columns you should display - this tests your knowledge of IF / ELSE.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Average difficulty
Course:
Exercise: Create a procedure to show all websites from a table which went online between two given dates, passed as parameters and taking default values.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Table-valued functions
Level: Harder than average
Course: Advanced SQL
Exercise: Show episodes, listing companions or enemies according to parameter, using an MSTVF function.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Table-valued functions
Level: Harder than average
Course: Advanced SQL
Exercise: Use a MSTVF to show categories, countries and continents that contain a given vowel.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Harder than average
Course:
Exercise: Using string concatenation and a variable, accumulate and then display the names of all of the delegates on a given training course.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Temporary tables and table variables
Level: Harder than average
Course: Advanced SQL
Exercise: Use a table variable or temporary table to build up a list of database problems in a single table.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Parameters and return values
Level: Harder than average
Course: Advanced SQL
Exercise: Return a continent name from one procedure, and pass the output value into another.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Setting criteria using WHERE
Level: Harder than average
Course: Introduction to SQL
Exercise: Use wildcards, AND, IN and OR to get a list of events to do with water.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Harder than average
Course:
Exercise: Create a CTE comparing website usage statistics for 2 countries, and use this to display a final analysis.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Harder than average
Course:
Exercise: Something to while away the time - convert a string of trainer ids into a string of table names, using any method you prefer!.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Variables
Level: Harder than average
Course: Advanced SQL
Exercise: Create a list variable to store all the events released in a specified year.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Stored procedures
Level: Harder than average
Course: Advanced SQL
Exercise: Create a stored procedure to list Dr Who episodes by frequency in two ways.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Aggregation and grouping
Level: Harder than average
Course: Introduction to SQL
Exercise: Use lots of grouping and criteria to list out year/doctor episode counts.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Derived tables and CTEs
Level: Harder than average
Courses: Introduction to SQLAdvanced SQL
Exercise: Use a series of CTEs to show categories for countries for events not containing the letters OWL.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Views
Level: Harder than average
Course: Introduction to SQL
Exercise: Use views based on views to show Doctor Who episodes with only 1 enemy and 1 companion.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Triggers
Level: Harder than average
Course: Advanced SQL
Exercise: Prevent anyone deleting events in the UK with an INSTEAD OF DELETE trigger.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Basic joins
Level: Harder than average
Course: Introduction to SQL
Exercise: Use inner joins to link lots of tables together, with a WHERE clause.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: More exotic joins
Level: Harder than average
Course:
Exercise: Create 2 self-joins between a table of families and itself, to show families, their parents and their grandparents.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Scalar functions
Level: Harder than average
Course: Advanced SQL
Exercise: Create all sorts of scalar functions in SQL to return info about Dr Who episodes from within a query.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Creating tables
Level: Harder than average
Course: Advanced SQL
Exercise: Create a table of genres for books, and create a foreign key constraint linking this to a table of authors.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Harder than average
Course:
Exercise: Write a stored procedure to accumulate delegate names for a given course, then use a cursor to build and display a tmeporary table.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Harder than average
Course:
Exercise: Design a database to hold rental and membership information for a DVD library.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Dynamic SQL
Level: Harder than average
Course: Advanced SQL
Exercise: Using a comma-delimited list variable filter another select statement using dynamic SQL.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Setting criteria using WHERE
Level: Harder than average
Course: Introduction to SQL
Exercise: 3 challenging queries combining criteria to find possible data anomalies.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Looping
Level: Harder than average
Course: Advanced SQL
Exercise: Write a nested WHILE loop to find the first N primes.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Harder than average
Course:
Exercise: Create a function to return the country domain (eg .uk) from a website address, using REVERSE, CHARINDEX, etc.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Scalar functions
Level: Harder than average
Course: Advanced SQL
Exercise: Use a CASE statement to classify each event according to whether it is the top of its class or not.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Temporary tables and table variables
Level: Harder than average
Course: Advanced SQL
Exercise: Write the names of your stored procedures and functions to a temporary table or table variable.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Harder than average
Course:
Exercise: Create an SQL query to group events by their year and month - this combines GROUP BY with calculations.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Harder than average
Course:
Exercise: Show all of the records in one table (films) with no corresponding records in another (actors) linked indirectly.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Harder than average
Course:
Exercise: Check if a given resource exists, then count courses containing its id (in a comma-delimited list) and return count using output parameter or return value.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Subqueries
Level: Harder than average
Course: Introduction to SQL
Exercise: Use two subqueries to list all events in neither the last 30 countries or the last 15 categories.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Basic joins
Level: Harder than average
Course: Introduction to SQL
Exercise: Use an outer join and criterion to list out the countries which have no corresponding events.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Harder than average
Course:
Exercise: Create a query to list out details from a table to text, using the PRINT statement to create a header.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Harder than average
Course:
Exercise: Create a single query to show the top and bottom 5 events in alphabetical order.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Looping
Level: Harder than average
Course: Advanced SQL
Exercise: Use a loop to show a comma-delimited list of all the films released in each month.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Harder than average
Course:
Exercise: Assign values from columns of a row of training courses to separate variables, using a single SELECT statement.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Derived tables and CTEs
Level: Harder than average
Courses: Introduction to SQLAdvanced SQL
Exercise: Create a CTE - common table expression - to display breadcrumbs using recursion.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Harder than average
Course:
Exercise: Within a transaction, update a column to show number of courses attended, then delete those people who have attended 5 or less - then roll back.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Harder than average
Course:
Exercise: Create a T-SQL query to list events by decade - combining the use of a GROUP BY clause with a CASE statement.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Harder than average
Course:
Exercise: Show all historical events for which either of two complicated combinations of criteria is true.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Calculations
Level: Harder than average
Course: Introduction to SQL
Exercise: Use the % modulus operator and a lot of ingenuity to show how big each country is relative to Wales.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Simple Queries
Level: Harder than average
Course: Introduction to SQL
Exercise: Run two SELECT queries to show the first and last events.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Harder than average
Course:
Exercise: Create a query showing the full date in the format: dddd dd mmmm yyyy, using DatePart or DateName, then show all events happening on Friday 13th.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Aggregation and grouping
Level: Harder than average
Course: Introduction to SQL
Exercise: Combine CAST, AVG, COUNT, LEN, UPPER and LEFT to show the average length of event names by category initial.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Derived tables and CTEs
Level: Harder than average
Courses: Introduction to SQLAdvanced SQL
Exercise: For each combination of the top 3 categories and top 3 countries, count the number of events using CTEs.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Aggregation and grouping
Level: Harder than average
Course: Introduction to SQL
Exercise: Use a complex CASE statement to show the number of events for each century, including the CUBE function.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Harder than average
Course:
Exercise: Create a query combining text and numbers, using LEN to find the length of a word and CAST to convert this to string.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Derived tables and CTEs
Level: Harder than average
Courses: Introduction to SQLAdvanced SQL
Exercise: Use common table expressions, subqueries or any other technique to solve a complex query.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Harder than average
Course:
Exercise: Create a stored procedure to show all of the events which begin with one letter, and end with another (optional) letter.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Derived tables and CTEs
Level: Harder than average
Courses: Introduction to SQLAdvanced SQL
Exercise: Cretae two CTEs to hold data before joining them together.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Pivots
Level: Harder than average
Course: Advanced SQL
Exercise: Show complete list of column headers using dynamic SQL and the PIVOT command.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Parameters and return values
Level: Harder than average
Course: Advanced SQL
Exercise: Count rows and pass the information out of a procedure using output parameters.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Calculations
Level: Harder than average
Course: Introduction to SQL
Exercise: Use the CHARINDEX function multiple times to show the number of characters between two words in a text string.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Calculations using dates
Level: Harder than average
Course: Introduction to SQL
Exercise: Display full dates, including the correct suffix (1st, 2nd, 3rd, etc).

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Variables
Level: Harder than average
Course: Advanced SQL
Exercise: Given the name of a Doctor Who, use variables to print details for him.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Parameters and return values
Level: Harder than average
Course: Advanced SQL
Exercise: Return from a stored proecure the name of the country with the most events and how many events there were.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Archived
Level: Harder than average
Course:
Exercise: Create various simple queries, showing the use of IS NULL for criteria and date criteria.

Go to exercise ...

Software: SQL
Version: Any version of SQL Server
Topic: Calculations
Level: Harder than average
Course: Introduction to SQL
Exercise: Divide events according to whether their first/last letters are the same or vowels.

Go to exercise ...

You can search our full list of SQL exercises here. Or why not learn to do them the right way on one of our classroom SQL training courses in the UK, or on one of our live online SQL courses wherever you are in the world?

This page has 0 threads Add post