Brainlag
  1. Library
  2. Debugging
  3. SQL

syntax errorWhat is a SQL syntax error?

SQLite stops with 'near "X": syntax error' when a query breaks the grammar of SQL. The word it names is where it got confused, and the mistake is usually just before it.

SQL5 min

What SQLite is telling you

Output
near "FROM": syntax error

SQLite reads a query word by word and checks it against SQL's grammar. Here it reached FROM at a moment when FROM could not come next, and gave up. Nothing runs.

The usual reasons:

  • A comma too many. SELECT title, year, FROM songs: after a comma SQL expects another column, and finds FROM.
  • Clauses in the wrong order. They always go SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT. An ORDER BY before a WHERE fails near WHERE.
  • Unclosed brackets or quotes. 'Neon Rain with no closing quote swallows the rest of the query.
  • A keyword used as a name. A table called order clashes with ORDER BY.

How to find it

The word in quotes is where SQLite got stuck, not always where the mistake is. Look at that word, then at what comes just before it: a stray comma, a missing comma, a clause that belongs later. If the message says near "" (nothing), SQLite reached the end of the query while still waiting for something, usually a closing bracket or quote.

Run it: a comma too many

Press Run and read the error from the bottom up.

Tables artists, songs, listeners, plays
artists 8 rows, 4 columns
idnamecountrygenre
1Nova LaneUKpop
2The TidewaterUSindie
3Kaito MoriJPelectronic
4Amara OseiGHafrobeats
5Lumen ChoirSEpop
6Rust & VelvetUKrock
7Pixel HeartsKRpop
8Juno BayAUindie
songs 30 rows, 5 columns
idtitleartist_idsecondsyear
1Neon Rain61782022
2Slow Motion11582025
3Paper Planes22332023
4Static12692020
5Golden Hour11622022
6Night Bus71572020
7Echoes22812022
8Sugar Rush12842019
9Low Tide42892019
10Glass House71522020
11Satellite12822025
12Wildfire32142022

First 12 of 30 rows. Query the table to see the rest.

listeners 10 rows, 4 columns
idusernameagecountry
1maya_k15UK
2devraj17IN
3lucia.p16ES
4tom_h14UK
5ana.s18BR
6kenji16JP
7zoe_r15US
8femi17NG
9liv13SE
10oscar.b16NULL
plays 70 rows, 5 columns
idlistener_idsong_iddayskipped
110162026-09-010
2832026-09-010
35162026-09-010
4222026-09-010
55212026-09-010
65232026-09-020
7612026-09-020
8662026-09-020
9822026-09-021
10552026-09-020
117132026-09-020
12262026-09-030

First 12 of 70 rows. Query the table to see the rest.

query.sql to run
SELECT title, year, seconds,
FROM songs
WHERE year = 2022;

The newest songs list breaks

This should list the title and year of the songs released after 2022, newest first. It stops with a syntax error. Fix it.

exercise_1.sql to run
SELECT title, year
FROM songs
ORDER BY year DESC
WHERE year > 2022;

Code editor. Press Control or Command plus Enter to run the code. Tab indents; to move focus out of the editor, press Escape and then Tab.