DBeaver's Visual Query Builder

 Quick! You need to create a complex query, but you don't remember how to join the tables. And you need to get it done ASAP.

You are in luck if you have an enterprise edition of DBeaver handy.  You can drag and drop the tables and then adjust the clauses as needed without having to dig into the table definitions.

1. Open the database 

The PostgreSQL DVD Rental database. 

I am using the PostgreSQL DVDRental dataset for these examples. I have opened the connection and listed the tables.

As you can see, the database contains numerous tables. 

We are going to build a query visually to get information about the customers.










2. Open a SQL Console 

Open SQL Console

The second step requires an SQL Console. You will find it under the SQL Editor tab.





3. Select Builder 

On the right side, Select Builder

On the right side of the console pane, tilted 90 degrees, is a tab labeled Builder. 


4. You are ready to drag and drop tables

 

The Query Builder Is Ready to use. 
You have three new panes. The top one is where you drop the tables.  The bottom left is where we tailor the query. The query will then appear in the bottom right pane.

Now drag the payment, rental, and customer tables from step 1 separately to the top pane, the one that says 'no active query',


You can rearrange the tables to create a harmonious relationship map that aligns with your artistic aesthetic.


You should have something like this: 

The tables are joined and ready for querying.









4. The query

The lower right display will have the query.

select

*

from

public.payment p

inner join public.customer c on

p.customer_id = c.customer_id

inner join public.rental r on

c.customer_id = r.customer_id

inner join public.payment p on

r.rental_id = p.rental_id

Not bad, considering you did not have to look up how to join the table.


Refine here

5. Refine the query 

Now we get to the lower left window.  This is where we refine.  Select the Where tab on the left and then the icon with the plus sign on the right. 

We want to skip the customers who have not made a payment. 

Making sure that payment.amount is greater than zero.






Note that the query is automatically adjusted to match the changes.


And we will switch to the sort tab, add an expression that asks for the payments be listed in descending order. 

Add a sort on the amount column, in descending order. 







And again, the query is adjusted to match the sorting demand.

6. The result

The query is ready to run.

select

*

from

public.payment p1

inner join public.customer c on

p1.customer_id = c.customer_id

inner join public.rental r on

c.customer_id = r.customer_id

inner join public.payment p2 on

r.rental_id = p2.rental_id

where

p1.amount > 0

order by

p1.amount desc


And the result.

The Results













We can eliminate excess data by removing the SELECT * statement and specifying the desired columns.



Specifying specific columns

















If you find yourself fighting the syntax of SQL, the Query Builder lets you concentrate on the output and saves you from the frustration.



Comments

Popular posts from this blog

Incremental Backups in PostgreSQL 17

How PostgreSQL's Aggregate FILTER Will Spoil You

Can Artificial Intelligence Created Better Tables Than You?