SQL Top 30
SQL Top 30
interview questions,
1. Write a query to fetch the EmpFname from the EmployeeInfo table in the
upper case and use the ALIAS name as EmpName.
4. Write a query to retrieve the first four characters of EmpLname from the
EmployeeInfo table.
5. Write a query to fetch only the place name(string before brackets) from the
Address column of EmployeeInfo table.?
Ans: Using the MID function in MySQL
SELECT MID(Address, 0, LOCATE('(',Address)) FROM EmployeeInfo;
OR
Using SUBSTRING
6. Write a query to create a new table that consists of data and structure copied
from the other table.
Ans: SELECT * INTO NewTable FROM EmployeeInfo WHERE 1 = 0;
OR
CREATE TABLE NewTable AS SELECT * FROM EmployeeInfo;
7. Write q query to find all the employees whose salary is between 50000 to
100000.?
Ans: SELECT * FROM EmployeePosition WHERE Salary BETWEEN
'50000' AND '100000';
8. Write a query to find the names of employees that begin with ‘S’
Ans: SELECT * FROM EmployeeInfo WHERE EmpFname LIKE 'S%';
10. Write a query to retrieve the EmpFname and EmpLname in a single column
as “FullName”. The first name and the last name must be separated with
space.
Ans:SELECT CONCAT(EmpFname, ' ', EmpLname) AS 'FullName' FROM
EmployeeInfo;
Q11. Write a query find number of employees whose DOB is between
02/05/1970 to 31/12/1975 and are grouped according to gender
12. Write a query to fetch all the records from the EmployeeInfo table
ordered by EmpLname in descending order and Department in the
ascending order.
16. Write a query to fetch all employees who also hold the managerial
position.
To retrieve the even records from a table, you have to use the MOD() function as
follows:
OR
WHERE EXISTS
21. Write a query to find the Nth highest salary from the table without
using TOP/limit keyword.
SELECT Salary FROM EmployeePosition E1 WHERE N-1 = ( SELECT
COUNT( DISTINCT ( E2.Salary ) ) FROM EmployeePosition E2 WHERE
E2.Salary > E1.Salary );
26. Write a query to display the first and the last record from the
EmployeeInfo table.
To display the first record from the EmployeeInfo table, you can write a query as
follows:
OR
29. Write a query to retrieve EmpPostion along with total salaries paid
for each of them.
30. Write a query to fetch 50% records from the EmployeeInfo table.
SELECT *