Query Basics

Let’s start with the basics. We’ll take a quick look at Ecto’s query syntax, then start writing some simple queries, including some that integrate user input.

The Query module uses Elixir macros to create a DSL (domain-specific language) that sits right in your Elixir code. The DSL syntax feels a lot like Elixir, but it’s a little more fluid and makes writing queries feel more natural.

For example, here’s a SQL query based on the data model in our sample app. You can run this in a Postgres console to see what it does:

  SELECT t.id, t.title, a.title
  FROM tracks t
  JOIN albums a ON t.album_id = a.id
  WHERE t.duration > 900;

And here’s that same query written in Ecto:

  query = from ...

Get Programming Ecto now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.