• Home
  • SQL Server
    • Articles
    • T-SQL Puzzles
    • Output Puzzles
    • Interview Questions
    • Performance Tuning
    • SQL SERVER On Linux
    • Resources
  • SSRS
    • SSRS Articles
    • Interview Questions
  • SSAS
    • SSAS Articles
    • DAX
  • SQL Puzzles
  • Interview Questions
    • SQL Interview Questions
    • Data Interview Questions
  • Python Interview Puzzles
  • New Features(SQL SERVER)
    • SQL SERVER 2017
    • SQL SERVER 2016
    • SQL SERVER On Linux
  • Social
    • Expert Exchange
      • Top Expert in SQL
      • Yearly Award
      • Certifications
      • Achievement List
      • Top Expert of the Week
    • HackerRank (SQL)
    • StackOverflow
    • About Me
      • Contact Me
      • Blog Rules

Improving my SQL BI Skills

Improving my SQL BI Skills

Daily Archives: June 13, 2016

SQL Puzzle | The Name Puzzle

13 Monday Jun 2016

Posted by Pawan Kumar Khowal in T SQL Puzzles, Tricky SQL Queries

≈ 1 Comment

Tags

Advanced SQL Interview Questions and Answers, Advanced SQL tutorial pdf, any recommended websites for sql puzzles, Best SQL Puzzles, Buy SQL Server Interview Questions Book Online at Low Price, Calculating nth Max salary, Check Prime Number puzzle, Circular prime number puzzle, Circular Prime Number Puzzlre, COmples tSQL puzzles, Complex SQL Challenges, complex sql statement(puzzle), Complex tsql, Complex TSQL Challenge, Delete Duplicate data from table without using Aggregate & Ranking functions, Divide rows into two columns, Download SQL Questions, Download SQL Server Interview Question in PDF, Download SQL SERVER Interview questions, Download SQL Server Interview questions and answers, download sql server interview questions and answers pdf, download sql server interview questions by Pawan Khowal, download sql server interview questions by Pawan Kumar, download sql server interview questions by Pawan Kumar Khowal, Download T-SQL Interview Questions, Even Odd Count Puzzle, Excellent t-SQL puzzles, Fibonacci Series puzzle, Find nth highest salary, Find Nth maximum value in SQL Server, find records not ending with s, find records not ending with s puzzle, Finding prime numbers with SQL server, Free Download SQL SERVER Interview questions, generate Fibonacci Series in SQL Server, Generate possible combinations puzzle, Get nth highest and lowest salary of an employee, implementation and administration, Improve SQL Skills, Interesting Interview Questions, Interview Puzzles in SQL Server, Interview Qs.SQL SERVER Questions, Interview questions and Answers for MS SQL Server designing, Interview Questions and Answers For SQL, Interview questions on Joins, Interview Questions on SQL, Interview SQL Puzzles, InterviewQuestions, InterviewQuestions for SQL, Joins, Joins Interview questions, Joins Puzzle, Khowal, last non null puzzle, Learn complex SQL, Learn SQL, Learn T-SQL, Microsoft SQL Server interview questions for DBA, MS SQL Server interview questions, MSBI Interview Questions, Multiple Column Pivoting Puzzle, Multiplication of Digits Puzzle, Multiply digits of a number, multiply digits of a number puzzle, nth maximum & minimum salary in a single sql query, Objective Puzzle, Pawan, Pawan Khowal, Pawan Kumar, Pawan Kumar Khowal, PL/SQL Challenges, Prev Value puzzle, Previous value puzzle, Prime numbers after given number puzzle, Prime numbers using SQL, Program to Print prime numbers, Puzzle SQL, puzzle sql developer, Puzzle SQl Server, Puzzles, PUzzles in SQL SERVER, Queries for SQL Interview, Records not ending with a character Puzzle, Records not ending with s Puzzle, Replace string puzzle, Reverse a string without loop, reverse function or recursive CTE, SELECT Puzzle, Single Query multiple conditions puzzle, Smart Replace puzzle, Split & Count Puzzle, Split & count puzzle in SQL, Split and Count puzzle SQL Server, SQL, sql - How to find third or nth maximum salary from salary table?, SQL - The Pattern Puzzles, SQL 2012, SQL 2014, SQL 2014 Interview Questions, SQL Challenge, SQL Challenges, SQL Extreme Difficult Puzzle, SQL FAQ, SQL FAQs, SQL Interview Q & A, SQL Interview Questions, SQL Interview Questions - Part 2, SQL Interview Questions for SQL Professionals‎, SQL Joins, SQL Pattern Puzzles, SQL pl/sql puzzles, SQL prime number puzzle, SQL puzzle, SQL Puzzle | 2nd Highest Salary in Each Department using OffSet & Fetch Method, SQL Puzzle | Break Table in Equal Parts, SQL Puzzle | Find Different Values Puzzle, SQL Puzzle | Get Weeks (Start Date - End Date) for a Month & Year, SQL Puzzle | Multiplication of Digits Puzzle, SQL Puzzle | Star Pattern Puzzle ( Fun with SQL ), SQL Puzzle | The Fiscal Puzzle - II, SQL Puzzle | The Middle 4 Puzzle, SQL Puzzle | The Time Difference Puzzle, SQL Puzzles, sql puzzles & answers, sql puzzles and answers free download, sql puzzles and answers pdf, sql puzzles for interview, sql puzzles oracle, SQL Queries, SQL Queries asked in interviews, SQL QUERY PUZZLES, SQL Query to Find Nth Highest Salary of Employee, SQL Questions, SQL Quiz, SQL Replace Puzzle, SQL Server - Common Interview Questions and Answers, SQL SERVER - Find Nth Highest Salary of Employee, SQL Server - General Interview Questions and Answers, sql server - How to find the nth highest salary in SQL?, sql server 2008 interview questions, SQL Server 2008 Interview Questions and Answers, SQL Server Database, SQL Server database developer interview questions and answers, sql server dba interview questions, SQL Server developer Interview questions and answers, SQL Server developer Interview questions with answers, SQL Server Developer T-SQL Interview Questions, SQL server filter index with LIKE and RIGHT function?, SQL SERVER Indexes, SQL Server Interview Puzzle, SQL SERVER Interview questions, SQL SERVER Interview questions & Answers, SQL Server Interview Questions - Part 1, SQL Server Interview questions and answers, SQL Server Interview Questions and Answers - Free PDF, SQL SERVER Interview questions and answers for experienced, sql server interview questions and answers for net developers, SQL Server Interview Questions And Answers.pdf, sql server interview questions by Pawan Khowal sql interview questions, SQL SERVER Interview questions pdf, SQL Server Interview Questions | MSBISKILLS.Com, SQL Server Interview Questions | MSBISKILLS.com Top 50 SQL Server Questions & Answers, SQL Server Interview Questions/Answers Part-1, SQL Server Puzzle, SQL SERVER Puzzles, SQL server Questions and Answers, SQL SERVER Tips, SQL Skills, SQL Sudoku, SQL Tips & Tricks, SQL Tips and Tricks, SQL Top clause, SQL Tough Puzzle, SQL Tricks, SQL Trikcy question, sql/database interview for puzzle sql developer, SQLBI Interview Questions, SQLSERVER, Star Pattern Puzzle, Sum of digits puzzle, Sum of digits SQL puzzle, T SQL Puzzles, T-SQL Challenge, T-SQL Interview Questions | SQL Server Interviews and Jobs, T-SQL Puzzle, T-SQL Query | [ Replace 6 Consecutive Digits with x from a string Puzzle ], T-SQL Query | [ The Complex Week Puzzle ], T-SQL Server Interview Questions, T-SQL Tricky Puzzles, The % percentage puzzle, The Biggest Gap Puzzle, The Fibonacci Series Puzzle, The Gap Puzzle, The Gap Puzzle Puzzle, The In Puzzle, The InNotIn Puzzle, The Middle 4 Puzzle, The Middle 4 SQL Puzzle, The Multiple Sums Puzzle, The new rows count puzzle, The Prime Number Puzzle, The Replace Puzzle, The Time Difference Puzzle, The Tree Puzzle, The Update Puzzle, Top 10 Frequently Asked SQL Query Interview Questions, TOP 100 SQL SERVER INTERVIEW QUESTIONS QUERIES, Top 50 SQL Server Interview Question for Testers, TOP Clause, Tough SQL Challenges, Tough SQL Puzzles, Tricky Questions, TSQL, TSQL Challenge, TSQL Challenges, TSQL Interview questions, TSQL Puzzle, TSQL Puzzles, TSQL Queries, Week puzzle, [Solved] sql query for nth highest & lowest salary


