Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2017 October SQL Puzzle: SQL Advance Query – Partition Employee Table in Three parts

SQL Puzzle: SQL Advance Query – Partition Employee Table in Three parts

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

Check the below input data and expected output to divide employee table into three partitions basis on salary ranges.

Input Data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
EmpId EmpName Salary
----------- ---------- -----------
1 abc 10000
2 xyz 20000
3 bcd 40000
4 rtf 30000
5 qwe 45000
6 tyu 65000
7 fgh 52000
8 dfc 35000
9 asd 45000
10 xcv 52000
11 olp 65000
12 lmj 25000
13 vbn 20000
14 efe 10000

Expected Output:

1
2
3
4
5
NumberOfPartitions SalaryRanges Counts
-------------------- ------------------------- -----------
1 10000-25000 5
2 30000-45000 5
3 52000-65000 4

Create a table with data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
CREATE TABLE tbl_Emp
(
EmpId INT
,EmpName VARCHAR(10)
,Salary INT
)
GO
INSERT INTO tbl_Emp
VALUES
(1,'abc',10000),
(2,'xyz',20000),
(3,'bcd',40000),
(4,'rtf',30000),
(5,'qwe',45000),
(6,'tyu',65000),
(7,'fgh',52000),
(8,'dfc',35000),
(9,'asd',45000),
(10,'xcv',52000),
(11,'olp',65000),
(12,'lmj',25000),
(13,'vbn',20000),
(14,'efe',10000)
GO

Solution: Using NTILE()

1
2
3
4
5
6
7
8
9
10
11
12
13
;WITH CTE AS
(
SELECT
Salary
,NTILE(3) OVER (ORDER BY Salary) as NumberOfPartitions
FROM tbl_Emp
)
SELECT
NumberOfPartitions
,CONCAT(MIN(Salary),'-',MAX(Salary)) SalaryRanges
,COUNT(*) Counts
FROM CTE
GROUP BY NumberOfPartitions

Please try the different solution for this puzzle and share it via comment...

Oct 9, 2017Anvesh Patel
SQL Puzzle: SQL Advance Query - Find the Column name which has all NULLSQL Puzzle: SQL Advance Query - Solve Date Overlapping Problem
Comments: 1
  1. Dinesh.IS
    October 11, 2017 at 7:26 am

    Declare @Cnt As Numeric(19,2),@NoOfTimes Int,@LoopTime Int,@ID As Int;
    Declare @SalaryDetails Table(ID Int,EmpID Int,EmpName VarChar(500),Salary Int)
    Delete From @SalaryDetails
    Set @Cnt=(Select Count(*) From TBL_EMP)
    Set @NoOfTimes=Ceiling(@Cnt/3)
    Set @LoopTime=3
    Set @ID=1
    While (@ID<=@LoopTime)
    Begin

    Insert Into @SalaryDetails
    Select @ID,* From (
    Select TOP (@NoOfTimes) * From TBL_EMP
    Where EmpID Not In(Select EmpID From @SalaryDetails)
    Order By Salary ASC ) A
    Set @ID=@ID+1

    End

    Select ID,Min(Salary) As FromAmount,Max(Salary) As ToAmount,Count(EMPID) As NoOfEmp
    From @SalaryDetails
    Group By ID

    ReplyCancel

Leave a Reply to Dinesh.IS Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

October 9, 2017 1 Comment SQL PuzzleAnvesh Patel, database, database research and development, dbrnd, NTILE, SQL Advance Query, SQL Interview, SQL Problem, SQL Programming, SQL Puzzle, SQL Query, SQL Tips and Tricks
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....