Skip to content
Advertisement

Simplification of SQL Query

SQL is not my specialty. I have a working query that accomplishes what I need it to, but I know there has got to be a cleaner way of accomplishing my goal.

The query should return:

Here is a SQLFiddle link to where you can find the tables and my solution: http://sqlfiddle.com/#!9/f2303f/2. Alternatively, here is the schema build:

And here is the query:

Specifically, I’d like to try and condense the latter two left joins into one. Any advice?

Advertisement

Answer

You can left join a single derived table in which the aggregation is done.

And you should also learn to use proper quotes as near as possible to standard SQL to minimize compatibility problems when porting to another DBMS. For literals only use single quotes and only use single quotes for literals. For identifiers only use back ticks and only use back ticks for identifiers. Do not use double quotes at all. In standard SQL and therefore many other DBMS double quotes are for identifiers, like back ticks in MySQL.

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