Tags
Advanced SQL Interview Questions and Answers, Advanced SQL tutorial pdf, alphanumeric values, any recommended websites for sql puzzles, Best SQL Puzzles, Buy SQL Server Interview Questions Book Online at Low Price, COmples tSQL puzzles, Complex SQL Challenges, complex sql statement(puzzle), Complex tsql, Complex TSQL Challenge, convert string to proper case in sql server, 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, Free Download SQL SERVER Interview questions, Get Next Value Puzzle, Huge blank areas in database field, Improve SQL Skills, 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, Learn complex SQL, Learn SQL, Learn T-SQL, Microsoft SQL Server interview questions for DBA, MS SQL Server interview questions, MSBI Interview Questions, numeric or alphanumeric values numeric values, Objective Puzzle, Pawan, Pawan Khowal, Pawan Kumar, Pawan Kumar Khowal, PL/SQL Challenges, Puzzle SQL, puzzle sql developer, Puzzle SQl Server, Puzzles, PUzzles in SQL SERVER, Queries for SQL Interview, Remove Huge Multiple Spaces from a DB Fields, Remove Multiple Spaces from a DB Fields, select query to find numeric or alphanumeric values, select query to identify numeric or alphanumeric values, Separate Int and char from a string column, Single Quote update SQL, SQL, 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 group by only rows which are in sequence, 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 | The KIND of DATA Puzzle | SQL Interview Question, 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 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 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, SUM of grouped COUNT in SQL Query, T SQL Puzzles, T-SQL Challenge, T-SQL Interview Questions | SQL Server Interviews and Jobs, T-SQL Puzzle, T-SQL Server Interview Questions, T-SQL Tricky Puzzles, The KIND of DATA Puzzle, The Status Puzzle, The type of DATA Puzzle, Top 10 Frequently Asked SQL Query Interview Questions, TOP 100 SQL SERVER INTERVIEW QUESTIONS QUERIES, Top 50 SQL Server Interview Question for Testers, Tough SQL Challenges, Tough SQL Puzzles, Tricky Questions, TSQL, TSQL Challenge, TSQL Challenges, TSQL Interview questions, TSQL Puzzle, TSQL Puzzles, TSQL Queries
SQL Puzzle | The KIND of DATA Puzzle | SQL Interview Question
In this puzzle you have to identify the type of data present for each Id value. The rules are-.
1. If only numeric values are present for an Id then we should present ‘NUMERIC’ in front of that Id.
2. If only NON numeric values are present for an Id then we should present ‘NON NUMERIC’ in front of that Id.
3. If both numeric and NON numeric values are present for an Id then we should present ‘Combined’ in front of that Id.
Please check the sample input and the expected output.
Sample Input
Id | Vals |
---|---|
1 | a |
1 | 1 |
2 | 1 |
2 | 1b |
3 | 2 |
4 | a |
4 | b |
5 | a |
5 | 341z |
6 | 1 |
6 | a |
6 | b |
7 | 1 |
8 | 10 |
8 | $ |
Expected Output
Id | Vals | TypeOfData |
---|---|---|
1 | a | Combined |
1 | 1 | Combined |
2 | 1 | Combined |
2 | 1b | Combined |
3 | 2 | NUMERIC |
4 | a | NON NUMERIC |
4 | b | NON NUMERIC |
5 | a | NON NUMERIC |
5 | 341z | NON NUMERIC |
6 | 1 | Combined |
6 | a | Combined |
6 | b | Combined |
7 | 1 | NUMERIC |
8 | 10 | Combined |
8 | $ | Combined |
Use below script to create table and insert sample data into it.
-- CREATE TABLE TestMultiples ( Id INT ,Vals VARCHAR(20) ) GO INSERT INTO TestMultiples VALUES (1,'a'), (1,'1'), (2,'1'), (2,'1b'), (3,'2'), (4,'a'), (4,'b'), (5,'a'), (5,'341z'), (6,'1'), (6,'a'), (6,'b'), (7,'1') GO INSERT INTO TestMultiples VALUES (8,'10'), (8,'$') GO SELECT * FROM TestMultiples GO -- |
Rules/Restrictions
The solution should be should use “SELECT” statement or “CTE”.
Add your solution(s) in the comments section or send you solution(s) to pawankkmr@gmail.com
SOLUTION – 1
-- SELECT Id,Vals, IIF( SUM(CASE WHEN a = 'NUMERIC' THEN 1 END) OVER (PARTITION BY Id) +SUM(CASE WHEN a = 'NON NUMERIC' THEN 1 END) OVER (PARTITION BY Id) IS NULL, a, 'Combined') TypeOfData FROM ( SELECT Id,vals , CASE WHEN TRY_CAST(Vals AS NUMERIC(20)) IS NOT NULL THEN 'NUMERIC' WHEN TRY_CAST(Vals AS NUMERIC(20)) IS NULL THEN 'NON NUMERIC' END a FROM TestMultiples )k -- |
Output-1
-- Id Vals TypeOfData ----------- -------------------- ----------- 1 a Combined 1 1 Combined 2 1 Combined 2 1b Combined 3 2 NUMERIC 4 a NON NUMERIC 4 b NON NUMERIC 5 a NON NUMERIC 5 341z NON NUMERIC 6 1 Combined 6 a Combined 6 b Combined 7 1 NUMERIC 8 10 Combined 8 $ Combined (15 rows affected) -- |
SOLUTION – 2 | By Vaibhav Goel – WITHOUT Vals COLUMN
-- ;WITH CTE AS ( SELECT Id,vals , CASE WHEN TRY_CAST(Vals AS NUMERIC(20)) IS NOT NULL THEN 'NUMERIC' WHEN TRY_CAST(Vals AS NUMERIC(20)) IS NULL THEN 'NON NUMERIC' END a FROM TestMultiples ) SELECT ID ,CASE WHEN cn> 1 THEN 'Combined' WHEN CN = 1 AND m = 'Numeric' THEN 'NUMERIC' WHEN cn = 1 and m = 'NON NUMERIC' THEN 'NON NUMERIC' END d FROM ( Select ID,COUNT(DISTINCT a) cn,MAX(a) m from cte GROUP BY ID ) ab -- |
Output-2 – WITHOUT Vals COLUMN
-- ID d ----------- ----------- 1 Combined 2 Combined 3 NUMERIC 4 NON NUMERIC 5 NON NUMERIC 6 Combined 7 NUMERIC 8 Combined (8 rows affected) -- |
Add a comment if you have any other or better solution in mind. I would love to learn it. We all need to learn.
Pawan Khowal
Pawan is a SQL Server Developer. If you need any help in writing code/puzzle or training please email at – pawankkmr”AT”gmail.com. Meanwhile please go throgh the top pages from his blog.
Page Detail | URL |
---|---|
☛ SQL Advance Puzzles | https://msbiskills.com/tsql-puzzles-asked-in-interview-over-the-years/ |
☛ SQL Tricky Queries | https://msbiskills.com/sql-puzzles-finding-outputs/ |
☛ SQL Server Perfomance tuning Articles | https://msbiskills.com/sql-performance-tuning/ |
☛ SQL Server Articles | https://msbiskills.com/t-sql/ |
☛ SQL Interview Questions & Answers | https://msbiskills.com/sql-server-interview-questions/ |
My SQL Groups on Facebook:
1. If you like this post, you may want to join my SQL SERVER Interview Puzzles/Interview Questions on Facebook: https://www.facebook.com/groups/1430882120556342/
2. If you like this post, you may want to join my SQL Server Puzzles on Facebook:
https://www.facebook.com/groups/206594023090781/
My SQL Page on Facebook:
2. For all the updates you may follow my page -> https://www.facebook.com/MSBISkillscom-1602779883299222/
Enjoy !!! Keep Learning
Http://MsbiSkills.com
;with cte
as
(
select ID,vals,
case when vals not like ‘%[^0-9]%’ then ‘numeric’
when vals like ‘%[^0-9]%’ then ‘nonNumeric’
end as ‘typeofdata’
from #TestMultiples
)
,cte1
as
(
select ID,COUNT(distinct typeofdata) as c,MAX(typeofdata) as m
from cte
group by ID
)
select A.Id,B.VALS ,case when c>1 then ‘COMBINED’
WHEN C=1 AND M=’NUMERIC’ THEN ‘NUMERIC’
WHEN C=1 AND M=’NONNUMERIC’ THEN ‘NONNUMERIC’
END AS typeofdata
FROM cte1 A
JOIN #TestMultiples B
ON A.Id=B.Id
LikeLiked by 1 person
Great as always 🙂 !!
LikeLike