Headder AdSence

Showing posts with label T-SQL. Show all posts
Showing posts with label T-SQL. Show all posts

Using Common Table Expressions (CTEs) in T-SQL

Using Common Table Expressions (CTEs) in T-SQL

Using Common Table Expressions (CTEs) in T-SQL

Learn how to use Common Table Expressions in SQL Server with this comprehensive guide.

Introduction to CTEs

Common Table Expressions (CTEs) are a powerful feature in T-SQL that allows you to define temporary result sets that can be referenced within a SELECT, INSERT, UPDATE, or DELETE statement.

CTEs can improve the readability of complex queries and can be used to create recursive queries.

CTEs are defined using the WITH keyword.

Benefits of Using CTEs

CTEs improve query organization and readability.

They allow for recursive queries, which can simplify certain types of data retrieval.

Creating a Simple CTE

To create a CTE, use the WITH statement followed by the CTE name and the AS keyword, then define the query in parentheses.

Example: WITH CTE_Name AS (SELECT column1, column2 FROM Table_Name)

Recursive CTEs

Recursive CTEs are useful for hierarchical data, such as organizational charts or category trees.

They consist of two parts: the anchor member and the recursive member.

Quick Checklist

  • Understand the syntax of CTEs.
  • Know when to use CTEs versus temporary tables.
  • Be aware of the scope and lifetime of a CTE.

FAQ

What is a CTE?

A Common Table Expression (CTE) is a temporary result set that you can reference within a SELECT, INSERT, UPDATE, or DELETE statement.

Can CTEs be recursive?

Yes, CTEs can be recursive, allowing for the retrieval of hierarchical data.

How do CTEs improve query readability?

CTEs allow you to break down complex queries into simpler components, making them easier to read and understand.

Related Reading

  • CTE vs Temporary Tables
  • Understanding SQL Joins
  • Performance Tuning in T-SQL

This tutorial is for educational purposes. Validate in a non-production environment before applying to live systems.

Tags: SQL Server, T-SQL, CTE, Data Engineering

Quick Checklist

  • Prerequisites (tools/versions) are listed clearly.
  • Setup steps are complete and reproducible.
  • Include at least one runnable code example (SQL/Python/YAML).
  • Explain why each step matters (not just how).
  • Add Troubleshooting/FAQ for common errors.

Applied Example

Mini-project idea: Implement an incremental load in dbt using a staging table and a window function for change detection. Show model SQL, configs, and a quick test.

FAQ

What versions/tools are required?

List exact versions of Snowflake/dbt/Airflow/SQL client to avoid env drift.

How do I test locally?

Use a dev schema and seed sample data; add one unit test and one data test.

Common error: permission denied?

Check warehouse/role/database privileges; verify object ownership for DDL/DML.

Handling NULLs Effectively in T-SQL

Handling NULLs Effectively in T-SQL

A SQL Server database interface showing NULL values in a table.

Handling NULLs Effectively in T-SQL

Learn how to manage NULL values in T-SQL for better data integrity and performance.

Introduction to NULL Handling in T-SQL

In T-SQL, NULL represents a missing or undefined value. Understanding how to handle NULLs is crucial for data integrity and accurate query results.

This tutorial covers effective techniques for managing NULL values in SQL Server, including functions and best practices.

NULL handling is a vital skill for data professionals.

Understanding NULL in SQL Server

NULL is not the same as an empty string or zero; it signifies the absence of a value. Knowing this difference is key for accurate data manipulation.

Queries involving NULL values can yield unexpected results if not handled properly.

Clarifying the concept of NULL is essential for effective data handling.

Common Functions for NULL Handling

T-SQL provides functions like ISNULL(), COALESCE(), and NULLIF() to manage NULL values effectively.

ISNULL() replaces NULL with a specified value, COALESCE() returns the first non-NULL value in a list, and NULLIF() returns NULL if two expressions are equal.

Utilizing these functions can simplify your queries.

Best Practices for NULL Management

Always consider NULL in your database design to prevent issues with data integrity.

Use appropriate defaults to minimize the occurrence of NULL values where applicable.

Consistently handle NULLs in your queries to avoid logic errors.

Proactive NULL handling leads to more robust applications.

Quick Checklist

  • Understand the definition of NULL in SQL Server.
  • Familiarize yourself with ISNULL(), COALESCE(), and NULLIF() functions.
  • Implement best practices for NULL management in your database design.
  • Test your queries to ensure they handle NULLs as expected.

