9/17/2018

Select query order of execution in SQL Server

In Many interview, this questions generally asked by the interviewer that what is the correct order of Select query for execution by SQL Server engine.

Below I have mentioned the correct order of SQL server query execution which SQL Server used to execute:


1. FROM and Joins
2. Where
3. Group By
4. Having
5. Select
6. Distinct
7. Top
7. Order By

SQL Server Functions with examples


SQL Server Functions :

Functions in SQL Server is a set of SQL statements that accepts only input parameters at run time and perform some actions and return the result, which can be only single value or a table.

Types of Functions:

System Defined Function

These functions are defined by SQL Server so these are Pre-defined functions. Like: 

AVG()
COUNT()
Sum()
ABS()  
Round() 
Convert() 

To use system defined function we have to pass parameters information only to get output from these functions.

User Defined Functions

These Functions are created by Users in database for performing some operations on data.

Types of User Defined Functions are:

Scalar Function:
User defined scalar function also returns single value as a result of actions perform by function. We return any datatype value from function.

Example:

--Create function to get employee full name

Create function UFD_GetEmpFullName
(
 @FirstName varchar(50),
 @LastName varchar(50)
)
returns varchar(101)
As
Begin return (Select @FirstName + ' '+ @LastName);
end

--Calling the above created function

Select dbo.UFD_GetEmpFullName(FirstName,LastName) as Name, Salary from dbo.Employee

Inline Table-Valued Function:

User defined inline table-valued function returns a table variable as a result of actions perform by function. The value of table variable should be derived from a single SELECT statement.

Example:

--Create function to get employees

Create function UFD_GetEmpDetails()
returns Table
As
 return (Select * from dbo.Employee)

 --Call the above created function

Select * from UFD_GetEmpDetails()

Multi-Statement Table-Value Function:

User defined multi-statement table-valued function returns a table variable as a result of actions perform by function. In this a table variable must be explicitly declared and defined whose value can be derived from a multiple SQL statements.

Example:

--Create function for EmpID,FirstName and Salary of Employee

Create function UFD_GetMulEmployee()
returns @Employee Table
(
EmpID int,
FirstName varchar(50),
Salary int
)
As
begin
 Insert into @Employee Select e.EmpID,e.FirstName,e.Salary from dbo.Employee e;

--Now update salary of first employee

 update @Employee set Salary=25000 where EmpID=1;

--It will update only in @Emp table not in Original Employee table

return
end

 --Call the above created function

Select * from UFD_GetMulEmployee()

SQL Server maximum number of indexes per table

In SQL server, maximum number of indexes are as:

In SQL server 2005 number of indexes per table as:

Clustered Index - 1

Non Clustered Index - 249

Total Indexes per table (Clustered + Non-Clustered) - 250

In SQL Server 2008 and 2012 number of indexes per table as:

Clustered Index - 1

Non Clustered Index - 999

Total Indexes per table (Clustered + Non-Clustered) - 1000

Note: You can put as many indexes per table but keep in mind, insert or update operation on that table will be slow due to many indexes.

To Learn more about SQL Server capacity, please go through Microsoft page:
https://docs.microsoft.com/en-us/sql/sql-server/maximum-capacity-specifications-for-sql-server?view=sql-server-2017


8/03/2018

Export Data to multiple excel sheet from SQL server Table using SSIS


Export Data to multiple excel sheet from SQL server Table using SSIS
Below solution where we need to export SQL data into multiple excel sheets, as we have below data in our SQL Table:

LoanID       State         TYPE        STEP
2                   CA            R6            1
3                   CA            R6           3
4                   CA            P1            4
67                 CA            P1            5
45                 CA           R2            7
23                 CA            R2            8
Now we want excel file (Destination) to have multiple tabs using column 'State' and 'Type'.
Below is how result in excel should be:
LoanID       State         TYPE        STEP
2                   CA            R6            1
3                   CA            R6           3

Second tab should be 'CA-P1'
LoanID       State         TYPE        STEP
4                   CA            P1            4
67                 CA            P1            5

And Third tab will be 'CA-R2'
LoanID       State         TYPE        STEP
45                 CA           R2            7
23                 CA            R2            8
Solution:
First tab should be 'CA-R6'







Sorting Alpha Numeric and Numeric data in SQL Server


Sorting Alpha Numeric and Numeric data

I have a varchar2 column in which data is present like :
1a
1b
1c
1
2
2a
2d 
and so on ... and output should be sorted like :
1,1a,1b,2,2c,2d ........ 11,22,23,24d ...
Below Query where I have used PATINDEX function of SQL Server, which is similar to the like operator
Syntax : The Patindex function takes two arguments:
PATINDEX ('%pattern%', exp)
declare @t table
(
v varchar(20)
)
insert @t
values ('1a'),

('1b'),

('1c'),

('1'),

('2'),

('2a'),

('2d '),
('11c '),
('111d '),
('11 '),
('22 '),
('222a '),
('222 '),
('111 '),
('11 ')

select *
from @t
order by left(v,patindex('%[^0-9]%',v + 'x')-1) * 1,v

5/30/2018

Exclude 0 from value in SQL Server


Exclude 0 from value in SQL Server

 

Create Table #TempData

(

ColumnData Varchar(20)

)

 

Insert into #TempData values('063506038')

Insert into #TempData values('0063506038')

Insert into #TempData values('00063506038')

Insert into #TempData values('00000063506038')

 

 

Select * from #TempData

 

Select REPLACE(LTRIM(REPLACE( ColumnData,'0',' ')),' ','0')  From #TempData

Tableau interview questions and answers for experienced professionals

  Tableau Interview Questions and Answers for experienced professional 1. What is TABLEAU? Tableau  is the powerful and fastest visualizing ...