Search code examples
javaandroidsqliteandroid-sqlite

android.database.sqlite.SQLiteException: near "UNION": syntax error


I am using SQLite database for android

SQLite query

SELECT t1.id,t1.name, t1.email FROM ((SELECT id,first_name as 
name,email FROM single_general where unique_code='UD') 
UNION ALL (SELECT name,email FROM school where 
unique_code='UD') ) t1

Solution

  • The SELECT syntax does not allow parentheses about the compound queries. Remove them, so that only the parentheses around the subquery are left:

    SELECT id, name, email
    FROM (SELECT id, first_name AS name, email
          FROM single_general
          WHERE unique_code = 'UD'
          UNION ALL
          SELECT 0, name, email
          FROM school
          WHERE unique_code = 'UD');