FAQ

What is the difference between NULL and an empty string in SQL Server?

NULL indicates the absence of a value, while an empty string is a defined value that contains no characters.

How can I check for NULL values in my queries?

Use the IS NULL condition in your WHERE clause to filter records with NULL values.

Can I index columns with NULL values?

Yes, you can index columns with NULL values, but keep in mind that NULLs are treated as a separate value in indexes.

Related Reading

  • SQL Server Functions
  • Data Integrity in SQL Server
  • T-SQL Best Practices

This tutorial is for educational purposes. Validate in a non-production environment before applying to live systems.

Tags: SQL Server, T-SQL, NULL handling, Data integrity, Database management

Quick Checklist

  • Prerequisites (tools/versions) are listed clearly.
  • Setup steps are complete and reproducible.
  • Include at least one runnable code example (SQL/Python/YAML).
  • Explain why each step matters (not just how).
  • Add Troubleshooting/FAQ for common errors.

Applied Example

Mini-project idea: Implement an incremental load in dbt using a staging table and a window function for change detection. Show model SQL, configs, and a quick test.

FAQ

What versions/tools are required?

List exact versions of Snowflake/dbt/Airflow/SQL client to avoid env drift.

How do I test locally?

Use a dev schema and seed sample data; add one unit test and one data test.

Common error: permission denied?

Check warehouse/role/database privileges; verify object ownership for DDL/DML.

SQL Server Tip: Handling NULLs Effectively in T-SQL

SQL Server Tip: Handling NULLs Effectively in T-SQL

A sleek SQL Server interface showcasing handling NULL values in T-SQL with code snippets.

Overview

Handling NULL values in SQL Server is crucial for accurate data analysis and reporting. NULLs can represent missing or unknown values, and if not handled properly, they can lead to incorrect calculations and outputs in queries.

Prerequisites

  • SQL Server: Version 2012 or later.
  • SQL Server Management Studio (SSMS): Latest version recommended.
  • Permissions: Read and write access to a database.

Step-by-step

  1. Understand NULL Behavior: NULL is a unique value in SQL Server that represents the absence of a value. When performing calculations or comparisons, NULLs can lead to unexpected results. For example:
  2. SELECT 10 + NULL AS Result; -- Result will be NULL
  3. Using ISNULL Function: Replace NULLs with a default value using the ISNULL function. This is useful in calculations and reporting.
  4. SELECT Name, ISNULL(Salary, 0) AS Salary FROM Employees;
  5. COALESCE for Multiple Values: COALESCE can be used to return the first non-NULL value in a list. This is particularly handy when dealing with multiple potential NULL sources.
  6. SELECT Name, COALESCE(Salary, Bonus, 0) AS TotalCompensation FROM Employees;
  7. NULLIF for Conditional Replacement: Use the NULLIF function to return NULL when two expressions are equal, which can be helpful in specific scenarios.
  8. SELECT Name, NULLIF(Salary, 0) AS SalaryOrNull FROM Employees;
  9. Handling NULLs in Aggregation: NULLs are ignored in aggregate functions, so ensure you're aware of this when calculating sums or averages.
  10. SELECT AVG(ISNULL(Salary, 0)) AS AverageSalary FROM Employees;

Why this matters

Effectively handling NULLs is essential for data integrity and accurate reporting. When NULLs remain unaddressed, they may distort analysis and lead to incorrect insights. Using functions like ISNULL, COALESCE, and NULLIF can help maintain data quality and improve the reliability of your reports and dashboards.

Troubleshooting / FAQ

  • Error on Calculation: If you encounter unexpected NULL results in calculations, check if any of the operands are NULL. Utilize ISNULL or COALESCE to mitigate this.
  • Common Pitfalls: Forgetting to account for NULLs in joins can lead to missing data in result sets. Always consider how NULLs may affect your JOIN conditions.

Next steps

Explore related topics such asData Types in SQL ServerorWriting Robust SQL Queries. Practicing these concepts will help solidify your understanding of NULL handling in T-SQL.

Quick Checklist

  • Prerequisites (tools/versions) are listed clearly.
  • Setup steps are complete and reproducible.
  • Include at least one runnable code example (SQL/Python/YAML).
  • Explain why each step matters (not just how).
  • Add Troubleshooting/FAQ for common errors.

