Category Archives: Programming Language

100 Days of SQL

sql

Day 63 – SQL CREATE INDEX Keyword

SQL CREATE INDEX keyword is used to create a new index on one or more columns of a table. An index is a data structure that improves the performance of SQL queries by allowing the database to quickly retrieve data based on the values in the indexed columns.

The basic syntax for using the CREATE INDEX keyword is as follows:

CREATE INDEX index_name
ON table_name (column1, column2, ...);

Here, index_name is the name of the new index, and table_name is the name of the table on which the index will be created. The column1, column2, etc. are the names of the columns that will be included in the index.

For example, to create an index named “idx_students” on the “Students” table, based on the “StudentID” column, the SQL statement would be:


CREATE INDEX idx_students
ON Students (StudentID);

This would create a new index named “idx_students” on the “Students” table, based on the “StudentID” column.

Indexes can improve the performance of SQL queries by allowing the database to quickly retrieve data based on the values in the indexed columns, rather than having to scan the entire table. However, indexes can also have a negative impact on performance if they are not used effectively, so it is important to carefully consider which columns to index and how to structure the indexes to best support the queries that will be run against the table.

100 Days of SQL

sql

Day 61 – SQL CONSTRAINT Keyword

SQL CONSTRAINT keyword is used to define rules and restrictions on the data in a table. Constraints are used to ensure data integrity and consistency in a database, by specifying conditions that must be met by the data.

There are several types of constraints that can be defined using the CONSTRAINT keyword in SQL, including:

  1. Primary key constraint: A primary key constraint is used to ensure that each row in a table is uniquely identified by a specific column or combination of columns. The syntax for defining a primary key constraint is as follows:

CREATE TABLE table_name (
    column1 datatype,
    column2 datatype,
    ...
    CONSTRAINT pk_constraint_name PRIMARY KEY (column1, column2, ...)
);

  1. Foreign key constraint: A foreign key constraint is used to ensure that data in one table corresponds to data in another table. The syntax for defining a foreign key constraint is as follows:

CREATE TABLE table_name1 (
    column1 datatype,
    column2 datatype,
    ...
    CONSTRAINT fk_constraint_name FOREIGN KEY (column1, column2, ...) REFERENCES table_name2(column3, column4, ...)
);

  1. Unique constraint: A unique constraint is used to ensure that each value in a column is unique. The syntax for defining a unique constraint is as follows:

CREATE TABLE table_name (
    column1 datatype,
    column2 datatype,
    ...
    CONSTRAINT unique_constraint_name UNIQUE (column1, column2, ...)
);

  1. Check constraint: A check constraint is used to ensure that the data in a column meets a specific condition or set of conditions. The syntax for defining a check constraint is as follows:

CREATE TABLE table_name (
    column1 datatype CONSTRAINT check_constraint_name CHECK (condition),
    column2 datatype,
    ...
);

  1. Not null constraint: A not null constraint is used to ensure that a column cannot contain null values. The syntax for defining a not null constraint is as follows:

CREATE TABLE table_name (
    column1 datatype NOT NULL,
    column2 datatype,
    ...
);

In each of these examples, the CONSTRAINT keyword is used to define a specific type of constraint on the data in a table. Constraints are a powerful tool for ensuring data integrity and consistency in a database, and they are commonly used in combination with other SQL statements to define complex data models and relationships.

100 Days of SQL

sql

Day 60 – SQL CHECK Keyword

SQL CHECK keyword is used to define a condition that must be satisfied by the values in a column when new data is inserted or updated in a table. It is also known as a check constraint.

The syntax for using the CHECK keyword is as follows:


CREATE TABLE table_name (
    column1 datatype constraint_name CHECK (condition),
    column2 datatype,
    ...
);

Here, column1 is the name of the column that you want to add the constraint to, datatype is the data type of the column, constraint_name is an optional name for the constraint, and condition is the condition that must be satisfied by the values in the column.

For example, the following SQL statement creates a table named “Students” with a check constraint on the “Age” column to ensure that the age of the students is between 18 and 30:


