How to make join queries using Sequelize on Node.js
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Sequelize performs joins using the include option in queries, not raw SQL JOIN syntax. You first define associations between models (hasMany, belongsTo, hasOne, belongsToMany), then use include in findAll or findOne to eagerly load related data. Sequelize translates these into SQL LEFT OUTER JOIN statements by default. Understanding the association types and include syntax is the key to effective join queries in Sequelize.
Define Associations
Basic Join: include
Nested Joins (Multiple Levels)
Filtering Joined Data (WHERE on Association)
Adding where to an include changes it from LEFT JOIN to INNER JOIN. To keep LEFT JOIN with filtering:
required: true vs required: false
Many-to-Many Join
Selecting Specific Columns
Aliased Associations
Ordering Joined Data
Counting and Aggregating with Joins
Common Pitfalls
- Forgetting to define associations before querying:
include: [Post]only works ifUser.hasMany(Post)(orPost.belongsTo(User)) was called first. Without the association definition, Sequelize throwsError: Post is not associated to User!. - N+1 query problem: Without
include, accessinguser.getPosts()for each user in a loop triggers a separate SQL query per user. Useincludeto load all related data in a single query (eager loading). required: truewhenwhereis used: Addingwhereinside anincludeautomatically switches toINNER JOIN. Users without matching posts are excluded from results. Setrequired: falseexplicitly if you want all users regardless of the filter match.- Circular or redundant includes: Including a model that includes its parent back creates circular queries and can cause performance issues or stack overflows. Only include in one direction per query.
- Alias mismatch: If you define an association with
as: 'writtenPosts', you must use the sameasvalue in theinclude. Using the model directly (include: [Post]) will not find the aliased association.
Summary
- Define associations first (
hasMany,belongsTo,belongsToMany), then useincludein queries include: [Model]performs a LEFT OUTER JOIN by default- Add
whereinsideincludeto filter joined data (switches to INNER JOIN) - Use
required: falseto force LEFT JOIN even withwhereconditions - Nest
includewithinincludefor multi-level joins - Use
attributesto limit which columns are fetched from joined tables
Related reading
- How to make MySQL handle UTF-8 properly
- How to make OleDb code run asynchronous?
- how to make oracle UTL_HTTP.request asynchronous?
- How to make primary key as autoincrement for Room Persistence lib
- How to make layout with rounded corners..?
- how to make synchronous call to indexeddb method from javascript
- How to make Sequelize use singular table names
- How to make Spring server to start even if database is down?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.