Applied Example

Mini-project idea: Implement an incremental load in dbt using a staging table and a window function for change detection. Show model SQL, configs, and a quick test.

FAQ

What versions/tools are required?

List exact versions of Snowflake/dbt/Airflow/SQL client to avoid env drift.

How do I test locally?

Use a dev schema and seed sample data; add one unit test and one data test.

Common error: permission denied?

Check warehouse/role/database privileges; verify object ownership for DDL/DML.

Related Reading

  • Using CROSS APPLY and OUTER APPLY in SQL Server

Using Common Table Expressions (CTEs) in T-SQL

Using Common Table Expressions (CTEs) in T-SQL

Visual representation of SQL Server CTEs in T-SQL with examples.

Introduction to Common Table Expressions (CTEs)

Common Table Expressions (CTEs) are a powerful feature in T-SQL that allow you to create temporary result sets that can be referenced within a SELECT, INSERT, UPDATE, or DELETE statement. They enhance query organization, readability, and maintainability.

Why This Matters

CTEs improve query structure by breaking down complex SQL statements into manageable segments. They can also enable recursion and help simplify repetitive code.

Step-by-Step Guide to Using CTEs

Basic Syntax

The syntax for a CTE is straightforward:

WITH CTE_Name AS (<     SELECT column1, column2<     FROM table_name<     WHERE condition< )< SELECT * FROM CTE_Name;

Example 1: Simple CTE

In this example, we’ll create a CTE to find employees with salaries greater than a specific amount:

WITH HighEarners AS (<     SELECT EmployeeID, FirstName, LastName, Salary<     FROM Employees<     WHERE Salary > 50000< )< SELECT * FROM HighEarners;

Example 2: CTE with Recursive Query

CTEs can also be used for recursive queries. Here’s how you can find all employees under a specific manager:

WITH EmployeeHierarchy AS (<     SELECT EmployeeID, FirstName, LastName, ManagerID<     FROM Employees<     WHERE ManagerID IS NULL<     UNION ALL<     SELECT e.EmployeeID, e.FirstName, e.LastName, e.ManagerID<     FROM Employees e<     INNER JOIN EmployeeHierarchy eh ON e.ManagerID = eh.EmployeeID< )< SELECT * FROM EmployeeHierarchy;

Troubleshooting/FAQ

What if my CTE returns no results?

Double-check the conditions in your CTE. Ensure that the base query inside the CTE is constructed correctly and matches the expected data.

Can I use multiple CTEs?

Yes, you can define multiple CTEs by separating them with commas:

WITH CTE1 AS (...), CTE2 AS (...)< SELECT * FROM CTE1, CTE2;

Quick Checklist

  • Prerequisites (tools/versions) are listed clearly.
  • Setup steps are complete and reproducible.
  • Include at least one runnable code example (SQL/Python/YAML).
  • Explain why each step matters (not just how).
  • Add Troubleshooting/FAQ for common errors.

2-Minute Case Study

Anita, 28, aims for ₹4 lakh emergency fund in 18 months. She picks a low-risk liquid/debt fund, sets a ₹22,000 SIP, and reviews once a quarter. For retirement, she chooses a Nifty 50 index fund with a 20-year SIP, increasing contributions 5% yearly.

FAQ

How much should I invest monthly?

Work backwards from goal and date; SIP = Goal ÷ Months (adjust for expected return).

Direct vs Regular plan?

Direct plans have lower expense ratios; over time that compounds to higher returns.

When should I sell?

Review annually. Rebalance if allocation drifts by >5–10% or when a goal is fully funded.

Related Reading

  • Snowflake Basics: Setting Up Your Snowflake Account and Warehouse

Mastering Common Table Expressions (CTEs) in SQL Server T-SQL

Mastering Common Table Expressions (CTEs) in SQL Server T-SQL

A visually appealing illustration of SQL Server Common Table Expressions (CTEs) showing their syntax and usage.

Introduction to Common Table Expressions (CTEs)

Common Table Expressions (CTEs) are a powerful feature in SQL Server that allow you to create temporary result sets that can be referenced within a SELECT, INSERT, UPDATE, or DELETE statement. They are particularly useful for simplifying complex queries and improving readability.

Why CTEs Matter

CTEs make your SQL code cleaner and more manageable. They can:

  • Enhance query organization
  • Facilitate recursive queries
  • Improve performance in certain scenarios

