Useful Functions in SQL


Following is the list of all useful SQL built-in functions:


  •    COUNT - The SQL COUNT aggregate function is used to count the number of rows in a database table.
  •    SQRT - This is used to generate a square root of a given number.
  •    RAND - This is used to generate a random number using SQL command.
  •    MAX - The SQL MAX aggregate function allows us to select the highest (maximum) value for a certain column.
  •    MIN - The SQL MIN aggregate function allows us to select the lowest (minimum) value for a certain column.
  •    CONCAT - This is used to concatenate any string inside any SQL command.
  •    Numeric - Complete list of SQL functions required to manipulate numbers in SQL.
  •    String - Complete list of SQL functions required to manipulate strings in SQL.
  •    AVG - The SQL AVG aggregate function selects the average value for certain table column.
  •    SUM - The SQL SUM aggregate function allows selecting the total for a numeric column.

CREATE INDEX IN SQL


Indexes allow the database application to find data fast without reading the entire table.

The CREATE INDEX statement is used to create indexes in tables

Indexes

An index can be created in a table to find data more rapidly and efficiently.users cannot see the indexes, they are just used to speed up searches/queries.

SQL CREATE INDEX Syntax

Creates an index on a table. Duplicate values are allowed:
CREATE INDEX index_name ON table_name (column_name)

SQL CREATE UNIQUE INDEX Syntax

Creates a unique index on a table. Duplicate values are not allowed:
CREATE UNIQUE INDEX index_name ON table_name (column_name)

CREATE INDEX Example

The SQL statement below creates an index named "EmpIndex " on the "Emp_id" column in the "EmpMaster" table:

CREATE INDEX EmpIndex ON EmpMaster (Emp_id)

If you want to create an index on a combination of columns, you can list the column names within the parentheses, separated by commas:

CREATE INDEX EmpIndex ON EmpMaster(Emp_id, Emp_Name)

For More details on SQL INDEX please check



Keys in SQL server.

Key is a sole or mixture of multiple fields in a table. It is used to fetch records/data-rows from data table according to the requirement. Keys are also used to create relationship among different database tables or views.

Types of SQL Keys

We have following types of keys in SQL which are used to fetch records from tables and to make relationship among tables or views.

1. Super Key

Super key is a set of one or more than one keys that can be used to identify a record uniquely in a table.

2. Candidate Key

A Candidate Key is a set of one or more fields/columns that can identify a record uniquely in a table. There can be multiple Candidate Keys in one table. Each Candidate Key can work as Primary Key.

 

3. Primary Key

Primary key is a set of one or more fields/columns of a table that uniquely identify a record in database table. It cannot accept null, duplicate values. Only one Candidate Key can be Primary Key.

4. Alternate key

A Alternate key is a key that can be work as a primary key. Basically it is a candidate key that currently is not primary key.

5. Composite/Compound Key

Composite Key is a combination of more than one fields/columns of a table. It can be a Candidate key, Primary key.

6. Unique Key

Unique key is a set of one or more fields/columns of a table that uniquely identify a record in database table. It is like Primary key but it can accept only one null value and it cannot have duplicate values.

7. Foreign Key


Foreign Key is a field in database table that is Primary key in another table. It can accept multiple null, duplicate values. 

What is an Operators in SQL

What is an Operators in SQL?

An operator is a reserved word or a character used mainly in an SQL statement's WHERE clause to perform operation, like comparisons and arithmetic. Operators are used to specify conditions in an SQL statement and to serve as conjunctions for multiple conditions in a statement.
  •         Arithmetic operators
  •          Comparison operators
  •          Logical operators

SQL Arithmetic Operators:
  •          +  Addition - Adds values on either side of the operator
  •          - Subtraction - Subtracts right hand operand from left hand operand
  •          * Multiplication - Multiplies values on either side of the operator             
  •          / Division - Divides left hand operand by right hand operand      
  •          % Modulus - Divides left hand operand by right hand operand and returns remainder   


SQL Comparison Operators:

  •          = Checks if the values of two operands are equal or not, if yes then condition becomes true.
  •          != Checks if the values of two operands are equal or not, if values are not equal then condition becomes true.     
  •          <> Checks if the values of two operands are equal or not, if values are not equal then condition becomes true.
  •          > Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.
  •          < Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.
  •          >= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.
  •           <= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.
  •          !< Checks if the value of left operand is not less than the value of right operand, if yes then condition becomes true.
  •          !> Checks if the value of left operand is not greater than the value of right operand, if yes then condition becomes true.


