EnjoY | Database Research And Development

Thursday, September 3, 2020

PostgreSQL 9.4: Using FILTER CLAUSE, multiple COUNT(*) in one SELECT Query for Different Groups

 PostgreSQL 9.4 has introduced one of the very good FILTER CLAUSE which is used to apply filters in aggregate functions.

Using FILTER, You can use different types of aggregate functions without applying any GROUP BY CLAUSE.

Now Imagine, that I have one Student table and I want total number of Students based different grades.
What happened without FILTER CLAUSE, We have to perform this calculation in the individual SELECT query.

But with the use of FILTER CLAUSE we can perform aggregation based on different FILTER values in a single SQL Query.

Below is a small demonstration of this:

Create one Student table with sample data:

Apply FILTER clause to count number of Students based on Marks:

Wednesday, September 2, 2020

SQL Server : Difference between Temp Table and Common Table Expression (CTE)

 

In this post, I am sharing the important difference between Temp Table and Common Table Expression (CTE) of the SQL Server.
I recently wrote an article on the difference between Temp Table and Table Variable of the SQL Server.


The theory and practice of Temp Table, Table Variable and CTE is one of my favourite topics of the SQL Server.

A CTE is used for a temporary result set that is defined within the execution scope of the query.
A Temp Table is also used for a temporary result set, but it can be defined for limited execution scope or can be used to define for global execution scope as a Global Temp Table.

A CTE is substituted for a view when the general use of a view is not required, and you do not have to store the definition of metadata.

CTE always uses memory and Temp Table stored in the TempDB.

The Scope of CTE is only for the query and can be referenced multiple times in other CTEs.
The child objects also access the Temp Table, and it persists until the end of the sessions.

We can not use NOLOCK and other isolation levels with CTE and with the Temp Table, we can apply it explicitly.

If we require to CTE for another batch query, it requires recreation of it.
The Temp Table can be used for all batches of a session.

A CTE doesn’t maintain any statistics and any metadata information, Temp table is maintaining a require statistics.

A CTE is a never part of the transaction and locking and its treated as system transaction.
The temp table operations are a part of user transactions.

If we have a large dataset, we should use Temp Table and with the small dataset, we can use CTE very effectively.

SQL Server: Difference between Temporary Table and Table Variable

 

Sharing one of the timeworn topics and we can also find an N number of good articles on this.

I also like this topic very much so I would like to write a small note on this.

First, Very important point:

Temp table is stored in TempDB and Table variable is stored in Memory.
The above statement is 100% wrong.
Table variable is also stored definition into TempDB and if the data volume is increased, sometimes it also stores data into TempDB.

For that reason, SQL Server 2014 introduced Hekaton storage engine for Memory optimized Table.
"SQL Server 2014: What is Hekaton?"
"SQL Server 2014: Create Memory Optimized File Group and Table"

If we require creating a unique index on data, we should use Temporary Table. We cannot apply Index on Table Variable.

If we are dealing with large number records for inserting and deleting, we should use a temporary table. If we have a small quantity of data, we should use Table Variable.

If the amount of data is frequently changing, query planner requires updated statistics to recompile object so for that we should use Temporary Table.
If the amount of data is not frequently changing, we should use Table Variable for better performance.

When we require rollback because of an outer user transaction, we should use Table Variable because it automatically discard all the data from the session.

If we would like to avoid locking issue, we should go with Table Variable because Temp Table is fully dependent on TempDB so sometimes it holds the transactions.

We can define explicit transaction on Temp Table where with the Table Variable we cannot define explicit transaction.

The maintenance and creation of metadata for Table Variable requires less time than Temp Table.

The scope of Table Variable is up to batch or stored procedure after completion of execution it drops automatically.
The scope of Temp Table is up to sessions, and once the session ends, it drops automatically, or we can also drop explicitly.

Tuesday, September 1, 2020

PostgreSQL: DELETE JOIN with an example

 

In this post, I am sharing a simple example of DELETE INNER JOIN statement in PostgreSQL.

Many of the database developers are exploring the PostgreSQL so DELETE a table from another table which is a very common requirement so I am sharing a simple example.

Create two sample tables with data:

DELETE INNER JOIN:

Check the result:

Featured Post

SQL Server : SELECT all columns to be good or bad in database system

This article is half-done without your Comment! *** Please share your thoughts via Comment *** In this post, I am going to write about one o...

Popular Posts