PARTITION
PARTITION is a SQL command used in creating tables, specifically partitioned tables, which logically break a table into segments that help improve database performance. Each segment represents a subset of data and is stored separately, enabling data management and access to be distributed across multiple pieces of hardware or database instances.
Example
Output
Explanation
This example creates a partitioned table employees
. The table is partitioned by the hired
column using the range method. Four partitions are defined, where each partition holds rows for which the hired
year falls within a given range. For example, partition p1 holds rows with hired
years from 1991 to 1995.
Example
Output
Explanation
In this example, the PARTITION BY
clause is used with the SELECT
statement to divide the employees
table into partitions based on the department_id
column. It then returns all data from the employees
table, but the rows in each partition are ordered separately.
Example
Output
Output:
Explanation
This code creates a partition of the SampleTable
based on the ID
field. The data is partitioned into four parts: less than 300, between 300 and 600, between 600 and 900, and greater than 900. The output query shows the count of records that fall into each of the partitions.
Example
Output
Explanation
In the SQL query given, the SUM() function is used with the OVER() clause to create a sub-total for each department. The PARTITION BY clause is used to divide the SUM() into partitions based on the DEPARTMENT_ID, and ORDER BY to sort the rows in each partition. This results in a running total of the salaries for each department, ordered by the salary within the department.