The SQL Indexes
SQL Indexes are special lookup tables that are used to speed up the process of
data retrieval. They hold pointers that refer to the data stored in a database,
which makes it easier to locate the required data records in a database table.
SQL Indexes work similar to the index of a book or a journal.
While an index speeds up the performance of data retrieval queries (SELECT
statement), it slows down the performance of data input queries (UPDATE and
INSERT statements). However, these indexes do not have any effect on the
data.
SQL Indexes need their own storage space within the database. Despite that, the
users cannot view them physically as they are just performance tools.
The CREATE INDEX Statement
An index in SQL can be created using the CREATE INDEX statement. This
statement allows you to name the index, to specify the table and which column
or columns to index, and to indicate whether the index is in an ascending or
descending order.
Preferably, an index must be created on column(s) of a large table that are
frequently queried for data retrieval.
Syntax
The basic syntax of a CREATE INDEX is as follows –
CREATE INDEX index_name ON table_name;
Types of Indexes
There are various types of indexes that can be created using the CREATE
INDEX statement. They are:
Unique Index
Single-Column Index
Composite Index
Implicit Index
Unique Indexes
Unique indexes are used not only for performance, but also for data integrity. A
unique index does not allow any duplicate values to be inserted into the table. It
is automatically created by PRIMARY and UNIQUE constraints when they are
applied on a database table, in order to prevent the user from inserting duplicate
values into the indexed table column(s). The basic syntax is as follows.
CREATE UNIQUE INDEX index_name
on table_name (column_name);
Single-Column Indexes
A single-column index is created only on one table column. The syntax is as
follows.
CREATE INDEX index_name
ON table_name (column_name);
Composite Indexes
A composite index is an index that can be created on two or more columns of a
table. Its basic syntax is as follows.
CREATE INDEX index_name
on table_name (column1, column2);
Implicit Indexes
Implicit indexes are indexes that are automatically created by the database
server when an object is created. For example, indexes are automatically created
when primary key and unique constraints are created on a table in MySQL
database.
The DROP INDEX Statement
An index can be dropped using SQL DROP command. Dropping an index can
effect the query performance in a database. Thus, an index needs to be dropped
only when it is absolutely necessary.
The basic syntax is as follows −
DROP INDEX index_name;
When should indexes be avoided?
Although indexes are intended to enhance a database's performance, there are
times when they should be avoided.
The following guidelines indicate when the use of an index should be
reconsidered.
Indexes should not be used on small tables.
They should not be used on tables that have frequent, large batch updates
or insert operations.
Indexes should not be used on columns that contain a high number of
NULL values.
Columns that are frequently manipulated should not be indexed.
What is an Aggregate Function in SQL?
An aggregate function in SQL returns one value after calculating multiple column values. We
often use aggregate functions with the SELECT statement's GROUP BY and HAVING
clauses.
There are five types of SQL aggregate functions:
Count()
Sum()
Avg()
Min()
Max()
SQL Aggregate Function Examples
Here are full examples for each aggregate function in SQL, including the SQL program code:
1. COUNT() Example
Task: Count the total number of customers in the Customers table.
-- Create the Customers table
CREATE TABLE Customers (
CustomerID INT,
Name VARCHAR(50),
City VARCHAR(50)
);
-- Insert data into Customers table
INSERT INTO Customers (CustomerID, Name, City) VALUES
(1, 'John Doe', 'New York'),
(2, 'Jane Smith', 'Los Angeles'),
(3, 'Sam Johnson', 'Chicago');
-- Count the number of customers
SELECT COUNT(*) AS total_customers
FROM Customers;
Output:
total_customers
---------------
3
2. SUM() Example
Task: Calculate the total sales (sum of price) from the Orders table.
-- Create the Orders table
CREATE TABLE Orders (
OrderID INT,
Product VARCHAR(50),
Price DECIMAL(10, 2)
);
-- Insert data into Orders table
INSERT INTO Orders (OrderID, Product, Price) VALUES
(1, 'Laptop', 1000.00),
(2, 'Smartphone', 500.00),
(3, 'Tablet', 300.00);
-- Calculate the total sales
SELECT SUM(Price) AS total_sales
FROM Orders;
Output:
total_sales
------------
1800.00
3. AVG() Example
Task: Find the average salary from the Employees table.
-- Create the Employees table
CREATE TABLE Employees (
EmployeeID INT,
Name VARCHAR(50),
Salary DECIMAL(10, 2)
);
-- Insert data into Employees table
INSERT INTO Employees (EmployeeID, Name, Salary) VALUES
(1, 'Alice Brown', 50000.00),
(2, 'Bob White', 60000.00),
(3, 'Charlie Green', 70000.00);
-- Calculate the average salary
SELECT AVG(Salary) AS average_salary
FROM Employees;
Output:
average_salary
---------------
60000.00
4. MIN() Example
Task: Find the youngest employee (minimum age) from the Employees table.
-- Create the Employees table with an age column
CREATE TABLE Employees (
EmployeeID INT,
Name VARCHAR(50),
Age INT
);
-- Insert data into Employees table
INSERT INTO Employees (EmployeeID, Name, Age) VALUES
(1, 'Alice Brown', 35),
(2, 'Bob White', 40),
(3, 'Charlie Green', 28);
-- Find the youngest employee
SELECT MIN(Age) AS youngest_employee
FROM Employees;
Output:
youngest_employee
-----------------
28
5. MAX() Example
Task: Find the highest salary from the Employees table.
-- Create the Employees table with salary
CREATE TABLE Employees (
EmployeeID INT,
Name VARCHAR(50),
Salary DECIMAL(10, 2)
);
-- Insert data into Employees table
INSERT INTO Employees (EmployeeID, Name, Salary) VALUES
(1, 'Alice Brown', 50000.00),
(2, 'Bob White', 75000.00),
(3, 'Charlie Green', 60000.00);
-- Find the highest salary
SELECT MAX(Salary) AS highest_salary
FROM Employees;
Output:
highest_salary
--------------
75000.00