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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • MySQL Instant DDL: Metadata Locks, Waiting Writers, and Query Pileups
  • Taming Parameter Sniffing in SQL Server 2022 With PSP Optimization
  • PostgreSQL vs Oracle: When Higher Cost Means Better Performance
  • The English WHERE Clause: Plain-English Filters in Couchbase SQL++ Using Jev Model

Trending

  • Building a Grounded Search Agent With Foundry, Azure AI Search, and Cosmos DB, Containerized With Docker and Observed With Grafana
  • A Walk-Through of the DZone Article Editor
  • Introduction to Error Prone: How to Catch Bugs at Compile Time
  • Microsoft Fabric Throttling: Diagnose Before You Scale

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Databases
  4. Visualizing SQL Statements

Visualizing SQL Statements

By 
Mpumelelo Msimanga
·
Apr. 07, 14 · Interview
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
19.0K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • MySQL Instant DDL: Metadata Locks, Waiting Writers, and Query Pileups
  • Taming Parameter Sniffing in SQL Server 2022 With PSP Optimization
  • PostgreSQL vs Oracle: When Higher Cost Means Better Performance
  • The English WHERE Clause: Plain-English Filters in Couchbase SQL++ Using Jev Model

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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