SQL Logical Operators:

  •          ALL: The ALL operator is used to compare a value to all values in another value set.
  •          AND: The AND operator allows the existence of multiple conditions in an SQL statement's WHERE clause.
  •          ANY: The ANY operator is used to compare a value to any applicable value in the list according to the condition.
  •          BETWEEN: The BETWEEN operator is used to search for values that are within a set of values, given the minimum value and the maximum value.
  •          EXISTS: The EXISTS operator is used to search for the presence of a row in a specified table that meets certain criteria.
  •          IN: The IN operator is used to compare a value to a list of literal values that have been specified.
  •          LIKE: The LIKE operator is used to compare a value to similar values using wildcard operators.
  •          NOT: The NOT operator reverses the meaning of the logical operator with which it is used.
  •          OR: The OR operator is used to combine multiple conditions in an SQL statement's WHERE clause.
  •         IS NULL: The NULL operator is used to compare a value with a NULL value.
  •          UNIQUE: The UNIQUE operator searches every row of a specified table for uniqueness (no duplicates).

View in Sql Server


View in Sql Server

A view is nothing but a SQL statement that is stored in the database with an allied name. A view is truly an arrangement of a table in the form of a SQL query. A view can be created from one or many tables which depends on the written SQL query to create a view.
Views, which are kind of virtual tables, allow users to do the following:

•        Structure data in a way that users or classes of users find natural or intuitive.
•        Limit access to the data such that a user can see and modify exactly what they need.
•        Summarize data from various tables which can be used to produce reports.

How to create view in DB:

Database views are created using the CREATE VIEW statement.
The basic CREATE VIEW syntax is as follows:

CREATE VIEW view_name AS
SELECT columns .....
FROM table_name
WHERE [condition];

You can have multiple tables in SELECT statement in very like you use them in normal SQL SELECT query.

WITH CHECK OPTION:

The WITH CHECK OPTION is a CREATE VIEW statement option. The purpose of the WITH CHECK OPTION is to ensure that all UPDATE and INSERTs satisfy the condition(s) in the view definition.
If they do not satisfy the condition, the UPDATE or INSERT returns an error.

The following is an example of creating same view CUSTOMERS_VIEW with the WITH CHECK OPTION:

CREATE VIEW Emp_Details_View AS
SELECT name, Address, PhoneNo, Salary, age
FROM  Emp_Details
WHERE age IS NOT NULL WITH CHECK OPTION;

The WITH CHECK OPTION in this case should deny the entry of any NULL values in the view's AGE column, because the view is defined by data that does not have a NULL value in the AGE column.

Update a View:

A view can be updated under certain conditions:

•        SELECT clause may not contain the keyword DISTINCT.
•        SELECT clause may not contain summary functions.
•        SELECT clause may not contain set operators.
•        SELECT clause may not contain an ORDER BY clause.
•        FROM clause may not contain multiple tables.
•        WHERE clause may not contain subqueries.
•        SELECT clause may not contain set functions.
•        Query may not contain GROUP BY or HAVING.
•        Calculated columns may not be updated.
•        All NOT NULL columns from the base table must be included in the view in order for the INSERT query to function.

Inserting Rows into a View:

Rows of data can be inserted into a view. The same rules that apply to the UPDATE command also apply to the INSERT command.
Here we cannot insert rows in Emp_Details_VIEW because we have not included all the NOT NULL columns in this view, otherwise you can insert rows in a view in similar way as you insert them in a table.

Deleting Rows into a View:

Rows of data can be deleted from a view. The same rules that apply to the UPDATE and INSERT commands apply to the DELETE command.
Following is an example to delete a record having AGE= 22.

DELETE FROM Emp_Details_VIEW  WHERE age = 22;

Dropping Views:

 Where you have a view, you need a way to drop the view if it is no longer needed. The syntax is very simple as given below:

DROP VIEW view_name;

Why we can't execute a stored procedure from a User Defined function(UDF)

Functions cannot "touch" any database but read them only. Stored procedures can do anything and everything with databases. You ...