I really enjoyed users participation in my previous question. Read SQL SERVER – Interesting Interview Questions before continuing reading this article. This interview question was about user participation and about how good and how different you can come with your T-SQL script. What I really liked is that many users took this test seriously and did their best to answer. I really want to congratulate all the readers who have attempted to answer this question.
As I have said earlier it did not matter what is the database structure, but it mattered what should be the good database architecture design. Here it only mattered if you can write T-SQL based on question.
Following readers have got correct answer.
I really want to thank and congratulate readers who have answered this question correct. I would suggest that you book mark this article as well original article for your personal reference. In future if you need ever recommendation regarding any interview I will be happy to list this articles for you in reference.
Now let us see their solution in detail.Please go over interview question again before continuing reading following comments.
Solution byZod
DECLARE @Class1 INT, @Class2 INT, @Class3 INT,
@Class1Total INT, @Class2Total INT, @Class3Total INT
SET @Class1Total = 0
SET @Class2Total = 0
SET @Class3Total = 0
DECLARE student_CURSOR CURSORĀ FOR
SELECT Class1, Class2, Class3
FROM StudentsEnroll
OPEN student_CURSOR
FETCH next FROM student_CURSOR
INTO @Class1, @Class2, @Class3
WHILE @@FETCH_STATUS = 0
BEGIN
SET @Class1Total = @Class1Total + @Class1
SET @Class2Total = @Class2Total + @Class2
SET @Class3Total = @Class3Total + @Class3
FETCH next FROM student_CURSOR
INTO @Class1, @Class2, @Class3
END
CLOSE student_CURSOR
DEALLOCATE student_CURSOR
SELECT 'Class1Ā hasĀ ' + CAST(@Class1Total ASĀ VARCHAR(10))Ā + 'Ā students'
UNION ALL
SELECT 'Class2Ā hasĀ ' + CAST(@Class2Total ASĀ VARCHAR(10))Ā + 'Ā students'
UNION ALL
SELECT 'Class3Ā hasĀ ' + CAST(@Class3Total ASĀ VARCHAR(10))Ā + 'Ā students'
GO
Solution by Eric
SELECT
'Class1Ā hasĀ ' + CAST(SUM(CAST([Class1] ASĀ INT)) ASĀ VARCHAR(10))Ā + 'Ā students.\n'
+ 'Class2Ā hasĀ ' + CAST(SUM(CAST([Class2] ASĀ INT)) ASĀ VARCHAR(10))Ā + 'Ā students.\n'
+ 'Class3Ā hasĀ ' + CAST(SUM(CAST([Class3] ASĀ INT)) ASĀ VARCHAR(10))Ā + 'Ā students.\n'
FROM [StudentsEnroll];
Solution by Imran Mohammed
SELECT CLASS+ 'Ā hasĀ '+CONVERT(VARCHAR(10),COUNT(orders ))+'Ā StudentsĀ 'Ā 'Output' FROM (
SELECT students, Class, Orders
FROM
(SELECT students, class1, class2,class3
FROM studentsenroll ) p
UNPIVOT
(Orders FOR Class IN
(class1, class2, class3 )
)AS unpvt) X WHERE orders = 1 GROUPĀ BY class
Solution by bhadeliaimran
DECLARE @opXml AS XML
SET @opXml = '<ClassStudĀ value=''' + REPLACE(
(SELECT
'Class1Ā hasĀ ' + CAST(SUM(CAST(Class1 AS SMALLINT)) ASĀ VARCHAR(4))Ā + 'Ā students' ,
',Class2Ā hasĀ ' + CAST(SUM(CAST(Class2 AS SMALLINT)) ASĀ VARCHAR(4))Ā + 'Ā students' ,
',Class3Ā hasĀ ' + CAST(SUM(CAST(Class3 AS SMALLINT)) ASĀ VARCHAR(4))Ā + 'Ā students'
FROM [StudentsEnroll] FOR XMLĀ PATH('')Ā ), ',', '''Ā /><ClassStudĀ value=''')Ā + '''Ā />'
SELECT x.value('@value', 'varchar(100)') AS [output] FROM @opXml.nodes('/ClassStud') AS p(x)
Solution by fly
DECLARE @tbl TABLE (i INT)
INSERTĀ INTO @tbl (i)
SELECT 1
UNION
SELECT 2
UNION
SELECT 3
SELECT
CASE WHEN i = 1 THEN 'ClassĀ 1Ā hasĀ ' + CAST(SUM(CAST (class1 ASĀ INT)) ASĀ VARCHAR(100))Ā + 'Ā Students' ELSE
CASE WHEN i = 2 THEN 'ClassĀ 2Ā hasĀ ' + CAST(SUM(CAST (class2 ASĀ INT)) ASĀ VARCHAR(100))Ā + 'Ā Students' ELSE
CASE WHEN i = 3 THEN 'ClassĀ 3Ā hasĀ ' + CAST(SUM(CAST (class3 ASĀ INT)) ASĀ VARCHAR(100))Ā + 'Ā Students' ELSE '' END
END
END
FROM [StudentsEnroll] JOIN @tbl t ON i <= 3
GROUPĀ BY i
Solution by pom
SELECT 'class1Ā hasĀ ' + CAST((SELECT COUNT(*) FROM StudentsEnroll WHERE class1=1 GROUPĀ BY class1) ASĀ CHAR(3))Ā + 'Students'
UNION
SELECT 'class2Ā hasĀ ' + CAST((SELECT COUNT(*) FROM StudentsEnroll WHERE class2=1 GROUPĀ BY class2) ASĀ CHAR(3))Ā + 'Students'
UNION
SELECT 'class3Ā hasĀ ' + CAST((SELECT COUNT(*) FROM StudentsEnroll WHERE class3=1 GROUPĀ BY class3) ASĀ CHAR(3))Ā + 'Students'
Other two solution proposed earlier in original article.
Solution 1 – Using only SELECT statement
DECLARE @Col INT
SET @Col = 1
WHILE (@Col < 4)
BEGIN
EXEC(āSELECTĀ Ā Ā Ā Ā āClassā+@Col+āĀ HasĀ āĀ +Ā CAST(COUNT(Students)Ā ASĀ VARCHAR(100))Ā +Ā āĀ StudentsāĀ Results
FROMĀ Ā Ā Ā Ā Ā Ā Ā Ā studentsenroll
WHEREĀ Ā Ā Ā Ā Ā Ā Ā Ā Classā+@Col+āĀ =Ā 1
GROUPĀ BYĀ Ā Ā Ā Ā Ā Classā+@Col)
SET @Col = @Col + 1
CONTINUE
END
Solution 2 – Getting results in one resultset
SELECT āClass1Ā hasĀ ā + CAST(COUNT(*) ASĀ VARCHAR(10))Ā +āĀ Studentsā
FROM StudentsEnroll
WHERE Class1 = 1
GROUPĀ BY Class1
UNION ALL
SELECT āClass2Ā hasĀ ā + CAST(COUNT(*) ASĀ VARCHAR(10))Ā +āĀ Studentsā
FROM StudentsEnroll
WHERE Class2 = 1
GROUPĀ BY Class2
UNION ALL
SELECT āClass3Ā hasĀ ā + CAST(COUNT(*) ASĀ VARCHAR(10))Ā +āĀ Studentsā
FROM StudentsEnroll
WHERE Class3 = 1
GROUPĀ BY Class3
Now the real question is which of this articles are favorite solution of yours. Please leave your choice along with reason in comment.
I promise if you can not write solution but if you understand the proposed solution in this article you will usually pass any database T-SQL problem in interviews. Additionally, if you are looking for job in SQL Server related area please find your right job here. Make sure to read all the interview questions and answers for SQL Server 2008 before you appear for interview.
Reference : Pinal Dave (https://blog.sqlauthority.com)
7 Comments. Leave new
USE DATABASE
GO
SELECT ‘Class1 has ‘ + CAST(COUNT(Class1) AS VARCHAR(10)) + ‘ students.’
FROM StudentsEnroll
WHERE Class1 = ‘True’
UNION ALL
SELECT ‘Class2 has ‘ + CAST(COUNT(Class2) AS VARCHAR(10)) + ‘ students.’
FROM StudentsEnroll
WHERE Class2 = ‘True’
UNION ALL
SELECT ‘Class3 has ‘ + CAST(COUNT(Class3) AS VARCHAR(10)) + ‘ students.’
FROM StudentsEnroll
WHERE Class3 = ‘True’
GO
**************************
Best wishes
Solution by Imran Mohammed
SELECT CLASS+ ā has ā+CONVERT(VARCHAR(10),COUNT(orders ))+ā Students ā āOutputā FROM (
SELECT students, Class, Orders
FROM
(SELECT students, class1, class2,class3
FROM studentsenroll ) p
UNPIVOT
(Orders FOR Class IN
(class1, class2, class3 )
)AS unpvt) X WHERE orders = 1 GROUP BY class
My favor solution is above one, the reason is for better performance, execute this query only require 1 table scan count and 1 logical reads.
Helllo Sir
MY self vipul and i want to knw how we got Row number in MSSQLSERVER2005 and how we use custom paging in select statement ……….
Reply me on my mail it ………
–[Note] I am using SQL Server Version 2008
Declare @totClass TinyInt
Declare @sqlQuery NVarchar(Max)=”
Select @totClass=IsNull(COUNT(*),0) From sys.columns
where object_id=object_id(‘studentsample’) and name like ‘class%’
While(@totClass>=1)
Begin
Set @sqlQuery=@sqlQuery+’Select ”Class”+Convert(varchar(2),’+Convert(Varchar(2),@totClass)+’)+” has total ”
+CONVERT(varchar(3),sum(Class’+Convert(Varchar(2),@totClass)+’))+ ” students” From studentSample’
If(@totClass1)
Begin
Set @sqlQuery=@sqlQuery+’ Union All ‘
End
Set @totClass=@totClass-1
End
Exec(@sqlQuery)
Declare @totClass TinyInt
Declare @sqlQuery NVarchar(Max)=”
Select @totClass=IsNull(COUNT(*),0) From sys.columns
where object_id=object_id(‘studentsample’) and name like ‘class%’
While(@totClass>=1)
Begin
Set @sqlQuery=@sqlQuery+’Select ”Class”+Convert(varchar(2),’+Convert(Varchar(2),@totClass)+’)+” has total ”
+CONVERT(varchar(3),sum(Class’+Convert(Varchar(2),@totClass)+’))+ ” students” From studentSample’
If(@totClass1)
Begin
Set @sqlQuery=@sqlQuery+’ Union All ‘
End
Set @totClass=@totClass-1
End
Exec(@sqlQuery)
I am using oracle and this works fine-
select classname||’ has ‘||count(num)||’ students ‘as no_of_students from
(
select * from studentsenroll )
unpivot (
num for classname in (class1,class2,class3)
)
where num=1
group by classname;
how to make it dynamic for n class in oracle