You’ve written some SQL code and you’re ready to query your database. You input the code and …. no data is returned. Instead, you get an error message.
Don’t despair! Coding errors are common in any programming language, and SQL is no exception. In this post, we’ll discuss five common mistakes people make when writing SQL.
2. You’ve written some SQL code and you’re ready
to query your database.
You input the code and …. no data is returned.
Instead, you get an error message.
Don’t despair! Coding errors are common in any
programming language, and SQL is no exception.
In this post, we’ll discuss five common mistakes
people make when writing SQL.
3. The most common SQL error is a syntax error.
Syntax: A set arrangement of words and commands.
People tend to make the same kinds of syntax mistakes.
Knowing what errors to look for is very important for
novice SQL coders.
The SQL errors we will look at are:
Misspelling Commands
Forgetting Brackets and Quotes
Specifying an Invalid Statement Order
Omitting Table Aliases
Using Case-Sensitive Names
1
2
3
4
5
Watch Your Language (and Syntax)
4. 1. Misspelling Commands
This is the most common type of mistake. Example:
If you run this query, you’ll get an error which states:
Misspelling FROM as FORM.
Misspellings are found in keywords (like SELECT,
FROM, and WHERE), or in table and column names.
Most SQL spelling errors are due to:
“Chubby fingers” hitting a letter near the right one:
SELEVT or FTOM
“Reckless typing” typing the correct letters in the
wrong order: SELETC or FORM
SELECT *
FORM dish
WHERE NAME = ’Prawn Salad’;
Syntax error in SQL statement ”SELECT * FORM[*] dish
WHERE NAME = ’Prawn Salad’;”;
SQL statement: SELECT * FORM dish WHERE NAME = ’Prawn
Salad’; [42000-176]
Solution:
The editor puts SELECT statement keyword in light
purple. If the keyword is black you know there’s a
problem (FORM is black). Correct statement is:
The keyword is now the right color, the statement
executes without an error.
SELECT *
FROM dish
WHERE NAME = ’Prawn Salad’
5. SELECT *
FROM artist
WHERE first_name = ’Vincent’ and
last_name = ’Monet’ or last_name =
’Da Vinci’;
SELECT *
FROM artist
WHERE first_name = ’Vincent’ and
(last_name = ’Monet’ or last_name =
’Da Vinci’);
2. Forgetting Brackets and Quotes
Brackets group operations together. In SQL,
the following order of operations...
… is not the same as:
A common mistake is to forget the closing bracket.
This statement:
returns an error :
Remember: brackets always come in pairs.
The same is true with quotes (‘ ‘ and ” ”).
SELECT *
FROM artist
WHERE first_name = ’Vincent’ and
(last_name = ’Monet’ or last_name =
’Da Vinci’;
ERROR: syntax error at or near ”;” Position: 102
Solution:
Getmoreexperienceandrememberpeopleusuallyforget
the closing, not the opening, bracket or quotation mark.
6. ORDER BY sets the order in which the results will
be displayed
Invalid order like this one:
3. Invalid statement order
SELECT statements have a predefined keyword order.
A correctly-ordered SELECT statement:
SELECT name
FROM dish
WHERE name = ’Prawn Salad’
GROUP BY name
HAVING count(*) = 1
ORDER BY name;
There’s no shortcut here; you simply have to remember
the correct keyword order for the SELECT statement:
SELECT identifies column names and functions
FROM specifies table name or names (and JOIN
conditions)
WHERE defines filtering statements
GROUP BY shows how to group columns
HAVING filters the grouped values
SELECT name
FROM dish
WHERE name = ’Prawn Salad’
ORDER BY name
GROUP BY name
HAVING count(*) = 1
The error message we see is pretty intimidating!
Syntax error in SQL statement ”SELECT name FROM dish
WHERE name = ’Prawn Salad’ ORDER BY name GROUP[*] BY
name HAVING count(*) = 1;”; SQL statement:
SELECT name FROM dish WHERE name = ’Prawn Salad’ ORDER
BY name GROUP BY name HAVING count(*) = 1; [42000-176]
Solution:
I suggest using a short SELECT order checklist. Refer to
your list for the correct order and practice.
1
2
3
4
5
6
7. 4. Omitting Table Aliases
Aliases distinguish among columns with the same name
acrosstables.
Aliasesaremandatoryifwejoinatabletoitself.
ThisSELECTstatement:
Returns an error:
SELECT *
FROM exhibit
JOIN exhibit ON (id = previous_id);
Ambiguous column name ”id”; SQL statement: SELECT
* FROM exhibit JOIN exhibit ON (id = previous_id);
[90059-176]
Note: When you get “ambiguous column name” error
message, you need table aliases.
The correct statement, with aliases, is:
SELECT ex.* , exp.name
FROM exhibit
JOIN exhibit ON (ex.id = exp.previous_id);
Solution:
Practice using table aliases for single-table SELECT
statements.
Usealiasesoften–theymakeyourSQLmorereadable.
8. 5. Using Case-Sensitive Names
Object names in databases are case-insensitive. This
error occurs when you write non-standard names for
tables or database objects. To correct errors tied to this
use double quotes on table name. For example:
You will need to use double quotes when:
The table will have a case-sensitive name.
The table name will contain special characters. This
includes using a blank space, like “Large Client”.
SELECT * FROM
”LargeClient”
WHERE cust_name = ’Mijona’;
Solution:
Avoid using non-standard names if you can. If not,
remember your double quotes!
Everybody Makes SQL Mistakes
Making mistakes is a normal and predictable part
of oftware development.
Don’t be discouraged.
When you make mistakes, try to analyze your code in
a structured way.
With a structured analysis, you can find and correct
your errors quicker.
Aldo Zelen
Find out more SQL topics on
academy.vertabelo.com/blog