SQL Query Interview Questions
Q1. SQL QUERY TO FIND SECOND HIGHEST SALARY OF EMPLOYEE
Ans: There are numerous ways of finding second most significant pay of Employee in SQL, you can either utilize SQL Join or Subquery to tackle this issue. Here is SQL inquiry utilizing Subquery:
select MAX(Salary) from Employee WHERE Salary NOT IN (select MAX(Salary) from Employee );
Q2. GET FIRST_NAME,LAST_NAME FROM EMPLOYEE TABLE
Ans: Select first_name, Last_Name from worker
Q3. SQL QUERY TO FIND MAX SALARY FROM EACH DEPARTMENT.
Ans: You can track down the most extreme compensation for every division by gathering all records by DeptId and afterward utilizing MAX() capability to work out greatest compensation in each gathering or every office.
SELECT DeptID, MAX(Salary) FROM Employee GROUP BY DeptID.
These inquiries become really fascinating assuming that Interviewer will request that you print division name rather than office id, all things considered, you really want to join Employee table with Department utilizing unfamiliar key DeptID, ensure you in all actuality do LEFT or RIGHT OUTER JOIN to incorporate divisions with next to no representative also. Here is the question
SELECT DeptName, MAX(Salary) FROM Employee e RIGHT JOIN Department d ON e.DeptId = d.DeptID GROUP BY DeptName;
In this question, we have utilized RIGHT OUTER JOIN since we want the name of the division from Department table which is on the right half of JOIN proviso, regardless of whether there is no reference of dept_id on Employee table.
Q4. GET FIRST_NAME FROM EMPLOYEE TABLE USING ALIAS NAME "Representative NAME"
Ans: Select first_name Employee Name from representative
Q5. Compose SQL QUERY TO DISPLAY THE CURRENT DATE.
Ans: SQL has inherent capability called GetDate() which returns the current timestamp. This will work in Microsoft SQL Server, different sellers like Oracle and MySQL additionally has identical capabilities.
Q6. GET FIRST_NAME FROM EMPLOYEE TABLE IN UPPER CASE
Ans: Select upper(FIRST_NAME) from EMPLOYEE
Q7. Compose A SQL QUERY TO CHECK WHETHER DATE PASSED TO QUERY IS THE DATE OF GIVEN FORMAT OR NOT.
Ans: SQL has IsDate() capability which is utilized to check passed esteem is a date or not of indicated design, it returns 1(true) or 0(false) in like manner. Recall ISDATE() is a MSSQL capability and it may not chip away at Oracle, MySQL or some other data set however there would be something almost identical.
SELECT ISDATE('1/08/13') AS "MM/DD/YY";
It will return 0 on the grounds that passed date isn't in right configuration
Q8. GET FIRST_NAME FROM EMPLOYEE TABLE IN LOWER CASE
Ans: Select lower(FIRST_NAME) from EMPLOYEE
Q9. Compose A SQL QUERY TO PRINT THE NAME OF THE DISTINCT EMPLOYEE WHOSE DOB IS BETWEEN 01/01/1960 TO 31/12/1975.
Ans: This SQL question is interesting, however you can use BETWEEN proviso to get all records whose date fall between two dates.
SELECT DISTINCT EmpName FROM Employees WHERE DOB BETWEEN '01/01/1960' AND '31/12/1975';
Q10. GET UNIQUE DEPARTMENT FROM EMPLOYEE TABLE
Ans: select particular DEPARTMENT from EMPLOYEE
Q11. SELECT FIRST 3 CHARACTERS OF FIRST_NAME FROM EMPLOYEE
Ans: Oracle Equivalent of SQL Server SUBSTRING is SUBSTR, Query : select substr(FIRST_NAME,0,3) from worker
SQL Server Equivalent of Oracle SUBSTR is SUBSTRING, Query : select substring(FIRST_NAME,1,3) from worker
MySQL Server Equivalent of Oracle SUBSTR is SUBSTRING. In MySQL start position is 1, Query : select substring(FIRST_NAME,1,3) from worker
Q12. GET POSITION OF 'O' IN NAME 'JOHN' FROM EMPLOYEE TABLE
Ans: Oracle Equivalent of SQL Server CHARINDEX is INSTR, Query : Select instr(FIRST_NAME,'o') from representative where first_name='John'
SQL Server Equivalent of Oracle INSTR is CHARINDEX, Query: Select CHARINDEX('o',FIRST_NAME,0) from representative where first_name='John'
MySQL Server Equivalent of Oracle INSTR is LOCATE, Query: Select LOCATE('o',FIRST_NAME) from representative where first_name='John'
GET FIRST_NAME FROM EMPLOYEE TABLE AFTER REMOVING WHITE SPACES FROM RIGHT SIDE
select RTRIM(FIRST_NAME) from representative
GET FIRST_NAME FROM EMPLOYEE TABLE AFTER REMOVING WHITE SPACES FROM LEFT SIDE