SlideShare a Scribd company logo
4
Most read
Five
Common
SQL Errors
academy.vertabelo.com
As you learn SQL, watch out for these
common coding mistakes
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.
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)
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’
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.
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
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.
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

More Related Content

PDF
Learn plsql
PPTX
Entigrity constraint
PPTX
Lecture 4 sql {basics keys and constraints}
PPTX
Lecture 3 sql {basics ddl commands}
PPT
PPT
e computer notes - Including constraints
PDF
A noobs lesson on solr (configuration)
PDF
Using mySQL in PHP
Learn plsql
Entigrity constraint
Lecture 4 sql {basics keys and constraints}
Lecture 3 sql {basics ddl commands}
e computer notes - Including constraints
A noobs lesson on solr (configuration)
Using mySQL in PHP

What's hot (12)

PPS
Project Fusion Reference Guide V2
PDF
Exportrows
PDF
Duplicaterows
DOCX
Optiva-13449_TestResults
PPTX
Database Management - Lecture 3 - SQL Aggregate Functions, Join
PDF
PHP 7.x - past, present, future
DOC
Pl sql using_xml
PDF
PHP 7.x - past, present, future (WordPress Varna #4)
PDF
7 reasons why developers should love Joomla!
RTF
Project2q
PPSX
Report alephcollection code_call_no_order
PPT
Project Fusion Reference Guide V2
Exportrows
Duplicaterows
Optiva-13449_TestResults
Database Management - Lecture 3 - SQL Aggregate Functions, Join
PHP 7.x - past, present, future
Pl sql using_xml
PHP 7.x - past, present, future (WordPress Varna #4)
7 reasons why developers should love Joomla!
Project2q
Report alephcollection code_call_no_order
Ad

Viewers also liked (20)

PDF
Why Should I learn SQL?
PPTX
7462 slideshare ala
PDF
Paraguay | Jul-16 | Future technology options and productive use
PPTX
[NCS 기반 채용] 핵심 파해치기
PDF
สตอเบอรรี่ ครบสูตรคุณประโยชน์เพื่อสุขภาพ
PPTX
Operational Risk Debt ReductionOpEd
PPTX
福建武夷山
PPT
AIA101.4.Automating Access
PPTX
Portafolio fisica terminado1p
PPTX
Chapter 6 report (MS Access)
PPTX
Presentazione G55 - Coworking/Fablab Partanna
PPTX
Chuchura Aroggyo Final Round 2017
PPTX
Sanghati Utsav Final Round 2016
PDF
What Makes Great Infographics
PDF
You Suck At PowerPoint!
PDF
STOP! VIEW THIS! 10-Step Checklist When Uploading to Slideshare
PDF
How To Get More From SlideShare - Super-Simple Tips For Content Marketing
PDF
Masters of SlideShare
PDF
10 Ways to Win at SlideShare SEO & Presentation Optimization
Why Should I learn SQL?
7462 slideshare ala
Paraguay | Jul-16 | Future technology options and productive use
[NCS 기반 채용] 핵심 파해치기
สตอเบอรรี่ ครบสูตรคุณประโยชน์เพื่อสุขภาพ
Operational Risk Debt ReductionOpEd
福建武夷山
AIA101.4.Automating Access
Portafolio fisica terminado1p
Chapter 6 report (MS Access)
Presentazione G55 - Coworking/Fablab Partanna
Chuchura Aroggyo Final Round 2017
Sanghati Utsav Final Round 2016
What Makes Great Infographics
You Suck At PowerPoint!
STOP! VIEW THIS! 10-Step Checklist When Uploading to Slideshare
How To Get More From SlideShare - Super-Simple Tips For Content Marketing
Masters of SlideShare
10 Ways to Win at SlideShare SEO & Presentation Optimization
Ad

Similar to Five Common SQL Errors (20)

PPTX
PPT
SQL -PHP Tutorial
PPTX
SQl data base management and design
PDF
Database Architecture and Basic Concepts
PPT
MYSQL
PPTX
Lesson 2_Working_with_Tables_and_SQL_Commands.pptx
PPT
Sql 2006
PPT
Advanced SQL Injection
PPT
Sql Injection Adv Owasp
PDF
RDBMS Lab03 applying constraints (UIU)
DOCX
SQL report
PPT
postgresqlllllllllllllllllllllllllllllll.ppt
PPT
CE 279 - WRITING SQL QUERIES umat edition.ppt
PPTX
SQL PPT.pptx
PDF
Database development coding standards
PPTX
Sql basics
PPTX
Creating database using sql commands
DOCX
Exploring collections with example
PDF
PPT
Sql Injection
SQL -PHP Tutorial
SQl data base management and design
Database Architecture and Basic Concepts
MYSQL
Lesson 2_Working_with_Tables_and_SQL_Commands.pptx
Sql 2006
Advanced SQL Injection
Sql Injection Adv Owasp
RDBMS Lab03 applying constraints (UIU)
SQL report
postgresqlllllllllllllllllllllllllllllll.ppt
CE 279 - WRITING SQL QUERIES umat edition.ppt
SQL PPT.pptx
Database development coding standards
Sql basics
Creating database using sql commands
Exploring collections with example
Sql Injection

Recently uploaded (20)

PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Pharma ospi slides which help in ospi learning
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
master seminar digital applications in india
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Cell Types and Its function , kingdom of life
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
01-Introduction-to-Information-Management.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Institutional Correction lecture only . . .
PPTX
GDM (1) (1).pptx small presentation for students
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
102 student loan defaulters named and shamed – Is someone you know on the list?
Pharma ospi slides which help in ospi learning
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
master seminar digital applications in india
Final Presentation General Medicine 03-08-2024.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Microbial diseases, their pathogenesis and prophylaxis
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Cell Types and Its function , kingdom of life
Abdominal Access Techniques with Prof. Dr. R K Mishra
01-Introduction-to-Information-Management.pdf
human mycosis Human fungal infections are called human mycosis..pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
Institutional Correction lecture only . . .
GDM (1) (1).pptx small presentation for students
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape

Five Common SQL Errors

  • 1. Five Common SQL Errors academy.vertabelo.com As you learn SQL, watch out for these common coding mistakes
  • 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