DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Detecting Plan Regression in SQL Server Using Query Store
  • AI Paradigm Shift: Analytics Without SQL
  • Beyond Partitioning and Z-Order: A Deep Dive into Liquid Clustering for Unity Catalog Managed Tables
  • One Query, Four GPUs: Tracing a Distributed Training Stall Across Nodes

Trending

  • Real-Time AI Inference at Scale Using Cloud Run, GPUs, and Vertex AI
  • Mocking Kafka for Local Spring Development
  • AWS Kiro: The Agentic IDE That Makes Specs the Unit of Work
  • The Hidden Latency of Autoscaling
  1. DZone
  2. Data Engineering
  3. Databases
  4. Visualizing SQL Statements

Visualizing SQL Statements

By 
Mpumelelo Msimanga user avatar
Mpumelelo Msimanga
·
Apr. 07, 14 · Interview
Likes (0)
Comment
Save
Tweet
Share
18.8K Views

Join the DZone community and get the full member experience.

Join For Free

Usually if I concentrate I am able to understand most SQL statements. There are times though such as:

  • When a set of tables is not familiar
  • When I did not write the SQL statement
  • When the SQL statement is long and involves many tables and joins
  • When I want to discuss a statement with a colleague
  • All of the above

Having a visual representation of a SQL statement can be helpful in deciphering the statement. My visualisation tool of choice for SQL is an Open Source application called Reverse Snowflake Joins (REVJ). As the name implies, this tool shines when it comes to showing you how your tables are related.

I have installed the tool on my workstation but when I am on the move I use the online version of the tool. Using the tool is straight forward, simply paste your SQL statement in the text area and generate the diagram, the online version generates an SVG image. I have at times found that the tool struggles with complex CASE statements. In such cases I remove the CASE statement and just include the fields used in the case statement. Below is a sample statement to show REVJ at work.

SELECT
    a.prod_cat_name
    ,b.prod_name
    ,c.prod_owner_name
    ,p.promo_id
    ,pt.promo_type
    ,sum(s.units) as total_units
    ,sum(s.sale_price) as total_sale_price
    ,sum(prev_s.units) as prev_yr_total_units
FROM
    product_category a
    JOIN product b ON a.product_cat_id = b.product_cat_id
    LEFT OUTER JOIN product_owner c ON a.product_cat_id = c.product_cat_id
    JOIN sales s ON b.product_id = s.product_id
    JOIN sales prev_s ON s.sale_year = prev_s.sale_year-1
    LEFT OUTER JOIN promotion p ON s.promo_id = p.promo_id
    RIGHT OUTER JOIN promo_type pt ON p.promo_type_id = pt.promo_type_id
WHERE
    pt.promo_type IN ('Email', 'TV')
    AND a.prod_cat_name = 'Electronics'
    AND s.sale_year >=2013
GROUP BY
    a.prod_cat_name
    ,b.prod_name
    ,c.prod_owner_name
    ,p.promo_id
    ,pt.promo_type
HAVING
    sum(s.units)>100
        

The generated image shown below. Notice how the filters applied to each table are also shown, further simplifying the task of understanding the SQL statement.

For more complex examples have a look at big samples page.
sql

Published at DZone with permission of Mpumelelo Msimanga. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Detecting Plan Regression in SQL Server Using Query Store
  • AI Paradigm Shift: Analytics Without SQL
  • Beyond Partitioning and Z-Order: A Deep Dive into Liquid Clustering for Unity Catalog Managed Tables
  • One Query, Four GPUs: Tracing a Distributed Training Stall Across Nodes

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook