Visualising SQL Queries for "normal" humans

I need to write some code and explain it to real humans.

The code will query a database and give a result combining info from several tables.

For a simple worked example, lets assume I have a database with customers, products and orders in tables. And my query is to show all customers who purchased a certain product type over time.

The SQL might be something like:

--- code to build the tables
CREATE TABLE tbl_customer
( customer_id   varchar(10) NOT NULL,  
  customer_name   varchar(40) NOT NULL,  
  customer_tel     varchar(10) ,  
  customer_address1 varcahr(80),
  customer_address2 varcahr(80),
  customer_address3 varcahr(80),
  customer_city varcahr(25),
  customer_postcode varcahr(10),
 
);  
GO

CREATE TABLE tbl_product
( product_id   int NOT NULL,  
  product_type   varchar(11) NOT NULL,  
  product_name   varchar(40) NOT NULL,
  product_price money NOT NULL,
  product_tax bolean,
);  
GO

CREATE TABLE tbl_order
( order_id   int NOT NULL,  
  product_id   int NOT NULL,  
  customer_id     varchar(10) NOT NULL,
  FOREIGN KEY(product_id) REFERENCES tbl_product(product_id),
  FOREIGN KEY(customer_id) REFERENCES tbl_customer(customer_id),
);  
GO

--- code to query the tables
SELECT
  C.customer_id,
  C.customer_name,
  C.customer_tel,
  O.order_date,
  P.product_name

FROM 
  tbl_customer AS C

LEFT JOIN 
  tbl_order O ON C.customer_id = customer_id 
LEFT JOIN 
  tbl_product P ON O.product_id = product_id 

WHERE  
P.product_type = 'chocolate'

By any chance is there some really clever R tool that can take that code and visualise it into something like an ER diagram. (ER diagrams usually need the relationship etc)

mssql_1752610653308
er_1752610700600

The two diagrams above come from a webtool SQLFlow. But to do it, I'm creating the table structure in the SQL which I don't want to do (my real database is massive, and a lot of the relationships are not defined by the database author).

My gut feeling says there will be a R package and I just don't know it!

I'm not sure about automatically creating the diagram from analyzing the SQL, but for generating the diagram some common packages to consider include DiagrammeR, dm, and datamodelr. Here's a DiagrammeR example:

library(DiagrammeR)

# Create an ER diagram
grViz("
digraph ER {
  graph [layout = dot, rankdir = LR]
  
  # Define nodes (tables)
  node [shape = record, style = filled, fillcolor = lightblue]
  
  Customer [label = '{Customer|customer_id (PK)\\lcustomer_name\\lcustomer_tel\\lcustomer_address1\\l...}']
  Product [label = '{Product|product_id (PK)\\lproduct_type\\lproduct_name\\lproduct_price\\lproduct_tax}']
  Order [label = '{Order|order_id (PK)\\lproduct_id (FK)\\lcustomer_id (FK)\\lorder_date}']
  
  # Define relationships
  Customer -> Order [label = '1:M']
  Product -> Order [label = '1:M']
}
")