STATISTICS
STATISTICS in SQL is a service that performs statistical analysis on the data in the database. It includes measures of central tendency and dispersion, correlations, frequency distributions, and more. The statistical information can be used by the query optimizer to create more efficient query plans. It is essential in data management and for maintaining optimal performance levels in a database system.
Example
Output
Explanation
In the above example, a table named SampleTable
is first created with three columns: ID
, Name
, and Age
. Five records are inserted into this table.
The SQL function AVG()
is used to calculate the average value of the Age
column in the SampleTable
. The output of the SELECT AVG(Age)
query is 35.0, which is the average age of all the people in the SampleTable
.
Example
Output
Explanation
In the example provided, a table named example_table is created with two columns, example_column1 and example_column2 of type INT and VARCHAR respectively. Several rows of data are then inserted into the table. Finally, the PostgreSQL ANALYZE command is used to collect statistics about the contents of tables in the database, which is important to help the PostgreSQL query planner to help make intelligent decisions during query optimization and execution. In this case, it’s collecting statistics for the table example_table. The output confirms the creation of the table, the insertion of the data, and the successful execution of the ANALYZE command.
Example
Output
Explanation
The above SQL Server command creates a statistics object called order_stats
on the Orders
table for the columns OrderID
and CustomerID
. These statistics provide SQL Server with metadata about the distribution of values in these columns and help enhance the performance of queries.
Example
Output
Explanation
The SQL query above retrieves statistical information about the data in the “salary” column of the “employees” table. It counts all rows (total), calculates sum, average, minimum, and maximum values of salaries. These functions provide a quick overview of the distribution of salaries in the employees table.