SQL Puzzle | The Name Puzzle

Here you have given 3 tables, You have to join three table on the basis of ID. Now the catch here is if the name present in table 3 (Three) then we have to take name from three, If not then we will have to move to Table 2 (Two) and if not then move to Table 1 (One)

Please check out the sample input and the expected output below-

Sample Input

Table 1

ID Name
1 Pawan
2 Sharlee
3 Rahisha
4 Disha
5 Ramesh
6 Kishan
7 Qoutilya
8 Sandeep

Table 2

ID Name
1 Diljit
2 Kumar
3 Khowal

Table 3

ID Name
1 MSBISKILLS.COM
6 MSBI

Expected Output

ID Name
1 MSBISKILLS.COM
2 Kumar
3 Khowal
4 Disha
5 Ramesh
6 MSBI
7 Qoutilya
8 Sandeep

Solution 1


--


SELECT a.ID,COALESCE(u.NAME,t.NAME,a.NAME) Name
FROM One a 
OUTER APPLY
(
	SELECT TOP 1 b.Name FROM
	Two b WHERE a.ID = b.ID
)t
OUTER APPLY
(
	SELECT TOP 1 c.Name FROM
	Three c WHERE a.ID = c.ID
)u


--

Solution 2


--


SELECT a.ID , CASE WHEN c.NAME IS NOT NULL THEN c.NAME 
			       WHEN b.NAME IS NOT NULL THEN b.NAME 
			  ELSE
				   a.Name  
              END Name
FROM One a 
LEFT JOIN Two b ON a.ID = b.ID
LEFT JOIN Three c ON a.ID = c.ID


--

Solution 3


--


SELECT a.ID , COALESCE(c.NAME,b.NAME,a.NAME) Name
FROM One a 
LEFT JOIN Two b ON a.ID = b.ID
LEFT JOIN Three c ON a.ID = c.ID


--

Below 3 Solutions are provided by a Senior MVP.

Solution 4


--



/*       Using Windows Functions       */



SELECT distinct [id], first_value([name]) over (partition by [id] order by [level] desc) as [name]
FROM
(

		SELECT 3 as [level], [id], [name] FROM [THREE]

		UNION ALL

		SELECT 2 as [level], [id], [name] FROM [TWO]

		UNION ALL

		SELECT 1 as [level], [id], [name] FROM [ONE]

) AS A



--

Solution 5


--


/*       Using FULL OUTER JOIN                     */

WITH CTE_STEP1 AS

(

	SELECT

	COALESCE(A.ID, B.ID) AS ID,

	COALESCE(A.NAME, B.NAME) AS NAME

	FROM

	[THREE] AS A

	FULL OUTER JOIN

	[TWO] AS B

	ON A.ID = B.ID

),

