This series of articles
This is the first of the Code In Postgres series of articles.
When developing systems we often have a choice of writing code (NodeJS, C#, Python or PHP etc) or SQL. I believe that sometimes the decision to write code is taken without fully evaluating how much of the task could be offloaded to SQL.
In this series of articles I wish to show the huge benefits of using and learning SQL by examining progressively more difficult scenarios with increasing amounts of SQL knowledge. In doing this I hope to illustrate that sometimes large amounts of extra code is written for what SQL can achieve quicker, with less complexity and more readability.
To be more specific, we should try to follow the rule of least power more often.
About the Ergast data set
For this series of articles we will be using the Ergast data set, which is a provided under the Attribution-NonCommercial-ShareAlike 3.0 Unported Licence.
Lets say you want to find out who the Formula 1 World Champion was in 2017. The schema for the tables we will be using is as follows:
An example of the data you would find in these tables is shown below:
driverStandings table has the points for every driver in every race and world champions would be the driver with the most points in a single season. The problem here is that there is no record in the
driverStandings table for which season a
raceId belongs to. So we need to get a bit creative… Here is one possible solution:
- Looking at the
yearcolumn, we can find all the
- Find the
driverIdfor all of those
raceIdby reading the
- Sort them by
- The very first row contains the
driverIdwhich has the most points in that season. This
driverIdis the world champion.
WHERE statement along with the ability to only return certain fields using
SELECT. After this we will see how this can be accomplished in one single SQL statement using
ORDER BY and
This code, despite there being a lot of it is relatively straight forward. We get a list of
raceId from the
qryRaces function and pass this to the
qryStandings function. The
qryStandings function fans out a series of Promise and collates them back again returning the standings of all drivers after all races in 2017, which is quite a lot of unnecessary data. Once we have all this data we then use a series of (included) library functions to sort the data by points and take the first result.
- There’s some nice re-usable functions here.
- The main code is quite concise and easy to understand.
- Longer than SQL
- We downloaded a reasonably large amount of data from the database, which could be slow.
- There are some non trivial constructs here such as flattening Arrays and using Promise.all to wait for a series of Promises.
IN clause can be used to match a column against a set of numbers. For example we may choose to write
WHERE "raceId" IN (4, 5, 7) which would be the same thing as writing
WHERE "raceId" = 4 OR "raceId" = 5 OR "raceId" = 7.
The smart thing here is that we are using a query to return the numbers for the
IN clause instead of a directly specified list.
ORDER BY statements are used to perform sorting of the record set, you can sort by multiple fields or use many types of expressions.
LIMIT controls how many items to return. It can also be combined with
OFFSET to skip rows.
- The inside of the
INclause can be ran and understood individually.
- Is the
limit 1a trick?
- It seems in code you can give the contents of IN clause a name (
raceIds) but this is not possible using SQL’s
IN, or is it?.