SQL Query Interview Questions
by Venkatesan M
GET ALL EMPLOYEE DETAILS FROM THE EMPLOYEE TABLE
Select * from employee
SQL QUERY TO FIND SECOND HIGHEST SALARY OF EMPLOYEE
There are many ways to find second highest salary of Employee in SQL, you can either use SQL Join or Subquery to solve this problem. Here is SQL query using Subquery:
select MAX(Salary) from Employee WHERE Salary NOT IN (select MAX(Salary) from Employee );
GET FIRST_NAME,LAST_NAME FROM EMPLOYEE TABLE
Select first_name, Last_Name from employee
SQL QUERY TO FIND MAX SALARY FROM EACH DEPARTMENT.
You can find the maximum salary for each department by grouping all records by DeptId and then using MAX() function to calculate maximum salary in each group or each department.
SELECT DeptID, MAX(Salary) FROM Employee GROUP BY DeptID.
These questions become more interesting if Interviewer will ask you to print department name instead of department id, in that case, you need to join Employee table with Department using foreign key DeptID, make sure you do LEFT or RIGHT OUTER JOIN to include departments without any employee as well. Here is the query
SELECT DeptName, MAX(Salary) FROM Employee e RIGHT JOIN Department d ON e.DeptId = d.DeptID GROUP BY DeptName;
In this query, we have used RIGHT OUTER JOIN because we need the name of the department from Department table which is on the right side of JOIN clause, even if there is no reference of dept_id on Employee table.
GET FIRST_NAME FROM EMPLOYEE TABLE USING ALIAS NAME “EMPLOYEE NAME”
Select first_name Employee Name from employee
WRITE SQL QUERY TO DISPLAY THE CURRENT DATE.
SQL has built-in function called GetDate() which returns the current timestamp. This will work in Microsoft SQL Server, other vendors like Oracle and MySQL also has equivalent functions.
GET FIRST_NAME FROM EMPLOYEE TABLE IN UPPER CASE
Select upper(FIRST_NAME) from EMPLOYEE
WRITE AN SQL QUERY TO CHECK WHETHER DATE PASSED TO QUERY IS THE DATE OF GIVEN FORMAT OR NOT.
SQL has IsDate() function which is used to check passed value is a date or not of specified format, it returns 1(true) or 0(false) accordingly. Remember ISDATE() is an MSSQL function and it may not work on Oracle, MySQL or any other database but there would be something similar.
SELECT ISDATE(‘1/08/13’) AS “MM/DD/YY”;
It will return 0 because passed date is not in correct format
GET FIRST_NAME FROM EMPLOYEE TABLE IN LOWER CASE
Select lower(FIRST_NAME) from EMPLOYEE
WRITE AN SQL QUERY TO PRINT THE NAME OF THE DISTINCT EMPLOYEE WHOSE DOB IS BETWEEN 01/01/1960 TO 31/12/1975.
This SQL query is tricky, but you can use BETWEEN clause 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’;
GET UNIQUE DEPARTMENT FROM EMPLOYEE TABLE
select distinct DEPARTMENT from EMPLOYEE
SELECT FIRST 3 CHARACTERS OF FIRST_NAME FROM EMPLOYEE
Oracle Equivalent of SQL Server SUBSTRING is SUBSTR, Query : select substr(FIRST_NAME,0,3) from employee
SQL Server Equivalent of Oracle SUBSTR is SUBSTRING, Query : select substring(FIRST_NAME,1,3) from employee
MySQL Server Equivalent of Oracle SUBSTR is SUBSTRING. In MySQL start position is 1, Query : select substring(FIRST_NAME,1,3) from employee
GET POSITION OF 'O' IN NAME 'JOHN' FROM EMPLOYEE TABLE
Oracle Equivalent of SQL Server CHARINDEX is INSTR, Query : Select instr(FIRST_NAME,’o’) from employee where first_name=’John’
SQL Server Equivalent of Oracle INSTR is CHARINDEX, Query: Select CHARINDEX(‘o’,FIRST_NAME,0) from employee where first_name=’John’
MySQL Server Equivalent of Oracle INSTR is LOCATE, Query: Select LOCATE(‘o’,FIRST_NAME) from employee where first_name=’John’
GET FIRST_NAME FROM EMPLOYEE TABLE AFTER REMOVING WHITE SPACES FROM RIGHT SIDE
select RTRIM(FIRST_NAME) from employee
GET FIRST_NAME FROM EMPLOYEE TABLE AFTER REMOVING WHITE SPACES FROM LEFT SIDE
select LTRIM(FIRST_NAME) from employee
September 20, 2019
January 29, 2019
May 26, 2018