CTE_STEP2 AS

(

	SELECT

	COALESCE(C.ID, D.ID) AS ID,

	COALESCE(C.NAME, D.NAME) AS NAME

	FROM

	CTE_STEP1 AS C

	FULL OUTER JOIN

	[ONE] AS D

	ON C.ID = D.ID

)

SELECT * FROM CTE_STEP2 ORDER BY ID

GO


--

Solution 6


--


SELECT * INTO #TEMP1 FROM [THREE] AS A;

GO


-- Select new ids from 2

INSERT INTO #TEMP1

SELECT * FROM [TWO] AS B

WHERE B.ID NOT IN

(SELECT ID FROM #TEMP1);

GO

-- Select new ids from 1

INSERT INTO #TEMP1

SELECT * FROM [ONE] AS C

WHERE C.ID NOT IN

(SELECT ID FROM #TEMP1);

GO

-- Show results

SELECT * FROM #TEMP1 AS D ORDER BY D.ID;

GO

--

Add a comment if you have any other solution in mind. I would love to learn it. We all need to learn.

Enjoy !!! Keep Learning

Pawan Khowal 

Http://MSBISkills.com

Share this

  • LinkedIn
  • Facebook
  • Twitter
  • WhatsApp
  • Email

SQL Puzzle | Generate IDs Puzzle

13 Monday Jun 2016

Posted by Pawan Kumar Khowal in SQL SERVER, T SQL Puzzles, Tricky SQL Queries

≈ Leave a comment

Tags

Advanced SQL Interview Questions and Answers, Advanced SQL tutorial pdf, any recommended websites for sql puzzles, Best SQL Puzzles, Buy SQL Server Interview Questions Book Online at Low Price, Calculating nth Max salary, Check Prime Number puzzle, Circular prime number puzzle, Circular Prime Number Puzzlre, COmples tSQL puzzles, Complex SQL Challenges, complex sql statement(puzzle), Complex tsql, Complex TSQL Challenge, Delete Duplicate data from table without using Aggregate & Ranking functions, Divide rows into two columns, Download SQL Questions, Download SQL Server Interview Question in PDF, Download SQL SERVER Interview questions, Download SQL Server Interview questions and answers, download sql server interview questions and answers pdf, download sql server interview questions by Pawan Khowal, download sql server interview questions by Pawan Kumar, download sql server interview questions by Pawan Kumar Khowal, Download T-SQL Interview Questions, Even Odd Count Puzzle, Excellent t-SQL puzzles, Fibonacci Series puzzle, Find nth highest salary, Find Nth maximum value in SQL Server, find records not ending with s, find records not ending with s puzzle, Finding prime numbers with SQL server, Free Download SQL SERVER Interview questions, generate Fibonacci Series in SQL Server, Generate possible combinations puzzle, Get nth highest and lowest salary of an employee, implementation and administration, Improve SQL Skills, Interesting Interview Questions, Interview Puzzles in SQL Server, Interview Qs.SQL SERVER Questions, Interview questions and Answers for MS SQL Server designing, Interview Questions and Answers For SQL, Interview questions on Joins, Interview Questions on SQL, Interview SQL Puzzles, InterviewQuestions, InterviewQuestions for SQL, Joins, Joins Interview questions, Joins Puzzle, Khowal, last non null puzzle, Learn complex SQL, Learn SQL, Learn T-SQL, Microsoft SQL Server interview questions for DBA, MS SQL Server interview questions, MSBI Interview Questions, Multiple Column Pivoting Puzzle, Multiplication of Digits Puzzle, Multiply digits of a number, multiply digits of a number puzzle, nth maximum & minimum salary in a single sql query, Objective Puzzle, Pawan, Pawan Khowal, Pawan Kumar, Pawan Kumar Khowal, PL/SQL Challenges, Prev Value puzzle, Previous value puzzle, Prime numbers after given number puzzle, Prime numbers using SQL, Program to Print prime numbers, Puzzle SQL, puzzle sql developer, Puzzle SQl Server, Puzzles, PUzzles in SQL SERVER, Queries for SQL Interview, Records not ending with a character Puzzle, Records not ending with s Puzzle, Replace string puzzle, Reverse a string without loop, reverse function or recursive CTE, SELECT Puzzle, Single Query multiple conditions puzzle, Smart Replace puzzle, Split & Count Puzzle, Split & count puzzle in SQL, Split and Count puzzle SQL Server, SQL, sql - How to find third or nth maximum salary from salary table?, SQL - The Pattern Puzzles, SQL 2012, SQL 2014, SQL 2014 Interview Questions, SQL Challenge, SQL Challenges, SQL Extreme Difficult Puzzle, SQL FAQ, SQL FAQs, SQL Interview Q & A, SQL Interview Questions, SQL Interview Questions - Part 2, SQL Interview Questions for SQL Professionals‎, SQL Joins, SQL Pattern Puzzles, SQL pl/sql puzzles, SQL prime number puzzle, SQL puzzle, SQL Puzzle | 2nd Highest Salary in Each Department using OffSet & Fetch Method, SQL Puzzle | Break Table in Equal Parts, SQL Puzzle | Find Different Values Puzzle, SQL Puzzle | Get Weeks (Start Date - End Date) for a Month & Year, SQL Puzzle | Multiplication of Digits Puzzle, SQL Puzzle | Star Pattern Puzzle ( Fun with SQL ), SQL Puzzle | The Fiscal Puzzle - II, SQL Puzzle | The Middle 4 Puzzle, SQL Puzzle | The Time Difference Puzzle, SQL Puzzles, sql puzzles & answers, sql puzzles and answers free download, sql puzzles and answers pdf, sql puzzles for interview, sql puzzles oracle, SQL Queries, SQL Queries asked in interviews, SQL QUERY PUZZLES, SQL Query to Find Nth Highest Salary of Employee, SQL Questions, SQL Quiz, SQL Replace Puzzle, SQL Server - Common Interview Questions and Answers, SQL SERVER - Find Nth Highest Salary of Employee, SQL Server - General Interview Questions and Answers, sql server - How to find the nth highest salary in SQL?, sql server 2008 interview questions, SQL Server 2008 Interview Questions and Answers, SQL Server Database, SQL Server database developer interview questions and answers, sql server dba interview questions, SQL Server developer Interview questions and answers, SQL Server developer Interview questions with answers, SQL Server Developer T-SQL Interview Questions, SQL server filter index with LIKE and RIGHT function?, SQL SERVER Indexes, SQL Server Interview Puzzle, SQL SERVER Interview questions, SQL SERVER Interview questions & Answers, SQL Server Interview Questions - Part 1, SQL Server Interview questions and answers, SQL Server Interview Questions and Answers - Free PDF, SQL SERVER Interview questions and answers for experienced, sql server interview questions and answers for net developers, SQL Server Interview Questions And Answers.pdf, sql server interview questions by Pawan Khowal sql interview questions, SQL SERVER Interview questions pdf, SQL Server Interview Questions | MSBISKILLS.Com, SQL Server Interview Questions | MSBISKILLS.com Top 50 SQL Server Questions & Answers, SQL Server Interview Questions/Answers Part-1, SQL Server Puzzle, SQL SERVER Puzzles, SQL server Questions and Answers, SQL SERVER Tips, SQL Skills, SQL Sudoku, SQL Tips & Tricks, SQL Tips and Tricks, SQL Top clause, SQL Tough Puzzle, SQL Tricks, SQL Trikcy question, sql/database interview for puzzle sql developer, SQLBI Interview Questions, SQLSERVER, Star Pattern Puzzle, Sum of digits puzzle, Sum of digits SQL puzzle, T SQL Puzzles, T-SQL Challenge, T-SQL Interview Questions | SQL Server Interviews and Jobs, T-SQL Puzzle, T-SQL Query | [ Replace 6 Consecutive Digits with x from a string Puzzle ], T-SQL Query | [ The Complex Week Puzzle ], T-SQL Server Interview Questions, T-SQL Tricky Puzzles, The % percentage puzzle, The Biggest Gap Puzzle, The Fibonacci Series Puzzle, The Gap Puzzle, The Gap Puzzle Puzzle, The In Puzzle, The InNotIn Puzzle, The Middle 4 Puzzle, The Middle 4 SQL Puzzle, The Multiple Sums Puzzle, The new rows count puzzle, The Prime Number Puzzle, The Replace Puzzle, The Time Difference Puzzle, The Tree Puzzle, The Update Puzzle, Top 10 Frequently Asked SQL Query Interview Questions, TOP 100 SQL SERVER INTERVIEW QUESTIONS QUERIES, Top 50 SQL Server Interview Question for Testers, TOP Clause, Tough SQL Challenges, Tough SQL Puzzles, Tricky Questions, TSQL, TSQL Challenge, TSQL Challenges, TSQL Interview questions, TSQL Puzzle, TSQL Puzzles, TSQL Queries, Week puzzle, [Solved] sql query for nth highest & lowest salary


SQL Puzzle | Generate IDs Puzzle

In the puzzle you have to combine 2 tables together and generate Ids to the rows for the second table

This puzzle was asked @SQL Server Central. Please check out the sample input and the expected output below-

Sample Input

Table 1

Id Name
1 AAA
2 AAA
3 AAA

Table 2

Id Name
NULL BBB
NULL BBB
NULL BBB

Expected Output

Id Name
1 AAA
2 AAA
3 AAA
4 BBB
5 BBB
6 BBB

Solution 1 | Union All & Row Numbers


--


SELECT ROW_NUMBER() OVER (ORDER BY NAME) Id , Name FROM
(
	SELECT Id,Name FROM [dbo].[Table1]
	UNION ALL
	SELECT Id,Name FROM [dbo].[Table2]
)r


--

Add a comment if you have any other solution in mind. I would love to learn it. We all need to learn.

Enjoy !!! Keep Learning

Pawan Khowal 

Http://MSBISkills.com

Share this

  • LinkedIn
  • Facebook
  • Twitter
  • WhatsApp
  • Email

SQL Server Interview Questions – Set 8 (10 Questions )

13 Monday Jun 2016

Posted by Pawan Kumar Khowal in Download SQL Interview Q's, SQL Concepts, SQL SERVER, SQL Server Interview Questions

≈ Leave a comment

Tags

Advanced SQL Interview Questions and Answers, Advanced SQL tutorial pdf, Download SQL Server Interview Question in PDF, Download SQL SERVER Interview questions, Download SQL Server Interview questions and answers, download sql server interview questions and answers pdf, download sql server interview questions by Pawan Khowal, download sql server interview questions by Pawan Kumar, download sql server interview questions by Pawan Kumar Khowal, Download T-SQL Interview Questions, Server Interview Questions Book, SQL Questions


SQL Server Interview Questions – Set 8 (10 Questions )

Sr No Download Download URL
13 SQL Server Interview Questions AND Answers [SET 8](10 Questions & Answers) Download

Question 1 : Can you tell me the difference between Name Node and Data Node?

Answer :

This question belongs to Big Data. Differences below-

NameNode
• NameNode is the centrepiece of HDFS.
• NameNode is also known as the Master
• NameNode only stores the metadata of HDFS – the directory tree of all files in the file system, and tracks the files across the cluster.
• NameNode does not store the actual data or the dataset. The data itself is actually stored in the DataNodes.
• NameNode knows the list of the blocks and its location for any given file in HDFS. With this information NameNode knows how to construct the file from blocks.
• NameNode is so critical to HDFS and when the NameNode is down, HDFS/Hadoop cluster is inaccessible and considered down.
• NameNode is a single point of failure in Hadoop cluster.
• NameNode is usually configured with a lot of memory (RAM). Because the block locations are help in main memory.

DataNode
• DataNode is responsible for storing the actual data in HDFS.
• DataNode is also known as the Slave
• NameNode and DataNode are in constant communication.
• When a DataNode starts up it announce itself to the NameNode along with the list of blocks it is responsible for.
• When a DataNode is down, it does not affect the availability of data or the cluster. NameNode will arrange for replication for the blocks managed by the DataNode that is not available.
• DataNode is usually configured with a lot of hard disk space. Because the actual data is stored in the DataNode.

Question 2 : Can you tell internal objects SQL Server Engine created inside TempDB?

Answer :

Internal Object in TempDB are
• Sorts
• Worktables
• WorkFiles
• VersionStore

Sorts-
Consider this as a workspace memory or Query memory. Whenever the operation was not fit in the memory it spills to tempDB. In case of Index sorting, it is spilled to User DB unless you specify the option “SORT _IN_TEMPDB”. It is technically not a work table. They use Uniform Extents for storage.

Worktables
Worktables are temporary rowsets. SQL Engine will call an internal routine to build a table. They are same as Temporary tables just like developers create using Create Table command. These are just pages in tempDB and they have –ve object ids. They use Mixed Extents.

Worktables are used for below operations-

• XML Documents
• Spool Operators
• Hash Match
• Exchange Spill
• Merge Join
• LOB Variables
• Cursors
• Service Broker

WorkFiles
WorkFiles are only used for one purpose that is temporary storage for Hashing. They use Uniform Extents

Version Store
They are used for Snapshot isolation including RCSI DB option. It is also used for online index rebuilding operations, triggers and MARS. Internally these extents are marked as “Version Store”.

Question 3 : How SQL Server gets SELECT Count(*) – Will it always use Table Scan every time?

Answer :

SELECT COUNT(*) will always use the smallest index structure that contains the total number of rows. The Optimizer will do a leaf level scan of the smallest nonclustered index for count(*) as well as count(not-nullable column)

Obviously if there is not index then the SQL optimizer will have to use table Scan.

Example – Table Scan


--

CREATE TABLE COUNTSTAR
(
	ID INT IDENTITY(1,1)
)
GO

INSERT INTO COUNTSTAR DEFAULT VALUES
GO 10000

SELECT COUNT(*) FROM COUNTSTAR

--

Execution Plan for the above query

Execution Plan - Table Scan

Execution Plan – Table Scan

Example – Index Scan


--

CREATE TABLE COUNTSTARIndex
(
	 ID  INT IDENTITY(1,1) PRIMARY KEY
	,ID0 INT DEFAULT 0
	,ID1 BIGINT DEFAULT 1
	,ID2 VARCHAR(10) DEFAULT 'Pawan'
)
GO

INSERT INTO COUNTSTARIndex DEFAULT VALUES
GO 10000

CREATE NONCLUSTERED INDEX Ix_ID0 ON COUNTSTARIndex(ID0)
GO

CREATE NONCLUSTERED INDEX Ix_ID1 ON COUNTSTARIndex(ID1)
GO

CREATE NONCLUSTERED INDEX Ix_ID2 ON COUNTSTARIndex(ID2)
GO

SELECT COUNT(*) FROM COUNTSTARIndex

--

Execution Plan for the above query

Execution Plan - Index Scan ( Smallest Structure )

Execution Plan – Index Scan ( Smallest Structure )

Question 4 : What is the Output of below Query?


--

CREATE TABLE # (a INT, b INT);

--

Answer :

The Output of the above query will be “Command Executed Successfully”.

Question 5 : What is the Output of below Queries?


--

CREATE TABLE food (a INT, b INT);

select a, b from food order by a, b;
select a, b from food order by 1, 2;

select a, b, count(*) from food group by a, b;
select a, b, count(*) from food group by 1, 2;

--

Answer :

First 3 queries will be executed successfully and provide data to the end user.Last query will return below error.

“Msg 164, Level 15, State 1, Line 7 Each GROUP BY expression must contain at least one column that is not an outer reference.”

Question 6 : Can you tell me the purpose of NTILE ranking function in SQL Server?

Answer :

The NTILE ranking function distributes the rows in an ordered partition into a specified number of groups. The groups are numbered, starting at one. For each row, NTILE returns the number of the group to which the row belongs.

Example


--

CREATE TABLE EmpRangers
(
     EmpId INT
    ,EmpName VARCHAR(10)
    ,Salary INT
)
GO

INSERT INTO EmpRangers
VALUES
(1,'a',1000),
(2,'b',900),
(3,'c',100),
(4,'d',1100),
(5,'e',1300),
(6,'f',700),
(7,'g',330),
(8,'h',800),
(9,'i',500),
(10,'j',340),
(11,'k',600),
(12,'l',700),
(13,'m',1000)
GO

INSERT INTO EmpRangers
VALUES (14,'n',1800)
GO

-- Query with Ntile ranking function

SELECT
             Salary
            ,NTILE(3) OVER (ORDER BY Salary) Grouper
    FROM
            EmpRangers

--

Question 7 : You have got a table. You have to write a SELECT query which will fetch alternate rows from the table starting with first row?

Answer :

We can use % mod here to get alternate rows.

Example


--

CREATE TABLE EmpRangers
(
     EmpId INT
    ,EmpName VARCHAR(10)
    ,Salary INT
)
GO

INSERT INTO EmpRangers
VALUES
(1,'a',1000),
(2,'b',900),
(3,'c',100),
(4,'d',1100),
(5,'e',1300),
(6,'f',700),
(7,'g',330),
(8,'h',800),
(9,'i',500),
(10,'j',340),
(11,'k',600),
(12,'l',700),
(13,'m',1000)
GO

SELECT EmpId,EmpName,Salary FROM EmpRangers
WHERE EmpId % 2 <> 0
GO

--

Question 8 : Can you tell me how many triggers can we create on a table?

Answer :

We can create 1 Instead of Trigger and Multiple After Triggers on a table. Also note that we can only create Instead of Triggers on a view.

Question 9 : How many kinds of database files present in SQL Server?

Answer :

There are three basic types of database files present in SQL Server Database-
• Primary data file ( Extension – mdf )
• Secondary data file ( Extension – ndf )
• Log file ( Extension – ldf )

Primary data files
This data file is mandatory whenever you create a new database, this file contains all the startup parameter of a database when SQL Server try to bring database online or when you create a new database on SQL Server. The common/recommended extension of primary data files is “.mdf” and it holds the system objects of a database such as tables, store procedures, views and functions.

Secondary data files
These are optional user defined data files; they hold user defined database objects such as tables, store procedures, views and functions. There could be multiple secondary files of a database. The common/recommended extension used for Secondary data files is “.ndf”

Log Files
Log files contain transactional information of a database’s day to day processing and are very important for database recovery process. All the transaction sequence and information is stored in these files. Every database has to have one log file in order to be operational.

DataFiles in SQL Server

DataFiles in SQL Server

Question 10 : What are Niladic functions in SQL Server?

Answer :

Niladic functions are functions that do not accept any parameters, are specified without parentheses, and return only one result.. “Nil” means nothing or null, and “adic” when is prefixed, means arguments.

Example


--

SELECT  CURRENT_TIMESTAMP
SELECT  CURRENT_USER
SELECT  SESSION_USER
SELECT  SYSTEM_USER
SELECT  USER

--

That’s all folks; I hope you’ve enjoyed the article and I’ll see you soon with some more articles.

Thanks!

Pawan Kumar Khowal

MSBISKills.com

Share this

  • LinkedIn
  • Facebook
  • Twitter
  • WhatsApp
  • Email

SQL Puzzle | REVERSE a String Without Using Reverse Function / Recursive CTE / While Loop

13 Monday Jun 2016

Posted by Pawan Kumar Khowal in SQL SERVER, SQL Server Interview Questions, T SQL Puzzles, Tricky SQL Queries

≈ 1 Comment

Tags

Advanced SQL Interview Questions and Answers, Advanced SQL tutorial pdf, any recommended websites for sql puzzles, Best SQL Puzzles, Buy SQL Server Interview Questions Book Online at Low Price, Calculating nth Max salary, Check Prime Number puzzle, Circular prime number puzzle, Circular Prime Number Puzzlre, COmples tSQL puzzles, Complex SQL Challenges, complex sql statement(puzzle), Complex tsql, Complex TSQL Challenge, Delete Duplicate data from table without using Aggregate & Ranking functions, Divide rows into two columns, Download SQL Questions, Download SQL Server Interview Question in PDF, Download SQL SERVER Interview questions, Download SQL Server Interview questions and answers, download sql server interview questions and answers pdf, download sql server interview questions by Pawan Khowal, download sql server interview questions by Pawan Kumar, download sql server interview questions by Pawan Kumar Khowal, Download T-SQL Interview Questions, Even Odd Count Puzzle, Excellent t-SQL puzzles, Fibonacci Series puzzle, Find nth highest salary, Find Nth maximum value in SQL Server, find records not ending with s, find records not ending with s puzzle, Finding prime numbers with SQL server, Free Download SQL SERVER Interview questions, generate Fibonacci Series in SQL Server, Generate possible combinations puzzle, Get nth highest and lowest salary of an employee, implementation and administration, Improve SQL Skills, Interesting Interview Questions, Interview Puzzles in SQL Server, Interview Qs.SQL SERVER Questions, Interview questions and Answers for MS SQL Server designing, Interview Questions and Answers For SQL, Interview questions on Joins, Interview Questions on SQL, Interview SQL Puzzles, InterviewQuestions, InterviewQuestions for SQL, Joins, Joins Interview questions, Joins Puzzle, Khowal, last non null puzzle, Learn complex SQL, Learn SQL, Learn T-SQL, Microsoft SQL Server interview questions for DBA, MS SQL Server interview questions, MSBI Interview Questions, Multiple Column Pivoting Puzzle, Multiplication of Digits Puzzle, Multiply digits of a number, multiply digits of a number puzzle, nth maximum & minimum salary in a single sql query, Objective Puzzle, Pawan, Pawan Khowal, Pawan Kumar, Pawan Kumar Khowal, PL/SQL Challenges, Prev Value puzzle, Previous value puzzle, Prime numbers after given number puzzle, Prime numbers using SQL, Program to Print prime numbers, Puzzle SQL, puzzle sql developer, Puzzle SQl Server, Puzzles, PUzzles in SQL SERVER, Queries for SQL Interview, Records not ending with a character Puzzle, Records not ending with s Puzzle, Replace string puzzle, Reverse a string without loop, reverse function or recursive CTE, SELECT Puzzle, Single Query multiple conditions puzzle, Smart Replace puzzle, Split & Count Puzzle, Split & count puzzle in SQL, Split and Count puzzle SQL Server, SQL, sql - How to find third or nth maximum salary from salary table?, SQL - The Pattern Puzzles, SQL 2012, SQL 2014, SQL 2014 Interview Questions, SQL Challenge, SQL Challenges, SQL Extreme Difficult Puzzle, SQL FAQ, SQL FAQs, SQL Interview Q & A, SQL Interview Questions, SQL Interview Questions - Part 2, SQL Interview Questions for SQL Professionals‎, SQL Joins, SQL Pattern Puzzles, SQL pl/sql puzzles, SQL prime number puzzle, SQL puzzle, SQL Puzzle | 2nd Highest Salary in Each Department using OffSet & Fetch Method, SQL Puzzle | Break Table in Equal Parts, SQL Puzzle | Find Different Values Puzzle, SQL Puzzle | Get Weeks (Start Date - End Date) for a Month & Year, SQL Puzzle | Multiplication of Digits Puzzle, SQL Puzzle | Star Pattern Puzzle ( Fun with SQL ), SQL Puzzle | The Fiscal Puzzle - II, SQL Puzzle | The Middle 4 Puzzle, SQL Puzzle | The Time Difference Puzzle, SQL Puzzles, sql puzzles & answers, sql puzzles and answers free download, sql puzzles and answers pdf, sql puzzles for interview, sql puzzles oracle, SQL Queries, SQL Queries asked in interviews, SQL QUERY PUZZLES, SQL Query to Find Nth Highest Salary of Employee, SQL Questions, SQL Quiz, SQL Replace Puzzle, SQL Server - Common Interview Questions and Answers, SQL SERVER - Find Nth Highest Salary of Employee, SQL Server - General Interview Questions and Answers, sql server - How to find the nth highest salary in SQL?, sql server 2008 interview questions, SQL Server 2008 Interview Questions and Answers, SQL Server Database, SQL Server database developer interview questions and answers, sql server dba interview questions, SQL Server developer Interview questions and answers, SQL Server developer Interview questions with answers, SQL Server Developer T-SQL Interview Questions, SQL server filter index with LIKE and RIGHT function?, SQL SERVER Indexes, SQL Server Interview Puzzle, SQL SERVER Interview questions, SQL SERVER Interview questions & Answers, SQL Server Interview Questions - Part 1, SQL Server Interview questions and answers, SQL Server Interview Questions and Answers - Free PDF, SQL SERVER Interview questions and answers for experienced, sql server interview questions and answers for net developers, SQL Server Interview Questions And Answers.pdf, sql server interview questions by Pawan Khowal sql interview questions, SQL SERVER Interview questions pdf, SQL Server Interview Questions | MSBISKILLS.Com, SQL Server Interview Questions | MSBISKILLS.com Top 50 SQL Server Questions & Answers, SQL Server Interview Questions/Answers Part-1, SQL Server Puzzle, SQL SERVER Puzzles, SQL server Questions and Answers, SQL SERVER Tips, SQL Skills, SQL Sudoku, SQL Tips & Tricks, SQL Tips and Tricks, SQL Top clause, SQL Tough Puzzle, SQL Tricks, SQL Trikcy question, sql/database interview for puzzle sql developer, SQLBI Interview Questions, SQLSERVER, Star Pattern Puzzle, Sum of digits puzzle, Sum of digits SQL puzzle, T SQL Puzzles, T-SQL Challenge, T-SQL Interview Questions | SQL Server Interviews and Jobs, T-SQL Puzzle, T-SQL Query | [ Replace 6 Consecutive Digits with x from a string Puzzle ], T-SQL Query | [ The Complex Week Puzzle ], T-SQL Server Interview Questions, T-SQL Tricky Puzzles, The % percentage puzzle, The Biggest Gap Puzzle, The Fibonacci Series Puzzle, The Gap Puzzle, The Gap Puzzle Puzzle, The In Puzzle, The InNotIn Puzzle, The Middle 4 Puzzle, The Middle 4 SQL Puzzle, The Multiple Sums Puzzle, The new rows count puzzle, The Prime Number Puzzle, The Replace Puzzle, The Time Difference Puzzle, The Tree Puzzle, The Update Puzzle, Top 10 Frequently Asked SQL Query Interview Questions, TOP 100 SQL SERVER INTERVIEW QUESTIONS QUERIES, Top 50 SQL Server Interview Question for Testers, TOP Clause, Tough SQL Challenges, Tough SQL Puzzles, Tricky Questions, TSQL, TSQL Challenge, TSQL Challenges, TSQL Interview questions, TSQL Puzzle, TSQL Puzzles, TSQL Queries, Week puzzle, [Solved] sql query for nth highest & lowest salary


SQL Puzzle | REVERSE a String Without Using Reverse Function / Recursive CTE / While Loop

In the puzzle we have an input string and you have find the reverse of the input string without using while loop, recursive cte and T-SQL reverse function.

Please check out the sample input and the expected output below-

Sample Input

InputStr
noitcnuf )(ESREVER gnisu tuo htiw gnirts a esreveR

Expected Output

InputStr OutputStr
Reverse a string with out using REVERSE() function noitcnuf )(ESREVER gnisu tuo htiw gnirts a esreveR

Solution 1 | Using Numbers Table


--


DECLARE @StringToReverse VARCHAR(55) = ''
SET @StringToReverse = 'Reverse a string with out using REVERSE() function'
DECLARE @Output AS VARCHAR(100) = ''

IF OBJECT_ID('tempdb..#x') IS NOT NULL
    DROP TABLE #x

SELECT NUMBER, SUBSTRING(@StringToReverse,Number,1) Chr, ROW_NUMBER() OVER (ORDER BY Number DESC) r INTO #x FROM 
( 

	SELECT 
		  DISTINCT NUMBER 
	FROM 
		  MASTER..SPT_VALUES
	WHERE
		  NUMBER > 0 AND NUMBER <= DATALENGTH(@StringToReverse) 

)h
ORDER BY NUMBER DESC

SELECT @Output=@Output+Chr FROM #x

SELECT @StringToReverse InputStr , @Output OutputStr

IF OBJECT_ID('tempdb..#x') IS NOT NULL
    DROP TABLE #x


--

Add a comment if you have any other solution in mind. I would love to learn it. We all need to learn.

Enjoy !!! Keep Learning

Pawan Khowal 

Http://MSBISkills.com

Share this

  • LinkedIn
  • Facebook
  • Twitter
  • WhatsApp
  • Email

Blog Stats

  • 1,085,263 hits

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 1,131 other subscribers

Pawan Khowal

502 SQL Puzzles with answers

Achievement - 500 PuzzlesJuly 18, 2018
The big day is here. Finally presented 500+ puzzles for SQL community.

200 SQL Server Puzzle with Answers

The Big DayAugust 19, 2016
The big day is here. Completed 200 SQL Puzzles today

Archives

June 2016
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
27282930  
« May   Jul »

Top Articles

  • pawankkmr.wordpress.com/2…
  • pawankkmr.wordpress.com/2…
  • pawankkmr.wordpress.com/2…
  • pawankkmr.wordpress.com/2…
  • pawankkmr.wordpress.com/2…

Archives

  • October 2020 (29)
  • September 2018 (2)
  • August 2018 (6)
  • July 2018 (25)
  • June 2018 (22)
  • May 2018 (24)
  • April 2018 (33)
  • March 2018 (35)
  • February 2018 (53)
  • January 2018 (48)
  • December 2017 (32)
  • November 2017 (2)
  • October 2017 (20)
  • August 2017 (8)
  • June 2017 (2)
  • March 2017 (1)
  • February 2017 (18)
  • January 2017 (2)
  • December 2016 (5)
  • November 2016 (23)
  • October 2016 (2)
  • September 2016 (14)
  • August 2016 (6)
  • July 2016 (22)
  • June 2016 (27)
  • May 2016 (15)
  • April 2016 (7)
  • March 2016 (5)
  • February 2016 (7)
  • December 2015 (4)
  • October 2015 (23)
  • September 2015 (31)
  • August 2015 (14)
  • July 2015 (16)
  • June 2015 (29)
  • May 2015 (25)
  • April 2015 (44)
  • March 2015 (47)
  • November 2012 (1)
  • July 2012 (8)
  • September 2010 (26)
  • August 2010 (125)
  • July 2010 (2)

Article Categories

  • Analysis Services (6)
    • DAX (6)
  • Data (2)
    • Data warehousing (2)
  • Integration Services (2)
  • Magazines (3)
  • Python (29)
  • Reporting Services (4)
  • SQL SERVER (820)
    • Download SQL Interview Q's (212)
    • SQL Concepts (323)
    • SQL Performance Tuning (155)
    • SQL Puzzles (331)
    • SQL SERVER 2017 Linux (6)
    • SQL Server Interview Questions (308)
    • SQL SERVER Puzzles (332)
    • T SQL Puzzles (547)
    • Tricky SQL Queries (439)
  • UI (30)
    • ASP.NET (5)
    • C# (13)
    • CSS (9)
    • OOPS (3)
  • Uncategorized (5)

Recent Posts

  • Python | The Print and Divide Puzzle October 30, 2020
  • Python | Count consecutive 1’s from a list of 0’s and 1’s October 30, 2020
  • Python | How to convert a number into a list of its digits October 26, 2020
  • Python | Validate an IP Address-IPV6(Internet Protocol version 6) October 26, 2020
  • Python | Print the first non-recurring element in a list October 26, 2020
  • Python | Print the most recurring element in a list October 26, 2020
  • Python | Find the cumulative sum of elements in a list October 26, 2020
  • Python | Check a character is present in a string or not October 26, 2020
  • Python | Check whether a string is palindrome or not October 26, 2020
  • Python | Find the missing number in the array of Ints October 26, 2020
  • Python | How would you delete duplicates in a list October 26, 2020
  • Python | Check whether an array is Monotonic or not October 26, 2020
  • Python | Check whether a number is prime or not October 26, 2020
  • Python | Print list of prime numbers up to a number October 26, 2020
  • Python | Print elements from odd positions in a list October 26, 2020
  • Python | Print positions of a string present in another string October 26, 2020
  • Python | How to sort an array in ascending order October 26, 2020
  • Python | How to reverse an array October 26, 2020
  • Python | Find un-common words from two strings October 26, 2020
  • Python | How to convert a string to a list October 26, 2020
  • Python | Find unique words from a string October 26, 2020
  • Python | Calculate average word length from a string October 26, 2020
  • Python | Find common words from two strings October 26, 2020
  • Python | Find the number of times a substring present in a string October 26, 2020
  • Python | Find maximum value from a list October 26, 2020
  • Python | How to find GCF of two numbers October 26, 2020
  • Python | How to find LCM of two numbers October 26, 2020
  • Python | How to convert a list to a string October 26, 2020
  • Python | Replace NONE by its previous NON None value October 26, 2020
  • Microsoft SQL Server 2019 | Features added to SQL Server on Linux September 26, 2018

Create a website or blog at WordPress.com

  • Follow Following
    • Improving my SQL BI Skills
    • Join 231 other followers
    • Already have a WordPress.com account? Log in now.
    • Improving my SQL BI Skills
    • Customize
    • Follow Following
    • Sign up
    • Log in
    • Report this content
    • View site in Reader
    • Manage subscriptions
    • Collapse this bar
 

Loading Comments...
 

You must be logged in to post a comment.