Basic Syntax of CTE

The syntax for a CTE is straightforward. It starts with theWITHclause followed by the CTE name and the query that generates the temporary result set.

WITH CTE_Name AS (<     SELECT Column1, Column2<     FROM TableName<     WHERE conditions< )< SELECT * FROM CTE_Name;

Practical Examples

Example 1: Simple CTE

Consider a scenario where we have a table namedEmployeeswith the following structure:

CREATE TABLE Employees (<     EmployeeID INT PRIMARY KEY,<     Name NVARCHAR(100),<     Salary DECIMAL(10, 2)< );

We want to select all employees with a salary greater than ₹50,000. Here’s how to do it using a CTE:

WITH HighEarners AS (<     SELECT EmployeeID, Name, Salary<     FROM Employees<     WHERE Salary > 50000< )< SELECT * FROM HighEarners;

Example 2: Recursive CTE

Recursive CTEs can be used for hierarchical data. Let’s assume we have aCategoriestable:

CREATE TABLE Categories (<     CategoryID INT PRIMARY KEY,<     CategoryName NVARCHAR(100),<     ParentCategoryID INT< );

To retrieve a full category hierarchy, we’ll create a recursive CTE:

WITH CategoryHierarchy AS (<     SELECT CategoryID, CategoryName, ParentCategoryID<     FROM Categories<     WHERE ParentCategoryID IS NULL<     UNION ALL<     SELECT c.CategoryID, c.CategoryName, c.ParentCategoryID<     FROM Categories c<     INNER JOIN CategoryHierarchy ch ON c.ParentCategoryID = ch.CategoryID< )< SELECT * FROM CategoryHierarchy;

Conclusion

Common Table Expressions are a valuable tool for SQL developers and data engineers. They simplify complex queries, making them easier to read and maintain. When used effectively, CTEs can significantly enhance your SQL coding practices.

FAQ

Q: Can CTEs be used in all SQL Server statements?

A: Yes, CTEs can be used in SELECT, INSERT, UPDATE, and DELETE statements.

Q: What is the maximum level of recursion for a recursive CTE?

A: The default maximum recursion level is 100. This can be modified using theOPTION (MAXRECURSION n)clause.

Q: Are CTEs stored in the database?

A: No, CTEs are not stored in the database. They exist only for the duration of the query.

Quick Checklist

  • Define a clear goal (amount + date).
  • Pick the right product (debt/index/hybrid) based on horizon.
  • Automate SIP; review annually.
  • Keep costs low (prefer direct plans).
  • Avoid chasing past performance.

2-Minute Case Study

Anita, 28, aims for ₹4 lakh emergency fund in 18 months. She picks a low-risk liquid/debt fund, sets a ₹22,000 SIP, and reviews once a quarter. For retirement, she chooses a Nifty 50 index fund with a 20-year SIP, increasing contributions 5% yearly.

FAQ

How much should I invest monthly?

Work backwards from goal and date; SIP = Goal ÷ Months (adjust for expected return).

Direct vs Regular plan?

Direct plans have lower expense ratios; over time that compounds to higher returns.

When should I sell?

Review annually. Rebalance if allocation drifts by >5–10% or when a goal is fully funded.

Related Reading

how to get pm/am datetime in T-SQL server


SELECT GETDATE() ActualDateTime,FORMAT(GETDATE(),'MM/dd/yyyy HH:mm:ss tt') TransformedDateTime;


 

Ctrl R not working in SQL Server?

Ctrl + R: it's general thing which SQL Developer uses often in day.

     Some times this Result Pane won't work, so we can again create this short cut, please go through below steps.


1. go to Tool > ..Options > Keyboard > Keyboard


2. Select Window.ShowResultsPane under Show Commands containing

3. SQL Query Editor under Use new short cut in:

4. Click empty space of Press Shortcut Keys:then give your Shortcut Key (pres your           
     required shortcut key)

What is Primary Key in SQL Server?


                                                                     PRIMARY KEY                                                                                         

1. It's a CONSTRAINT, a collection of columns uniquely identifies each row in a table

2. Only one PRIMARY KEY constraint allows on Table.

3. PRIMARY KEY column will not allow NULL Values & duplicate values















What is the difference between DELETE and TRUNCATE statements?


                                                           TRUNCATE vs DELETE
















