8 Tips Absolute Beginners Can Use to Fix SQL Queries
Code errors are common – and frustrating. And when you’re just learning SQL, it can be very challenging to find and fix your mistakes. In this post, we’ll show you eight ways to solve or eliminate common SQL coding errors.
Today, we’ll talk about some tips the SQL beginner can use to avoid several common errors. These tips work in any database environment. As we go along, we’ll be showing some error messages. The exact wording of your error messages may be a little different, but don’t worry; each database engine has their own variants, but the meaning is the same.
That being said, we won’t be focusing on error messages. Instead, we’ll pinpoint the actual cause of the problem (such as forgetting a parenthesis or a quotation mark). This way, you’ll learn how to bypass the issue in the first place.
Ready? Let’s begin!
1. Place Open and Close Parentheses First
Remembering the closing character is key to eliminating unbalanced parentheses, quotes, double quotes, or square brackets. Best practices suggest typing both characters first (open and close) and then typing whatever goes inside.
The following example shows an error when parentheses are not balanced:
Example 1: Unbalanced parentheses
2. Don’t Put a Comma at the End of a Column or Table Sequence
Commas act as a separator in SQL. There should not be any commas between FROM and the first table name or after the final table name. The same idea applies to column definition: when you create a table, be sure not to type an extra comma after the final column name.
This is a really common error.
Example 2: An extra comma
3. Use Partial Query Evaluation to Debug Long SQL Queries
Many SQL clients like Navicat or pgAdmin allow the partial execution of a query. You can do this by using your mouse to highlight part of the code. Using this divide-and-conquer technique, you can easily isolate and fix errors. Obviously, the marked part must be valid SQL.
The upcoming query has two errors. If we execute the opening line of the query, we can see the first problem. (Hint: the «llastname» column doesn’t exist.)
Example 3: An incorrect column name
However if we execute the complete SQL statement, we get an error related to unbalanced parentheses:
Example 4: Subquery with wrong parentheses
We can also mark a subquery and execute it individually, as in the next example:
Example 5: Incorrect function name
4. Pay Attention to Column and Table Names
Pay very close attention when typing column names or table names. If possible, try to copy and paste the name from a command you know is correct – preferably one that’s already executed properly. Best practices suggest copying and pasting names even if you think it’s more time-consuming than typing.
Having a misspelled column name or referring to a column in a table not in the FROM clause is very common indeed. Always look for typos in the column name, make sure the table in the FROM has this column, and make sure that the table is mentioned in FROM .
Example 6: Incorrect table name
Another good practice is to use a table alias or a table name as a column prefix. This is doubly important when you have two or more tables in the <>FROM clause. The following error can appear if you refer to two identically-named columns in different tables:
Example 7: Ambiguous column names
Example 8: Prefixed column names
To fix these errors, add the table name before the column name. (In the above example, that would be employee.depto_id and department.depto_id instead of just depto_id .)
5. Compare Compatible Data Types Only
When you write comparison conditions in the WHERE clause, make sure both data types are compatible with the comparison operator and with each other. If this is not possible, you may have to cast one of the data types. The general rule is to compare numbers against numbers, character strings against character strings, etc.
Some database systems automatically convert data types where possible; others provide enhanced data type conversions (i.e. a TIMESTAMP value can be automatically converted to a DATE before comparisons). Still other database services don’t offer conversions at all. So it is best to look out for these potential issues yourself.
Anyway, the following SQL code gets a data mismatch error because a CHAR string is being compared with an integer value:
Example 9: Mismatched data types
No operator matches the given name and argument type(s). You might need to add explicit type casts to solve this one.
6. Use IS NULL When Comparing NULL Values
If you only need to verify whether a column has a NULL value, pay special attention to which expressions you use. One common mistake is to use = NULL or <> NULL , but these expressions are not syntactically valid. Use IS NULL and IS NOT NULL clauses instead.
Let’s see the incorrect and correct samples:
Example 10: Incorrect NULL comparison
Example 11: Correct NULL comparison
7. Always Include the JOIN Condition
There is more than one valid way to do a join in SQL. The traditional way is to list all the tables to be joined in the FROM clause and put the join conditions in the WHERE clause to build pairs of records. The other (more declarative) way is to use the JOIN clause and list the join conditions after the ON clause. Both are syntactically equivalent, but you should know how to identify the join condition for both.
Here we have two valid joins:
Example 12: Two equivalent joins
However, the tip is: Don’t forget the join condition! Every time you join two or more tables, you must write a join condition to link both tables. If you don’t specify this, you won’t get an error message; you’ll just get incorrect results. These will be wrong because every record from the first table will be joined with all records of the second table. This type of result set is called a Cartesian product of two tables, and usually is not an expected result.
Example 13: A Cartesian product – usually not the result you want
8. Include Non-Aggregated Columns from the SELECT List in GROUP BY Columns
When using aggregate functions, there are some restrictions on what columns can be included in the SELECT list (i.e. the column names after the SELECT clause). You can only include the columns specified in the GROUP BY clause, plus aggregate functions and constants. If you select only aggregate columns, any other column will generate an error. You can see this in the following example.
Example 14: Extra columns in the SELECT list
Example 15: No extra columns in the SELECT list
Sometimes we need to filter data by using an aggregation function. One of the most common mistakes is to put a condition using the aggregate function in the WHERE clause. This is shown below:
Example 16: Incorrect aggregate function in the WHERE
Remember, if you need to filter using an aggregate function, the correct way is to put the condition using the aggregate in the HAVING clause, as in the following example:
Example 17: The aggregate function is in the HAVING clause
Try It Yourself!
LearnSQL is a great place to get started with SQL. Several courses are geared for beginning learners. You can test the tips in this article on the exercises in this SQL course.