PREDICATE - FILTER - SCOPE
DATABASE CONCEPT/TERMs
AI ASSISTs

UNDERSTANDING PREDICATE, QUERY FILTERING, AND QUERY SCOPE
(USING MULTIPLE RELATED-TABLES)

"how data is retrieved and structured LOGICALLY"

This guide explains the concepts using multiple RELATED tables (users, orders, products) so you can see how predicate, filtering, and scope work across relationships (JOIN).

EXAMPLE DATASET: RELATED TABLES

USERS TABLE

id name status role
1Andiactiveadmin
2Budiinactiveuser
3Citraactiveuser
4Dediactiveadmin

ORDERS TABLE

id user_id product_id status total
1011201paid150000
1022202pending50000
1033203paid200000
1044204cancelled75000

PRODUCTS TABLE

id name price stock
201Laptop100000005
202Mouse1500000
203Keyboard30000010
204Monitor20000002

PREDICATE (CONDITION)

users.status = "active"

users.role = "admin"

orders.status = "paid"

products.stock > 0

  

👉 These predicates define conditions across multiple related tables.

QUERY FILTERING (PROCESS WITH JOIN)

SELECT users.name, orders.id, products.name, orders.total

FROM users

JOIN orders ON users.id = orders.user_id

JOIN products ON products.id = orders.product_id

WHERE users.status = "active"

  AND orders.status = "paid"

  AND products.stock > 0;

  

RESULT (ACTIVE USERS WITH PAID ORDERS & AVAILABLE PRODUCTS):

user_name order_id product_name total
Andi101Laptop150000
Citra103Keyboard200000

Explanation:

  • users.status = "active" → only active users
  • orders.status = "paid" → only paid orders
  • products.stock > 0 → only available products

QUERY SCOPE (REUSABLE FILTERING WITH RELATION)

FUNCTION scopeActiveUsers(query):

    RETURN query WHERE users.status = "active"

FUNCTION scopePaidOrders(query):

    RETURN query WHERE orders.status = "paid"

FUNCTION scopeAvailableProducts(query):

    RETURN query WHERE products.stock > 0

  

USING MULTIPLE SCOPES

query = SELECT users.name, orders.id, products.name, orders.total

        FROM users

        JOIN orders ON users.id = orders.user_id

        JOIN products ON products.id = orders.product_id

query = scopeActiveUsers(query)

query = scopePaidOrders(query)

query = scopeAvailableProducts(query)

RESULT = EXECUTE(query)

  

👉 Same result as filtering, but now reusable and modular.

BIG PICTURE FLOW

Predicate (multi-table) 
→ Filtering (JOIN)
→ Scope
→ Reuse
→ Result

FINAL CONCLUSION

  • Predicate: logical conditions across tables
  • Query Filtering: applying conditions using JOIN + WHERE
  • Query Scope: reusable filtering logic across relationships

SIMPLE INTUITION

  • Predicate = rules (even across tables)
  • Filtering = selecting related data
  • Scope = reusable multi-table filter

Comments