Introduction
Sometimes, you need to write SQL directly to do things that are hard or slow with Rails helpers. Raw SQL lets you talk straight to the database.
When you want to run a complex query that Rails can't easily build.
When performance is critical and you need a faster query.
When you want to use database-specific features not supported by Rails.
When debugging or testing SQL queries before adding them to Rails.
When you need to run a custom update or delete that ActiveRecord doesn't support.