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
Featured image by Getty Images on Unsplash+.
Oracle sponsored this post.
Structure Queries Clearly
Large SQL statements can be hard to read and debug. CTEs, aka thewith 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.
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
with quiz_totals as ( … ),
workout_totals as ( … )
select * from workout_totals
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
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.
customers=>custorder_items=>oritshipment_list_batches=>shlb
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
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 …
select case --
when formatted_lines_are_too_long --
then 'Use comments to break them up' --
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 aselect, insert, update or delete statement:
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 * fromii=>insert into $1 values ( $2 )crt=>create table $1 ( c1 int );drt=>drop table $1 cascade constraints purge;
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.
YOUTUBE.COM/THENEWSTACK
Tech moves fast, don't miss an episode. Subscribe to our YouTube
channel to stream all our podcasts, interviews, demos, and more.