Avoid the 5 Most Common SQL Mistakes Beginners Make

Over the years there I’ve seen students makes these common SQL mistakes.  They are really easy to avoid, and once you know the solution, you’ll find you write SQL much faster and with less tears and frustration than before.

All the examples for this lesson are based on Microsoft SQL Server Management Studio and the sample databases AdventureWorks and WideWorldImporters.  You can get started using these free tools with my Guide Getting Started Using SQL Server.

Avoid the 5 Most Common SQL Mistakes Beginners Make

Listed here are some of the most common SQL mistakes I’ve seen students or my self make.  Personally, I’m guilty of incompletely highlighting a command.  Also, there are time when I pick the wrong database!

One – Selecting the wrong database

When you launch SSMS (SQL Server Management Studio) and open a query, the window can be set to look at the Master database.

Common SQL Mistakes - Master is Selected

Selecting the Incorrect Database

This isn’t good, as this isn’t YOUR database, so SQL Server won’t have a clue about any tables and columns you’re trying to query.

Solution

Remember to always check the Available Database selection is set to your database, such as AdventureWorks2016…

 

Two – Misspelling Statements

SQL is not very forgiving when it comes to spelling. Spell SELECT as SELCT and it has no clue what you’re talking about.  Because the database isn’t human and has zero common sense, we need to be precise and spell all statements exactly!

Keywords Spelled Incorrectly - Common SQL Mistakes

Misspelling Keywords

A good tip to remember is SSMS color codes your statement. SELECT will be coded blue.

If it isn’t, most likely there’s a spelling issue.

Solution

To avoid this common SQL mistake pay attention to your typing. Get in the habit of capitalizing keywords such as SELECT and FROM. It makes your query easier to read, and separates the keywords from other portions, such as table and column names.

Scan your commands to make sure they’re color coded and that you don’t see any red underlines.

 

Three – Missing Bracket and Single Quotes

Brackets [] and single quotes ‘ are used frequently in SQL. They come in pairs, for with an opening bracket [, there must be a closing one ]. And text values are enclosed in single quotes.

If you miss a quote, then how does SQL know where your text ends, and the statement resumes?

You can have a quote be part of your data such as O’Riley, in this case, it doesn’t make sent to have an opening and ending quote, but you’ll need to make some adjustments (see: How to Include a Single Quote in a SQL Query).

Solution

Remember brackets and quotes most always come in pairs.

People rarely forget the opening bracket or quote but do tend to forget to close them.

Four – Highlights

SSMS’s query windows are a great place to write SQL and try out statements; however, if, during your edits, you select a portion of a statement and then try to run it, the window “thinks” you only want to run that portion. If it isn’t a complete statement, it will fall.

Partial Highlighted Command

Partially Highlighting a Command

In our example the table Person.Person isn’t completely highlighted, which results in an error.

Solution

Selecting a statement and running it is useful when you have several queries in the window. Just make sure you select complete lines.

Start your selection one line below, this ensures you don’t miss the “tail.”

Five – Commas and Semi-Colons

Both commas and semi-colons are used in SQL. Commas are used to separate lists, such as columns being selected or sorted; whereas, semi-colons are used to indicate the end of a statement. Though not strictly required, it’s best practice to place a semi-colon at the end of your statement.

This become mandatory with working with more complex statements, such Common Table Expressions.

common sql mistakes - trailing commas

“Dangling” commas

It can be easy to forget a comma, especially when you edit statements. Consider the following, where we commented out the last column we wish to select, notice how we forgot to remove the extra comma.

Solution

Rather than placing the comma at the end of a column, try putting it on the front. That way, when you remove a column, especially the last one, it won’t cause an issue.

Also, remember that semi-colons are mostly optional, and used to indicate the end of a statement.

I hope these you find these tips useful.

Are there any tips or common SQL mistakes and solutions you want to share? I would love to hear from you.

Have a great day!

Click Here to Leave a Comment Below 0 comments