CREATE TABLE Students (
    StudentID INT PRIMARY KEY,
    Name VARCHAR(50),
    Age INT CHECK (Age BETWEEN 18 AND 30),
    Gender VARCHAR(10)
);

In this case, the CHECK keyword is used to define a condition that must be satisfied by the values in the Age column, and any attempts to insert or update data in the table that violates this condition will result in an error.

Note that the CHECK keyword can be used with various logical operators, such as =, <>, <, >, <=, >=, BETWEEN, IN, LIKE, and more, to define complex conditions for the values in a column.

The CHECK constraint is a powerful tool for ensuring data integrity and consistency in a database, and it can be used to enforce various business rules and policies on the data.

100 Days of SQL

sql

Day 59 – SQL CASE Keyword

SQL CASE keyword is used to add conditional logic to a SQL statement, allowing you to perform different actions based on different conditions. The CASE statement can be used in various SQL statements, including SELECT, WHERE, ORDER BY, and GROUP BY.

The syntax for using the CASE keyword is as follows:


SELECT column_name,
    CASE
        WHEN condition1 THEN result1
        WHEN condition2 THEN result2
        ELSE result3
    END
FROM table_name;

Here, column_name is the name of the column that you want to include in the query result, and condition1, condition2, and result3 are the conditions and corresponding results that you want to apply to the data.

For example, the following SQL statement selects the customer name and their credit status, and assigns a value of “Good” or “Bad” based on their credit limit:


SELECT CustomerName,
    CASE
        WHEN CreditLimit > 10000 THEN 'Good'
        ELSE 'Bad'
    END AS CreditStatus
FROM Customers;

In this case, the CASE statement is used to add a conditional logic that assigns the value “Good” to customers with a credit limit greater than 10000, and “Bad” to all other customers.

Note that the CASE keyword can be used with multiple conditions and corresponding results by using multiple WHEN clauses. Also, an ELSE clause can be included to specify a default value if none of the conditions are met.

The CASE keyword is a powerful tool for adding conditional logic to SQL queries and is often used in combination with other SQL keywords to perform complex data analysis and transformation.

100 Days of SQL

sql

Day 58 – SQL BETWEEN Keyword

SQL BETWEEN keyword is used in a WHERE clause to specify a range of values that a column value must fall within.

The syntax for using the BETWEEN keyword is as follows:


SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

Here, column_name is the name of the column that you want to filter on, and value1 and value2 are the minimum and maximum values of the range, respectively.

For example, the following SQL statement selects all the orders from the “Orders” table where the order date is between January 1, 2022, and March 31, 2022:


SELECT *
FROM Orders
WHERE OrderDate BETWEEN '2022-01-01' AND '2022-03-31';

In this case, the BETWEEN keyword is used to specify the range of values for the OrderDate column, and all the rows where the OrderDate falls within this range are selected.

Note that the BETWEEN keyword is inclusive, meaning that the values of value1 and value2 are included in the range. So, in the above example, orders with the dates January 1, 2022, and March 31, 2022, will be included in the result set.

Also, it is important to note that the BETWEEN keyword works with numerical and date/time values, as well as with character strings that represent numerical or date/time values in a recognized format.

100 Days of SQL

sql

Day 57 – SQL BACKUP DATABASE Keyword

SQL BACKUP DATABASE keyword is used to create a backup of a SQL Server database.

The syntax for using the BACKUP DATABASE keyword is as follows:


BACKUP DATABASE database_name
TO backup_device

Here, database_name is the name of the database that you want to back up, and backup_device is the physical device or location where you want to store the backup, such as a disk, tape, or a network share.

For example, the following SQL statement creates a full backup of the “MyDatabase” database and stores it in a backup file named “MyDatabase.bak” on the local disk:


BACKUP DATABASE MyDatabase
TO DISK = 'C:\\Backup\\MyDatabase.bak'

In this case, the TO DISK option specifies that the backup should be written to a disk file, and the full path and file name of the backup file are provided as the argument.

The BACKUP DATABASE keyword also supports various other options and parameters, such as specifying the backup type, compression, encryption, media options, and more. These options can be used to customize the backup process according to your specific requirements.

