Combine multiple SELECT statements

Wrap individual sub-statements in parenthesis to make the syntax unambiguous:

(SELECT result FROM tbl1 LIMIT 1)
UNION ALL
(SELECT result FROM tbl2 LIMIT 1)

The manual about UNION is very clear on the matter:

select_statement is any SELECT statement without an ORDER BY, LIMIT, FOR UPDATE, or FOR SHARE clause. (ORDER BY and LIMIT can be attached to a subexpression if it is enclosed in parentheses. Without parentheses, these clauses will be taken to apply to the result of the UNION, not to its right-hand input expression.)


Wrapping in a subquery will get around it, but it gets a bit ugly.

SELECT result FROM (select 'a'::text AS result from foo limit 1) a
UNION ALL
SELECT result FROM (select 'b'::text AS result from bar limit 1) b

UPDATE

See Erwin's response. It is better.


create view my_data1 
AS
with data as
(
    select student_id,sum(marks) as total_marks 
    from   marks_marks 
    group by 1
) , 
data1 as
(
    select id, name 
    from   students_class
), 
data2 as
(
    select applicant_name,
           id,
           class_name 
    from   students_students
)
select data2.applicant_name , 
       data1.name as class_name ,  
       data.total_marks 
from   data2 
join   data1  on data1.id = data2.class_name 
join   data   on data.student_id = data2.id



select * from my_data1