All Collections
Formatting your SQL queries
Formatting your SQL queries

Style recommendations for SQL courses.

Amy Peterson avatar
Written by Amy Peterson
Updated over a week ago

In order to ensure our students best engage with the exercises you create, DataCamp has specific exercise formatting guidelines that apply across platforms and all SQL courses. 

First, all SQL queries need the following:

  • Formatting in accordance with Holywell's SQL Style guide

  • 30 lines or less of code, including code comments

  • Include code comments for all major tasks that students are asked to complete

  • Indent column/table names, subqueries, CASE statements, etc. appropriately using tabs

  • An identical number of lines between the sample and solution code

  • Identical code comments in sample and solution code

The query solution code you see below includes the following:

  • Indented columns in SELECT that each take up one line. We encourage you to optimize for vertical space since the console students see is relatively small. If you have a small query with 1-3 columns selected, feel free to include them on the same line. For more than 3 columns selected, please space them out as shown in the image below.

  • Commas at the end of each item in the query. We understand that it’s generally acceptable to place your commas at the beginning of each line, but we recommend that you place them at the end for consistency across courses.

  • An indented subquery that is clearly visible. If you include subqueries, columns, CASE statements, or other information that is part of a separate unit in your query, please indent it accordingly.

  • In-line code comments that represent each task a student is asked to complete. Please use in-line comments where possible. If your comments are brief, feel free to include them in the same line as the information in the query. If they’re longer (closer to 60 characters), please include them in a separate line. 

For comparison, the sample code below includes the following:

  • Three underscores ('___') for blanks in the code that students are asked to complete. You'll notice that for all columns selected below, there are three underscores representing what students are asked to fill in (i.e., has a blank of ___). This is treated as a "unit" of information. In the WHERE statement, there are two sets of blanks representing two "units" of information that need to be filled in (a logical operator and a number). Please follow this convention throughout your course. If you expect that a particular topic/section will be confusing for students to complete and detract from the learning objective, consider completing small portions of the code to lead students in the right direction (i.e., fill in the table alias, or parts of a CASE statement).

  • Code comments and number of lines exactly matching the solution code

Additional guidelines:

  • Include only one SELECT statement per exercise. Our course editor doesn’t allow you to generate new information from a second select statement within one exercise. You will likely need to create separate exercises or a sequential/iterative exercise where appropriate.

  • If you use DDL/DML (i.e., INSERT/UPDATE/DELETE), you will not generate any output in the Teach Editor. In order to generate an output and show the results of these procedures, you will need to end your exercise with a SELECT statement querying data that has been changed.

One final recommendation:

  • We strongly recommend that you format your query using a SQL code formatter such as this one. It will save you a considerable amount of time in formatting queries and ensure that your content is consistent. 

Did this answer your question?