In the last few years, I have worked on various Ruby on Rails (RoR) projects.

I had a chance to put my hands on SQL queries only a few times, which is pretty common in RoR projects.

To access the database RoR uses a Ruby gem called ActiveRecord most of the time. It allows accessing MySQL and Postgres and other relational DB without any knowledge of SQL.

Most of Rails developers are glad not to touch SQL queries. So after a few years, I forgot how to write any queries that are more complex than Select ... From ..., and that became a serious problem at some point.

When I started working for a FinTech start-up, I had to maintain a project that was using a great number of SQL queries.

There were good reasons to use raw SQL queries instead of ActiveRecord:

  • To work efficiently with large tables containing millions of rows.

  • To create complex reports that make use of advanced SQL, such as partitions and subqueries.

I quickly realised that it was time to (re)learn SQL.

To start I took an online course on Udemy, but It turned out that it was too basic. If you don't know the basic of SQL it is a good course, otherwise, find something more challenging.

I then bought the book SQL Cookbook (O'Reilly - Antonio Molinaro. That was a really good purchase. As expected the book was full of many recipes regarding SQL problems, starting from the very easy ones to the complex multiline queries that are common in data reporting and warehousing.

What I really appreciate about the book are:

  • All the recipes provide a clear descriptive explanation or the queries.
  • The solutions are provided for different SQL database servers: MySql, Postgress, Oracle and SQL Server.

SQL cookbook is not about learning SQL from scratch (even if it starts with easy recipes), but it helps to practise writing queries. To get the most it, after reading the problem do not jump on reading the solution. Make your brain sweat and try to jot down a solution, and only after that, you should read the solution.

I hope you found this post helpful.

Thanks for reading!