TRUNCATE
DELETE
Truncate is used to delete the all records in table
Delete is used to delete the row level and table level data
We can’t rollback the data
We can rollback data if we maintain the transaction
It’s auto committed
It’s explicit committed
It’s a DDL (Data Definition Language) Command
It’s a DML (Data Manipulation Language) Command
It’s faster than the delete
It’s very slow when we compare with TRUNCATE



Ways of column alias name in SQL Server.



As per my knowledge I am using in three ways of giving alias names to the database table column in SQL Server while selecting,

Below are the types which I am using:

1. Giving the alias name in Square Bracket [ ]

2. Giving the alias name with Underscore _

3. Giving the alias name with Double Quotes “ ”
4. Giving Direct Expected Name

1. Square Brackets

          The required column/ header name should be with in the Square Brackets only, with in this bracket we can give spaces also in expected name, so mostly we will use these kinds of brackets when we have space in our expected column name, below is the example.

Example:


SELECT                                                                                                           
  Empno                                         --Original Name of the column   
, Empno AS [Employee Number]--Alias Name of the Column   FROM tbl_Emp

O/P


2. Underscore _

          The required column/ header name should be with Underscore i.e. _, with this underscore only between the two words like Employee and Name, we have to concatenate these two with Underscore I.e. Employee_Name

Example:


SELECT  
  Empno   --Original Name of the column                
, Empno  AS Employee_Number --Alias Name of the Column   FROM tbl_Em            

O/P












3. Double Quotes:

          The required column/ header name should be within the Double Quotes only, with in this double quote we can give spaces also in expected column name, so this also mostly we will use these kinds of Quotes when we have spaces in our expected column name, below is the example.

Example:


SELECT                                                                                                           
  Empno --Original Name of the column                                           
, Empno AS "Employee Number"--Alias Name of the Column   FROM tbl_Emp                                                                                            

O/P













4. Direct Expected Name:

          This like directly whatever you what you can give, there is no such condition like above, but here we can’t write column name with spaces, it will work only for single world column name

Example:


SELECT                                                                                                            
  Empno                                         --Original Name of the column   
, Empno AS "EmployeeNumber"--Alias Name of the Column  
FROM tbl_Emp                                                                                             

O/P











Please comment below

Department (Dept) Sample Data


Please Copy the below Query and run on your SSMS.


/* Droping the Existing Dept Table: */
DROP TABLE IF EXISTS tbl_Dept;

/* Creating New Dept Table: */
CREATE TABLE tbl_Dept(
tbl_Deptno INT NOT NULL PRIMARY KEY
, DName VARCHAR(50) NOT NULL
, Location VARCHAR(50) NOT NULL);

/* Inserting Data into Dept Table: */
INSERT INTO tbl_Dept VALUES 
 (10,'Accounting','New York')
,(20,'Research','Dallas')
,(30,'Sales','Chicago')
,(40,'Operations','Boston');

/*cSeleting Inserted Data */
SELECT * FROM tbl_Dept

Click Here for : Employee Table Sample Data

Please comment below


Employee (Emp) Sample Data


Please Copy the below Query and run on your SSMS.


/*Please relook once you before Drop the Existing Table: */
DROP TABLE IF EXISTS tbl_Emp;

/*Creating New Employee Table*/
CREATE TABLE tbl_Emp(
  Empno INT NOT NULL PRIMARY KEY
, Ename VARCHAR(50) NOT NULL
, Job VARCHAR(50) NOT NULL
, Mgr INT
, Hiredate DATE
, Sal DECIMAL(10,2)
, Comm DECIMAL(10,2)
, Deptno INT NOT NULL);


/*Inserting Data Into above created Table: */
INSERT INTO tbl_Emp (Empno,Ename,Job,Mgr,Hiredate,Sal,Comm,Deptno)
VALUES
  (7369, 'SMITH', 'CLERK', 7902, '6/13/93', 800, 0.00, 20)
