Welcome to plsql4all.blogspot.com SQL, MYSQL, ORACLE, TERADATA, MONGODB, MARIADB, GREENPLUM, DB2, POSTGRESQL.

Saturday, 31 August 2024

New Faeture in PostgreSQL 17

In PostgreSQL, the RETURNING clause is a powerful feature that allows you to return data from rows that are modified by INSERT, UPDATE, or DELETE statements. This feature is particularly useful when you need to retrieve the values of certain columns after an operation, without the need for a separate SELECT query.


What's New in the Latest PostgreSQL Release Regarding RETURNING:


In the latest PostgreSQL releases (up to version 15 as of the last update), the RETURNING clause has seen continued support and minor enhancements. Here's what you should know:


1. Enhanced Performance:

   - PostgreSQL has made continuous improvements in query performance, which indirectly benefits the use of the RETURNING clause. The optimizer and execution plans have been fine-tuned, making operations that involve RETURNING more efficient in specific scenarios.


2. Expanded Use Cases:

   - The RETURNING clause continues to support more complex expressions and functions, giving users the flexibility to return computed values, rather than just direct column data.


3. Use with Common Table Expressions (CTEs):

   - PostgreSQL supports using the RETURNING clause within Common Table Expressions (CTEs). This allows for more complex operations where you can chain multiple INSERT, UPDATE, or DELETE operations while capturing and using the returned data within the same query.


Basic Examples of RETURNING Usage:-


INSERT with RETURNING:

 

  INSERT INTO employees (name, position, salary)

  VALUES ('John Doe', 'Manager', 75000)

  RETURNING id, name, position;

 

  - This returns the id, name, and position of the newly inserted row.


UPDATE with RETURNING:

 

  UPDATE employees

  SET salary = salary * 1.1

  WHERE position = 'Manager'

  RETURNING id, name, salary;

 

  - This updates the salary of all employees with the position 'Manager' and returns their id, name, and the new salary.


DELETE with RETURNING:

 

  DELETE FROM employees

  WHERE id = 10

  RETURNING id, name;


  - This deletes the employee with id 10 and returns the id and name of the deleted employee.


The RETURNING feature in PostgreSQL continues to be a robust tool for developers. While there haven't been groundbreaking changes specifically to this feature in the latest releases, it remains a critical part of PostgreSQL's capabilities, enhanced by the general improvements in performance, support for complex expressions, and integration with other SQL features like CTEs.

Friday, 16 August 2024

What is Slowly changing dementions SCDs

Slowly Changing Dimensions (SCDs) refer to the concept of managing and tracking changes in dimension data over time within a data warehouse. A dimension is typically a descriptive attribute related to facts in a data warehouse, such as customer, product, or location information. Since this data can change over time (e.g., a customer changes their address), it's crucial to handle these changes appropriately in the data warehouse to maintain accurate historical data.


Types of Slowly Changing Dimensions:-


There are primarily three types of Slowly Changing Dimensions:


1. Type 1 (Overwrite):-

   - Definition: In Type 1 SCD, when a change occurs in the dimension data, the old value is overwritten with the new one. This method does not maintain any historical data.

   - Use: Type 1 is used when historical accuracy is not important or when only the current value of the data is needed.

   - Example: 

     - Scenario: A customer’s last name is spelled incorrectly in the database.

     - Action: When the error is corrected (e.g., "Jonhson" corrected to "Johnson"), the old value "Jonhson" is overwritten with the correct value "Johnson", and no history of the old value is kept.


2. Type 2 (Add New Row):-

   - Definition: In Type 2 SCD, a new row is added to the dimension table each time a change occurs. This method maintains a full history of all changes.

   - Use: Type 2 is used when it is important to keep track of historical data, such as tracking the history of a customer’s address over time.

   - Example:

     - Scenario: A customer moves from one city to another.

     - Action: A new row is added to the dimension table with the new address, while the old address is retained in the existing row. Additional columns, such as Start_Date and End_Date, may be used to indicate the period during which each address was valid.