Note that the BACKUP DATABASE keyword requires the user to have appropriate permissions to perform the backup operation, and the backup device must have sufficient space to store the backup.

100 Days of SQL

sql

Day 56 – SQL ASC Keyword

SQL ASC keyword is used in the ORDER BY clause of a SELECT statement to sort the result set in ascending order.

The syntax for using the ASC keyword is as follows:


SELECT column_name(s)
FROM table_name
ORDER BY column_name ASC;

Here, column_name is the name of the column by which you want to sort the result set in ascending order.

For example, the following SQL statement selects all the customers from the “Customers” table and sorts them in ascending order by their last names:


SELECT *
FROM Customers
ORDER BY LastName ASC;

In this case, the result set is sorted in ascending order by the values in the LastName column.

Note that the ASC keyword is optional in SQL, as the default sorting order is ascending. So, the above example can also be written without the ASC keyword, like this:


SELECT *
FROM Customers
ORDER BY LastName;

Both of these statements will produce the same result set, sorted in ascending order by the values in the LastName column.

100 Days of SQL

sql

Day 55 – SQL AS Keyword

SQL AS keyword is used to give an alias or a temporary name to a table or a column in the query result. It is often used to make the output more meaningful or to simplify the syntax of the query.

The syntax for using the AS keyword to assign an alias to a column is as follows:


SELECT column_name AS alias_name
FROM table_name;

Here, column_name is the name of the column in the table that you want to alias, and alias_name is the temporary name that you want to assign to the column.

For example, the following SQL statement selects the customer name and the total amount of orders, and assigns an alias TotalAmount to the calculated column:


SELECT Customers.CustomerName, SUM(Orders.Amount) AS TotalAmount
FROM Customers
JOIN Orders ON Customers.CustomerID = Orders.CustomerID
GROUP BY Customers.CustomerName;

In this case, the SUM function is used to calculate the total amount of orders for each customer, and the AS keyword is used to assign an alias TotalAmount to the calculated column in the query result.

The AS keyword can also be used to give an alias to a table or a subquery, like this:


SELECT *
FROM (SELECT column_name FROM table_name) AS alias_name;

In this case, the AS keyword is used to assign a temporary name alias_name to the subquery result set, which can then be used as a table in the outer query.

100 Days of SQL

sql

Day 54 – SQL ANY Keyword

SQL ANY keyword is used in conjunction with a comparison operator to evaluate a condition against multiple values in a subquery or a list of values.

The syntax for using the ANY keyword is as follows:


SELECT column_name(s)
FROM table_name
WHERE column_name operator ANY (subquery or list of values);

Here, column_name is the name of the column in the table that you want to evaluate, operator is the comparison operator (such as =, >, <, >=, <=, <>, IN, LIKE, etc.), and subquery or list of values is the set of values against which you want to compare.

For example, the following SQL statement selects all the customers from the “Customers” table whose customer IDs match any of the values in the subquery:


SELECT *
FROM Customers
WHERE CustomerID = ANY (SELECT CustomerID FROM Orders WHERE OrderDate = '2022-01-01');

In this case, the subquery returns a list of customer IDs for the orders that were placed on January 1st, 2022, and the ANY keyword is used to compare the CustomerID column with this list of values. If the CustomerID matches any of the values in the list, the row is returned.

100 Days of SQL

sql

Day 53 – SQL AND Keyword

SQL AND keyword is a logical operator used to combine multiple conditions in a SQL statement. The AND operator requires that all conditions separated by it must be true for the overall condition to be true.

The syntax for using the AND operator in a SQL WHERE clause is as follows:


SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;

Here, condition1, condition2, condition3, etc. are the conditions to be evaluated, and the AND operator is used to combine them.

For example, the following SQL statement selects all records from the customers table where the country column is Germany and the city column is Berlin:


SELECT *
FROM customers
WHERE country = 'Germany' AND city = 'Berlin';

In this example, the two conditions separated by the AND operator must both be true for the overall condition to be true. Only records that meet both criteria will be returned by the SELECT statement.

Note that the AND operator can be used in conjunction with other logical operators, such as OR and NOT, to create more complex conditions in a SQL statement.