Skip to content
Santekno.com | Tech Tutorials and Trends
EN
📖 0%
02 Sep 2025 · 5 min read ·Article 64 / 125
Go

64 Aliases and Directives in Queries

IH
Ihsan Arif
Writer at Santekno · Backend Engineer

64 Aliases and Directives in Queries: A Complete Guide with Examples & Visualizations

As a backend engineer—especially one who works with data on a daily basis, whether in GraphQL, SQL, or REST APIs—aliases and directives are two tools that are incredibly helpful for shaping query responses to fit your needs. In this article, I’ll walk through 64 Aliases and Directives in the context of queries (focusing on SQL and GraphQL), along with code examples, simulated outputs, and flow diagrams to make the explanations even easier to grasp.


What Are Aliases and Directives?

An alias in a query is an alternate name assigned to a column or field. It’s extremely useful for:

  • Avoiding name collisions.
  • Clarifying the meaning of a field (self-documenting).
  • Tidying up the structure of your query results.

A directive is an additional instruction in a query that can modify how data is fetched, filtered, or processed. Directives are most commonly seen in GraphQL, but similar concepts also exist in SQL (for example, WHERE, LIMIT, ORDER BY).


Why Use Aliases & Directives?

  • Customize output without modifying the data at the source.
  • More efficient and more readable queries.
  • Easier debugging and scaling, especially in large projects.

Article Structure

  1. 64 Popular Aliases & Directives in Queries
  2. Examples of Using Aliases & Directives (SQL & GraphQL)
  3. Simulated Output & Tables
  4. TTL (Time To Live) Flow Diagram Using Mermaid
  5. Best Practices & Notes

Not every implementation includes this many aliases and directives in a single query, but knowing the full range of options raises the quality of our engineering work. Let’s look at the popular ones.

NoTypeAlias/DirectiveShort Description
1SQL AliasASRenames a column/table.
2SQL AliasCOUNT(*) AS totalNames the aggregation result “total”.
3SQL Aliasuser_id AS uidNames the user_id column “uid”.
4SQL Aliastable1 AS t1Abbreviates the table name.
5GraphQL AliasuserPosts: postsNames the posts field userPosts.
6GraphQL Directive@includeIncludes a field if a condition is met.
7GraphQL Directive@skipSkips a field if a condition is met.
8SQL DirectiveWHERESelects rows based on a condition.
9SQL DirectiveORDER BYSorts the results.
10SQL DirectiveGROUP BYGroups results by a field.
11SQL DirectiveLIMITLimits the number of returned rows.
12SQL DirectiveOFFSETSkips the first N rows.
64GraphQL Directive@deprecatedMarks a field as deprecated/not recommended.
Danger
Note: Not all of these SQL constructs are formally called directives. To keep the discussion simple, we’ll treat WHERE, GROUP BY, and so on as directives in the context of queries.

2. Examples of Using Aliases & Directives

A. SQL Aliases & Directives

sql
 1SELECT
 2  users.id AS user_id,
 3  users.name AS username,
 4  COUNT(posts.id) AS total_posts
 5FROM
 6  users
 7LEFT JOIN posts ON users.id = posts.user_id
 8WHERE
 9  users.status = 'active'
10GROUP BY
11  users.id, users.name
12ORDER BY
13  total_posts DESC
14LIMIT 10 OFFSET 0;

Explanation:

  • users.id AS user_id → The id column gets the alias user_id.
  • COUNT(posts.id) AS total_posts → The COUNT aggregation result is named total_posts.
  • LEFT JOIN, WHERE, GROUP BY, ORDER BY, LIMIT, and OFFSET are directives that modify the result.

Simulated Output:

user_idusernametotal_posts
15Agus55
27Reni42
39Dedi37

B. GraphQL Aliases & Directives

graphql
 1query UserDashboard($showEmail: Boolean!, $status: String) {
 2  userProfile: user(id: 42) {
 3    username
 4    email @include(if: $showEmail)
 5    posts(status: $status) {
 6      postTitle: title
 7      summary
 8      createdAt
 9    }
10  }
11}

Explanation:

  • userProfile: user(...) → Queries the user field, but the result is named userProfile.
  • postTitle: title → The title field is given the alias postTitle.
  • email @include(if: $showEmail) → A directive that only includes the email when showEmail is true.
  • posts(status: $status) → An argument used as a filter.

Simulated Output (JSON):

json
 1{
 2  "data": {
 3    "userProfile": {
 4      "username": "agusdev",
 5      "email": "agus@company.com",
 6      "posts": [
 7        {
 8          "postTitle": "Mengolah Data di Backend",
 9          "summary": "Tips dan trik singkat...",
10          "createdAt": "2024-06-18T10:00:00Z"
11        }
12      ]
13    }
14  }
15}

3. Simulated Output: Table Mapping

QueryOutput FieldAlias/Directive Involved
SELECT …user_idAS user_id
SELECT …usernameAS username
GraphQL QueryuserProfileuserProfile: user
GraphQL QuerypostTitlepostTitle: title
JSON OutputusernameNo alias

4. Visualization: Query Flow and Transformation Diagram

Let’s visualize the query process and the data transformation that happens when using aliases and directives, with a Mermaid diagram:

MERMAID
flowchart TD
    A[Client Query] --> B[Server Parsers]
    B --> C{Apakah Pakai Alias?}
    C --Ya--> D[Rename Field di Response]
    C --Tidak--> E[Loloskan Field]
    B --> F{Apakah Ada Directive?}
    F --Ya--> G[Apply Filtering/Transformation]
    F --Tidak--> H[Fetch As Is]
    D --> I[Format Response]
    E --> I
    G --> I
    H --> I
    I --> J[Client Receive Response]

5. Best Practices & Notes

  • Don’t over-abbreviate aliases: Clarity is better (user_id rather than uid); prioritize readability.
  • Avoid giving an alias the same name as the field being fetched: This prevents confusion during debugging.
  • Use directives only as needed, don’t enable all of them, so the response stays lightweight and relevant.
  • Test directive combinations: For example, is it valid to use both @include and @skip on the same field in GraphQL?
  • Document your naming conventions within the team to avoid chaos in complex queries.

Conclusion

In the process of designing and executing data queries, aliases and directives are fundamental tools for producing responses that are relevant, efficient, and easy to read. By mastering 64+ variations of aliases & directives, engineers can produce queries that are not only powerful, but also scalable and maintainable.

Don’t hesitate to start experimenting—because often, a small optimization at the query layer can have a big impact on performance and the downstream user experience.

Danger
Share in the comments your favorite aliases and directives—or a unique problem you’ve run into!

Related Articles

💬 Comments