3. Type 3 (Add New Column):-

   - Definition: In Type 3 SCD, a new column is added to the dimension table to store the old value when a change occurs. This method typically tracks limited historical information, often only the previous value.

   - Use: Type 3 is used when changes are infrequent, and only the most recent change needs to be tracked along with the current value.

   - Example:

     - Scenario: A product changes its category from "Electronics" to "Home Appliances."

     - Action: A new column is added to the dimension table (e.g., Previous_Category), which stores the old value Electronics, while the Current_Category column holds the new value Home Appliances.


Real-Life Examples of Slowly Changing Dimensions:-


Example 1: Customer Address Change (Type 2):-

   - Scenario: A bank maintains customer information, including addresses. Over time, customers move to new locations.

   - Implementation: Using Type 2 SCD, each time a customer changes their address, a new record is added to the `Customer` dimension table with the updated address. This approach ensures that the bank can analyze data based on the customer's location at any given time in the past.


Example 2: Employee Role Change (Type 3):-

   - Scenario: An HR system tracks employee job titles. Occasionally, employees get promoted or move to different roles within the company.

   - Implementation: Using Type 3 SCD, when an employee’s job title changes, the old title is stored in a new column (e.g., Previous_Job_Title), while the current title is stored in the existing Current_Job_Title column. This allows HR to easily see both the current and previous roles of an employee.


Example 3: Product Price Update (Type 1):-

   - Scenario: A retail company updates the prices of its products periodically.

   - Implementation: Using Type 1 SCD, whenever the price of a product is updated, the old price is overwritten with the new price in the `Product` dimension table. Historical pricing information is not retained since only the current price is relevant for day-to-day operations.


Use of Slowly Changing Dimensions:-


- Historical Data Tracking: SCDs are crucial for maintaining accurate historical data, which is vital for trend analysis, auditing, and reporting.

- Accurate Reporting: They enable accurate and consistent reporting, ensuring that business decisions are based on complete historical data.

- Compliance and Auditing: Certain industries require tracking changes over time to comply with regulatory standards, making SCDs essential for compliance.

Saturday, 3 August 2024

COUNT (*) vs COUNT(1) In SQL

In SQL, COUNT(*) and COUNT(1) are often used interchangeably to count the number of rows in a table. While they achieve the same result, there are subtle differences in their usage and performance. Here’s a detailed comparison:-


COUNT(*)


- Usage: Counts all rows in the table, including rows with NULL values.

- Syntax: 

  SELECT COUNT(*) FROM table_name;

- Performance: Modern SQL databases optimize COUNT(*) queries efficiently. The * does not mean that all columns are selected; it is simply a standard notation to count all rows.


COUNT(1)


- Usage: Counts all rows in the table. The 1 is a constant expression and does not refer to any column.

- Syntax: 

  SELECT COUNT(1) FROM table_name;

 

- Performance: Similar to COUNT(*), modern SQL databases optimize COUNT(1) effectively. Since 1 is a constant, it can be slightly more efficient in certain scenarios because it doesn’t need to reference any columns.


Differences:-


1. Conceptual Difference:- 

- COUNT(*) counts all rows, considering all columns.

- COUNT(1) counts all rows, but 1 is just a placeholder and doesn’t reference any column.


2. Performance:-

- In most modern SQL databases, there is no significant performance difference between COUNT(*) and COUNT(1) due to query optimization techniques.

 - Older versions of some databases might show slight differences, but these differences are generally negligible.


3. Usage in Joins:

- When using joins, COUNT(*) can be useful to count rows from a specific table even if some columns are joined with NULL values.

- COUNT(1) remains unaffected by the actual columns being joined, as it uses a constant.


Example:-


Given a table employees:


| id | name     | department |

|----|----------|------------|

| 1  | John Doe | Sales      |

| 2  | Jane Doe | Marketing  |

| 3  | Alice    | NULL       |


Using COUNT(*):-


SELECT COUNT(*) FROM employees;


Result: 3


Using COUNT(1):


SELECT COUNT(1) FROM employees;


Result: 3


In both cases, the result is the same: 3, indicating that there are three rows in the employees table


For most practical purposes, COUNT(*) and COUNT(1) are interchangeable and will produce the same results with similar performance. The choice between them is often a matter of personal or organizational coding standards rather than a strict performance or functionality requirement.

Please provide your feedback in the comments section above. Please don't forget to follow.