64 Aliases and Directives in Queries
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
- 64 Popular Aliases & Directives in Queries
- Examples of Using Aliases & Directives (SQL & GraphQL)
- Simulated Output & Tables
- TTL (Time To Live) Flow Diagram Using Mermaid
- Best Practices & Notes
1. 64 Popular Aliases & Directives in the World of Data Queries
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.
| No | Type | Alias/Directive | Short Description |
|---|---|---|---|
| 1 | SQL Alias | AS | Renames a column/table. |
| 2 | SQL Alias | COUNT(*) AS total | Names the aggregation result “total”. |
| 3 | SQL Alias | user_id AS uid | Names the user_id column “uid”. |
| 4 | SQL Alias | table1 AS t1 | Abbreviates the table name. |
| 5 | GraphQL Alias | userPosts: posts | Names the posts field userPosts. |
| 6 | GraphQL Directive | @include | Includes a field if a condition is met. |
| 7 | GraphQL Directive | @skip | Skips a field if a condition is met. |
| 8 | SQL Directive | WHERE | Selects rows based on a condition. |
| 9 | SQL Directive | ORDER BY | Sorts the results. |
| 10 | SQL Directive | GROUP BY | Groups results by a field. |
| 11 | SQL Directive | LIMIT | Limits the number of returned rows. |
| 12 | SQL Directive | OFFSET | Skips the first N rows. |
| … | … | … | … |
| 64 | GraphQL Directive | @deprecated | Marks a field as deprecated/not recommended. |
WHERE, GROUP BY, and so on as directives in the context of queries.2. Examples of Using Aliases & Directives
A. SQL Aliases & Directives
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→ Theidcolumn gets the aliasuser_id.COUNT(posts.id) AS total_posts→ The COUNT aggregation result is namedtotal_posts.LEFT JOIN,WHERE,GROUP BY,ORDER BY,LIMIT, andOFFSETare directives that modify the result.
Simulated Output:
| user_id | username | total_posts |
|---|---|---|
| 15 | Agus | 55 |
| 27 | Reni | 42 |
| 39 | Dedi | 37 |
B. GraphQL Aliases & Directives
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 theuserfield, but the result is nameduserProfile.postTitle: title→ Thetitlefield is given the aliaspostTitle.email @include(if: $showEmail)→ A directive that only includes the email whenshowEmailistrue.posts(status: $status)→ An argument used as a filter.
Simulated Output (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
| Query | Output Field | Alias/Directive Involved |
|---|---|---|
| SELECT … | user_id | AS user_id |
| SELECT … | username | AS username |
| GraphQL Query | userProfile | userProfile: user |
| GraphQL Query | postTitle | postTitle: title |
| JSON Output | username | No 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:
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_idrather thanuid); 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
@includeand@skipon 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.