, (7499, 'ALLEN', 'SALESMAN', 7698, '8/15/98', 1600, 300, 30)
, (7521, 'WARD', 'SALESMAN', 7698, '3/26/96', 1250, 500, 30)
, (7566, 'JONES', 'MANAGER', 7839, '10/31/95', 2975, null, 20)
, (7698, 'BLAKE', 'MANAGER', 7839, '6/11/92', 2850, null, 30)
, (7782, 'CLARK', 'MANAGER', 7839, '5/14/93', 2450, null, 10)
, (7788, 'SCOTT', 'ANALYST', 7566, '3/5/96', 3000, null, 20)
, (7839, 'KING', 'PRESIDENT', null, '6/9/90', 5000, 0, 10)
, (7844, 'TURNER', 'SALESMAN', 7698, '6/4/95', 1500, 0, 30)
, (7876, 'ADAMS', 'CLERK', 7788, '6/4/99', 1100, null, 20)
, (7900, 'JAMES', 'CLERK', 7698, '6/23/00', 950, null, 30)
, (7934, 'MILLER', 'CLERK', 7782, '1/21/00', 1300, null, 10)
, (7902, 'FORD', 'ANALYST', 7566, '12/5/97', 3000, null, 20)
, (7654, 'MARTIN', 'SALESMAN', 7698, '12/5/98', 1250, 1400, 30);


/*Selecting Data from Table*/
select * from tbl_Emp


Please comment below 


How to add new Column to existing table in SQL Server


     I know it's simple statement and most of the people known this query, but some times we forgot the syntax so that I got a thought why should not post this? ... :) :) 

Syntax: 

ALTER TABLE <Table Name> ADD <Column Name> <Data Type>

Example

ALTER TABLE Emp2 ADD E_Addres INT

 Please See below Screenshot 


User Defined Table Type


SQL Server  providing the User Defined Table Type, as of my knowledge this is useful when we need to pass parameter to the Stored Proc or Functions as a table values.

Here we can create different data types of columns like INT, VARCHAR etc.

we can create User Type Table  in two ways, here I am giving only one type simply

/* Create a user-defined table type */
CREATE TYPE LocationTableType AS TABLE
    ( LocationName VARCHAR(50)
    , CostRate INT )
GO



















It would create User Defined Table Type





















Stored Procedures

Stored Procedure


     Stored Procedures, these are most important or common things in every Data Base these will play main role in lot of cases, now I am going to tell you some points

     Stored Procedures, as of known everyone it's a collection of SQL Statements, Yes its exactly right.

     Stored Procedure takes the multiple parameters and will returns the output as result set to the calling program.

Advantages of Stored Procedure

Reduce the Network Traffic, Yes it will reduce the network traffic you may get a doubt on this, how the network traffic would reduce using the Stored Procedure?, Yes it will reduce, for example if we are calling 10 statements and calculating these 10 statements in application level then passing to the server it takes much time so instead of calling 10 statements we can call just single statement that simply pass to the server and will fire at server level, these 10 statements will execute as batch, I hope you got the logic here.

Security, using stored procedures user can't find the database level objects, because in application level we are calling only stored procedure only, so if any third person (malicious users) in between application and server they cannot see the objects of Data Base

These everyone knows I hope

Re-usability of Code, Yes we can use multiple times which we created as Stored Procedure or collection of SQL statements, no need to write these statement multiple times, if any changes are required we need to Alter the Stored Procedure.




 which may you don't know if you know more information or if anything wrong in below just mail me.

Clustered Indexes


CLUSTERED INDEX


     Yes, Here I am going to explain about Indexes in SQL Server  as of my knowledge, I hope every one heard about Indexes but don't have a complete idea about it.

Ok, Let's start

     Basically Indexes are using for Query performance in SQL Server (actually not in SQL Server in all other Data Bases also using for performance only) , these are on-disk structure, and I read some where we can call as row store Index.

     Clustered Index will creates an Index Key and stored the data rows of the tables in order to clustered Index Key in sorting order (ascending or descending) , flow of the clustered index follows B-Tree structure.




    We can create using SQL Server Management Studio or T-SQL, a clustered Index can be rebuild or reorganize on demand to manage the Index Key order.

     When we create a table with the Primary Key that table will create along with Index Key, if table having the Clustered Index then those kind of tables called Clustered Tables, if that table doesn't have any Clustered Index then that we call as Heap Tables

    There can be only one Clustered Index per table, Table doesn't allow more than one Clustered Index

Syntax:
CREATE CLUSTERED INDEX <Index Name> ON <Table Name> (<Column Name>);   
Example:
CREATE CLUSTERED INDEX INDX_Emp_Tbl_Emp_Num ON dbo.Emp (Emp_Num);   

In Generally If table have more than 8 MB then only it goes to Index to get the data else it directly fetch the data from the table it self.

Soon I will update more Information on this