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