Thursday, January 25, 2018

SQL INTERVIEW QUESTIONS (SCENARIO BASED) - Part 1


FINDING Second Highest Salary in SQL

CREATE TABLE #EMP
(
NAME NVARCHAR(100), SALARY BIGINT)

INSERT INTO #EMP VALUES('One',100)
INSERT INTO #EMP VALUES('Two',200)
INSERT INTO #EMP VALUES('Three',300)
INSERT INTO #EMP VALUES('Four',400)

-- USING CTE
;WITH CTEEmp AS
(
      SELECT ROW_NUMBER() OVER(ORDER BY SALARY DESC) AS RN,
      NAME,
      SALARY
      FROm #EMP
)


SELECT NAME, SALARY, RN FROM CTEEmp WHERE RN = 2

-- USING CO RELATED QUERIES
SELECT * FROM #EMP e1
WHERE 2 = (SELECT count(0) FROM #Emp e2 WHERE e1.Salary >= e2.Salary)


FIND THE Phone number's of the employees having salary > 200

CREATE TABLE #EMP
(
ID INT IDENTITY(1,1) , NAME NVARCHAR(100), SALARY BIGINT
)

INSERT INTO #EMP VALUES('One',100)
INSERT INTO #EMP VALUES('Two',200)
INSERT INTO #EMP VALUES('Three',300)
INSERT INTO #EMP VALUES('Four',400)


CREATE TABLE #EMPDetails
(
      ID INT IDENTITY(1,1) , [Address] NVARCHAR(100), PHONENumber BIGINT
)

INSERT INTO #EMPDetails VALUES('Delhi',12345)
INSERT INTO #EMPDetails VALUES('Noida',54321)
INSERT INTO #EMPDetails VALUES('Mumbai',32145)
INSERT INTO #EMPDetails VALUES('Pune',14532)


-- CTE WAY
;WITH CTE1 AS
(
      SELECT ROW_NUMBER() OVER(ORDER BY e.ID) AS RN,
      e.NAME,
      ed.PHONENumber
      FROM
      #Emp e JOIN #EmpDetails ed ON e.ID = ed.ID     
)

SELECT * FROM CTE1 WHERE RN > 2

-- SUB QUERY WAY
SELECT * FROM #EmpDetails WHERE ID IN (SELECT ID FROM #EMP WHERE SALARY > 200)


-- DELETE DUPLICATE RECORDS FROM A TABLE

-- DELETE DUPLICATE RECORDS FROM A TABLE:

CREATE TABLE #EMP
(
      NAME NVARCHAR(100), SALARY BIGINT
)

INSERT INTO #EMP VALUES('One',100)
INSERT INTO #EMP VALUES('Two',200)
INSERT INTO #EMP VALUES('Three',300)
INSERT INTO #EMP VALUES('Three',300)
INSERT INTO #EMP VALUES('Four',400)

SELECT * FROM #EMP

;WITH CTEDup AS
(
      SELECT ROW_NUMBER() OVER(PARTITION BY SALARY ORDER BY (SELECT 1)) AS RN,
      NAME,
      SALARY
      FROM #EMP
)

DELETE FROM CTEDUP WHERE RN > 1

SELECT * FROM #EMP
-------------------------------------------------------------------------


-- RANKING FUNCTIONS

CREATE TABLE #Customer
(
      NAME NVARCHAR(100), ProductName NVARCHAR(100), Amount BIGINT, ShopName NVARCHAR(100)
)

INSERT INTO #Customer VALUES('Vijay','Deo', 280, 'ParkAvenue')
INSERT INTO #Customer VALUES('Praveen','Perfume', 1000, 'KS')
INSERT INTO #Customer VALUES('Praveen','Shirt',1700, 'LP')
INSERT INTO #Customer VALUES('Tripuresh','Tee',900,'UCB')
INSERT INTO #Customer VALUES('Manish','Cap', 100, 'UCB')


SELECT * FROM #Customer


SELECT
ROW_NUMBER() OVER(ORDER BY NAME) AS OrderNumber,
NAME, ProductName, Amount, ShopName
FROM #Customer

SELECT
ROW_NUMBER() OVER(PARTITION BY NAME ORDER BY ShopName) AS CustomerNumber,
NAME, ProductName, Amount, ShopName
FROM #Customer
  
SELECT
RANK() OVER(ORDER BY Name) AS CustomerNumber,
NAME, ProductName, Amount, ShopName
FROM #Customer

-- RANK will give output as 1,1,3,4,5

SELECT
DENSE_RANK() OVER(ORDER BY Name) AS CustomerNumber,
NAME, ProductName, Amount, ShopName
FROM #Customer

-- DENSE_RANK will give output as 1,1,2,3,4

Tuesday, January 23, 2018

C# Tricky Questions

Lets have an example of below classes:















Explanation:


A a = new A();
since new A object instance is created, and the static members loads when the class loads in the memory,
A's Static constructor will be called first.
then, A's public constructor will be called.





A c = new B();
Since new B object instance is created, but the reference is of A. The static constructor of B will be called first followed by A's static one. Then A's public constructor and B's public constructor will be called.





B d = new B();


Since new B object instance is created, the result will be same as above.





Similarly, if in the given scenario, there will be no static constructor defined in either of the classes, then static constructors calls would not be there in the result and the output would have followed the same sequence.







SQL Joins


SQL JOINS EXAMPLES:


Below are two tables #A and #B, There are A_id and B_id columns in the tables respectively. We have inserted a few values in both the tables. Please note that 50 is the common value in both tables.



INNER JOIN:


Matching records from both the tables will be the output:





LEFT OUTER JOIN:


All records from LEFT table and Matching records from Right table. NULL where the condition doesn't match:







RIGHT OUTER JOIN:


All records from RIGHT table and Matching records from LEFT table. NULL where the condition doesn't match:





FULL OUTER JOIN:


All records from both tables, Matching records from both tables and NULL wherever no match.





CROSS JOIN:


Cartesian product of both tables: 4*4 = 16 records in this case. Please note that there will be no condition in this case.





UNION:


Duplicate matching records entries are removed. All other records are union.







UNION ALL:


Duplicate entries will be there in this case.












SQL INTERVIEW QUESTIONS (SCENARIO BASED) - Part 1

FINDING Second Highest Salary in SQL CREATE TABLE #EMP ( NAME NVARCHAR ( 100 ), SALARY BIGINT ) INSERT INTO #EMP VALUES (...