Posts

Showing posts from April, 2021

When we can use abstract class & interface? Why we are using abstract class & interface?

  When to use Abstract Class? An abstract class is a good choice if we are using the inheritance concept since it provides a common base class implementation to derived classes. An abstract class is also good if we want to declare non-public members.  In an interface, all methods must be public. If we want to add new methods in the future, then an abstract class is a better choice. Because if we add new methods to an interface, then all of the classes that already implemented that interface will have to be changed to implement the new methods. When to use interface? If the functionality we are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing a common functionality to unrelated classes. Interfaces are a good choice when we think that the API will not change for a while. Interfaces are also good when we want to have something

MySQL query asked in interviews?

Concat two columns in SELECT query - SELECT employeeid, CONCAT(firstname, lastname) AS employee_name FROM employee; Concat by adding space -> CONCAT(firstname, ' ', lastname) AS employee_name Write a query to find even records in MYSQL.   SELECT * FROM EMPLOYEE WHERE id IN(SELECT id FROM EMPLOYEE WHERE id%2 = 0); Write a query to find odd records in MYSQL. SELECT * FROM EMPLOYEE WHERE id IN(SELECT id FROM EMPLOYEE WHERE id%2 <> 0); Get duplicate records from table SELECT name, count(name) FROM employee GROUP BY name HAVING(name) > 1 Get 4th Highest salary with limit function and without using limit functions SELECT name,email from users ORDER BY DESC LIMIT 4,1 Finding the highest salary select MAX(Salary) from Employee; Finding the 2nd highest salary Query-1 SELECT MAX(Salary) FROM Employee WHERE Salary NOT IN (SELECT MAX(Salary) FROM Employee); Query-2 select MAX(Salary) from Employee WHERE Salary <> (select MAX(Salary) from Employee ) Finding the nth highest