Question: I am trying to write an sql query that counts the rows of two columns that reside in different tables joined together by a UNION ALL.

```
SELECT SUM(usernames) AS total 
   FROM 
   ((SELECT count(username) AS usernames 
        FROM table1 
        WHERE columa < '20' AND columnb = 'c' ) 
   UNION ALL 
   (SELECT count(name) AS usernames 
       FROM table2 
       WHERE columna2 < '20' and columnb2 = 'Cat' ))

```

This is not working of course. I ran this statement in phpMyAdmin and it gave me the error....

> 
> Every derived table must have it's own alias.
> 
> 
> 

So rewrote the SQL statement as...

```
SELECT SUM(usernames) AS total 
   FROM 
   ((SELECT count(username) AS usernames 
        FROM table1 a 
        WHERE a.columa < '20' AND a.columnb = 'c' ) 
   UNION ALL 
   (SELECT count(name) AS usernames 
       FROM table2 b 
       WHERE b.columna2 < '20' and b.columnb2 = 'Cat' ))

```

This also gives me the same error...

> 
> Every derived table must have it's own alias.
> 
> 
> 

What am i missing here?

Answer: 
The select statements are missing the alias, not table1 and table2 specifically. Try removing parenthesis:

```
SELECT SUM(usernames) AS total 
   FROM 
   (SELECT count(username) AS usernames 
        FROM table1 
        WHERE columa < '20' AND columnb = 'c'  
   UNION 
   SELECT count(name) AS usernames 
       FROM table2 
       WHERE columna2 < '20' and columnb2 = 'Cat' ) a

```

Or creating aliases:

```
SELECT SUM(usernames) AS total 
   FROM 
   ((SELECT count(username) AS usernames 
        FROM table1 
        WHERE columa < '20' AND columnb = 'c' )  a
   UNION ALL 
   (SELECT count(name) AS usernames 
       FROM table2 
       WHERE columna2 < '20' and columnb2 = 'Cat' ) b) c

```
```
SELECT *
  FROM 
     ( SELECT 1 n) -- this a derived table, with a column alias (n)
     x -- and this is a table alias for said table

```