Skip to content
Advertisement

Trying to make a new table by pulling data from two tables and keep getting ‘Error: Every derived table must have its own alias’ on this query

I have an ‘Orders’ table and a ‘Records’ table.

Orders table has the following columns:

Records table has the following columns:

I’m trying to pull and compile the following list of data but I keep getting an error message:

Here’s my query:

What am I doing wrong?

Advertisement

Answer

The subquery in the join needs an alias. It also needs to return the order_id column, so it can be joined.

I would actually write your query as:

Rationale:

  • you don’t want, and need, distinct in the aggregate functions here; it is inefficient, and might even yield wrong results

  • count(*) is more efficient count(<expression>) – so, use it, unless you know why you are doing otherwise

  • I removed an unecessary level of nesting

If there are orders without records, you might want a left join instead.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement