VOOZH about

URL: https://thenewstack.io/3-sql-writing-tips-and-tricks-to-enhance-productivity/

⇱ 3 SQL Writing Tips and Tricks To Enhance Productivity - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2025-03-05 07:00:59
3 SQL Writing Tips and Tricks To Enhance Productivity
sponsor-oracle,sponsored-post-contributed,
Data / Databases / Programming Languages

3 SQL Writing Tips and Tricks To Enhance Productivity

Using common table expressions, good table aliases and your editor’s formatting tools can make SQL easier to read and debug.
Mar 5th, 2025 7:00am by Chris Saxon
👁 Featued image for: 3 SQL Writing Tips and Tricks To Enhance Productivity
Featured image by Getty Images on Unsplash+.
Oracle sponsored this post.
If you’re a database developer, database administrator or data analyst, writing SQL to get data into and out of databases is a key part of your job. Doing this quickly and effectively improves your productivity. Conversely, working with a tangled mess of statements and data leaves you stuck. In the first part of this series, I demonstrated how choosing good names, normalizing your tables and creating constraints give you a solid structure to be productive when writing SQL. In this article, I’ll cover ways to structure SQL to make it easier to read and debug. Techniques such as common table expressions (CTEs) and table aliases can transform statements from indecipherable riddles to clear logic.

Structure Queries Clearly

Large SQL statements can be hard to read and debug. CTEs, aka the with clause, enable you to break them into smaller parts. CTEs are named subqueries that come at the top of select statements. You access these subqueries like regular tables later in the query. This brings a few benefits:
  • You can build the query bit-by-bit.
  • You can give each CTE a meaningful name.
  • You can check the results of each CTE.
For example, the Oracle Dev Gym offers free quizzes, workouts and classes to help you learn SQL. Each of these activities has its own tables. Combining all these in one query to report all activities is a daunting task. Using the with clause, you can create a CTE for each activity type. You can start with getting quiz totals:
with quiz_totals as ( … ) 
select * from quiz_totals
Then add workout totals and verify they are correct:
with quiz_totals as ( … ), 
 workout_totals as ( … ) 
select * from workout_totals
Repeat this for class totals and combine the results of each CTE to get all totals, like so:
with quiz_totals as ( … ), 
 workout_totals as ( … ), 
 class_totals as ( … ), 
 all_totals as ( 
 select * from quiz_totals union all 
 select * from workout_totals union all 
 select * from class_totals 
 ) 
select * from all_totals
If you need to change the queries for any activity type, it’s clear that the logic is contained in the corresponding CTE. It is far simpler than hunting through a mass of nested subqueries. Using CTEs to break up logic into smaller problems makes the process more manageable. However, each CTE can still reference many tables. Whenever you’re working with many tables, there’s an important question to answer: Which columns belong to which table? Make this clear by prefixing each column with its table’s alias.

Use Good Table Aliases

Without table aliases, knowing where each column is from is tough. This makes queries harder to understand and change. However, unaliased columns have a bigger problem: they can lead to errors. The most common issue is when two tables have columns with the same name. If you use the unaliased name, the database cannot identify which table it’s from, and the statement will fail. What’s worse is this problem can affect existing SQL if you add a column that causes a name clash. Qualifying columns with their table avoids these problems. Single-letter table aliases taken from the start of the table name are appealing but can quickly lead to problems. For example, say you write a query that accesses both the customers and contracts tables. If you give one the alias “c,” how do you know which it relates to without scrolling through the statement? A better approach is to use four-character aliases taken from the start of the table name:
  • For a single-word table, the alias is its first four characters.
  • Two-word tables take the first two letters of each word.
  • Three-word tables use the first two letters of the first word and the first letter of the last two words.
  • Four-word tables use the first character of each word.
For example,
  • customers => cust
  • order_items => orit
  • shipment_list_batches => shlb
In rare cases, this gives different tables the same alias. If this happens, pick a new alias for one table, following this system as closely as possible. If you need to access the same table twice in a query, add a prefix to the alias stating the table’s role. The columns you’re joining them on are a good source for this. For example, you may need to join customers to their delivery and payment addresses, both stored in the addresses table. Adding deli or paym as appropriate makes it clear which role the address table plays:
from customers cust
join addresses deli_addr 
on cust.delivery_address_id = deli_addr.address_id
join addresses paym_addr
on cust.payment_address_id = paym_addr.address_id
Using a standard aliasing system quickly becomes second nature, makes it clear which table columns belong to and avoids errors. A standard structure is key to further aid the readability of your code.

Use a Consistent Style

The best way to format your SQL is the source of many debates. We all have our own preferences for where and how to indent clauses. Whether keywords should be in uppercase or lowercase is a long-running battle. Ultimately, most of these choices come down to personal preference. So, the most important advice is:

Choose a formatting style and stick to it.

However you like to format your SQL, we can all agree that mixing and matching styles within a statement like this is jarring and hard to read:
SELECT Some_Columns 
 From a_table 
 JOIN another_table 
on …
The best way to ensure a standard style is to use your editor’s auto formatter. Run it after writing each statement. This is quicker than formatting as you go. You can also share the rules with your colleagues to keep your whole codebase formatted similarly. Occasionally, auto formatters can struggle to spot where to place line breaks in complex SQL using niche features. This can result in combining expressions into long lines that scroll right off the edge of the screen. If you hit this problem, a trick to overcome it is to place an empty comment where you want line breaks. The formatter has to respect these, guaranteeing a line break exactly where you want it. For example:
select case -- 
 when formatted_lines_are_too_long --
 then 'Use comments to break them up' --
Using a standard formatter is one of the many ways your editor can help you write SQL faster, so it’s worth investing time to learn your editor’s productivity features.

Get To Know Your Editor

You’ve likely enabled autocomplete for table and column names to help you write SQL. But this is just one way your tools can help you be more productive. For example, the Oracle SQL Developer extension for VS Code has a few gems to help you. You can drag tables or columns from the schema browser into the editor. It then asks you whether to use these in a select, insert, update or delete statement: 👁 GIF shows dragging tables or columns from the schema browser into the editor.
This saves you from having to type out every column by hand, a tedious task for tables with many columns. You can also configure code snippets in VS Code that expand short sequences into large code blocks. Here are a few that I rely on to speed up the process of writing SQL:
  • ssf => select * from
  • ii => insert into $1 values ( $2 )
  • crt => create table $1 ( c1 int );
  • drt => drop table $1 cascade constraints purge;
Spending time learning the keyboard shortcuts in your editor for everyday tasks can also reap good rewards.

Conclusion

Business requirements can be complex. Translating these to SQL can be challenging and can lead to massive monstrosities if you’re careless. Taking care to structure SQL clearly by using CTEs and good table aliases can speed up SQL’s writing and maintenance processes. Using auto-formatters and other tools in your editor can further streamline tasks and enhance your productivity. However, as we saw in the first part of this series, the most significant gains come from building a solid data model. Choosing good names, normalizing your tables and creating constraints make understanding your schema simple and writing SQL a snap.
Oracle offers a wide range of technologies for building, testing, and maintaining applications in the cloud and in your data center. Find free tools and learning resources at oracle.com/developer
Learn More
The latest from Oracle
TRENDING STORIES
Chris is a developer advocate for Oracle Database, where his job is to help you get the best out of it and have fun with SQL. You can find him on Twitter, @ChrisRSaxon, and on his blog, All Things SQL.
Read more from Chris Saxon
Oracle sponsored this post.
SHARE THIS STORY
TRENDING STORIES
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.